Connecting Gmail SMTP with Fly.io

Hi everyone,

New to fly.io. I have a node ts express server hosted in fly.io and a separate client side hosted in cloudflare. I am using nodemailer to send emails from the clientside to my server and this is working well locally. However whenever I try to make calls to my deployed address:

await axios.post("https://my-server-v2.fly.dev/api/submitForm", values);

I get the error:
Error sending email: connect ECONNREFUSED 127.0.0.1:587

Can someone help me out with this? I’m using nodemailer and google’s smtp.

Not a node expert here, put you posted a code fragment that seemingly connects with my-server-v2.fly.dev on port 443, but your error message says something related to IP address 127.0.01 (which, in normal circumstances, is not mapped to my-server-v2.fly.dev) and port 587.

Can you explain your setup a little more?

How did you reach the conclusion that its on port 443?

Yes, I can elaborate and share my config.
This is my toml file, I have added the ports there (checked a few issues related to this) but it doesnt work.

[build]

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]

[[services]]
  internal_port = 25
  protocol = "tcp"

[[services.ports]]
    port = 25

[[services.ports]]
    handlers = ["tls", "http"]
    port = 465

[[services.ports]]
    port = 587

Then on my config file for the smtp i have the following:

import dotenv from "dotenv";
dotenv.config();

export const emailConfig = {
  service: process.env.SMTP_SERVICE,
  port: Number(process.env.SMTP_PORT),
  secure: false,
  auth: {
    user: process.env.SMTP_USERNAME,
    pass: process.env.SMTP_PASSWORD,
  },
};

export const receiverEmail = process.env.RECEIVER_EMAIL;

My serverside also as the method post(/api/submitForm) configurated.

Basically the .env PORT is 465 (it was 587 before), am I missing something else that I need to config?

I don’t know the inner workings of nodemailer, so I interpreted what I saw, and that was the axios https request (https = 443).

But it seems like the node server is trying to access host 127.0.0.1 which leads me to believe that you are actually not using Google’s SMTP server.

Does your .env contain SMTP_SERVICE (check the value)? Is the .env included in the Docker image or is it ignored / not copied?

If you connect to your VM via fly console ssh, can you find your .env there and take a look it its contents?

My .env has the following config:

SMTP_SERVICE=“gmail.com
SMTP_PORT="465

Does your .env contain SMTP_SERVICE (check the value)? Is the .env included in the Docker image or is it ignored / not copied?

Do you mean in the dockerfile? I’ve realised now that the dockerignore has .env in the ignore list.

Edit: I have added a `ENV PORT=465" to the dockerfile and the error seemed to have been fixed, now it displays a different one:

Error sending email: Missing credentials for "PLAIN"

So I’m checking the dockerfile again.

If .env is in your .dockerignore, then it won’t be included in your Docker image. You can set the env vars via fly.toml or secrets (fly secrets set ...). See the docs:

1 Like

Thank you. So this is now working.

Like you said the problem here was that my .env values were not being taken in consideration in my dockerfile. I configured the secrets and it worked. Thank you very much :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.