Connection Timeouts with Nodemailer

We need to know your netlify site name.: pacesettersolutionsllc.com

I am using nodemailer with my google workspace account to try and send emails for people who contact us from our site. I am having timeout issues. I am not having any issues when I test this on my local computer. Is there some type of setting that needs to be set to fix this timeout issue? Are email ports blocked? I know that I have purchased my domain through netlify and added MX records for my site already. Any help would be appreciated. Thanks!

Error: Connection timeoutJul 20, 04:43:44 PM: 2b0fe30c INFO at SMTPConnection._formatError (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:798:19)Jul 20, 04:43:44 PM: 2b0fe30c INFO at SMTPConnection._onError (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:784:20)Jul 20, 04:43:44 PM: 2b0fe30c INFO at Timeout. (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:237:22)Jul 20, 04:43:44 PM: 2b0fe30c INFO at listOnTimeout (node:internal/timers:569:17)Jul 20, 04:43:44 PM: 2b0fe30c INFO at processTimers (node:internal/timers:512:7) {Jul 20, 04:43:44 PM: 2b0fe30c INFO code: ‘ETIMEDOUT’,Jul 20, 04:43:44 PM: 2b0fe30c INFO command: 'CONN’Jul 20, 04:43:44 PM: 2b0fe30c INFO }

It’s unlikely you’ve reached the lambda timeout limit which is 10 seconds by default on Netlify. Can you explain further your stack, architecture and services used to send the mail (e.g. Postmark/AWS SES). I expect this to be a timeout on your SMTP server but it is unclear where the above log is from.

1 Like

@kylesloper I have a remix front end, using xata database, and netlify. That’s it. On the app, I have a contact form, which sends an email to one of my business partners if someone were to fill it out. I am using nodemailer for this through my google email account. I purchased my domain through netlify. I have a google workspace account that I have connected the emailing service in my netlify account which has my domain. I am able to email normally through gmail without any issues after I set up the MX records on my Netlify dashboard. This is the function that I have for sending the email. Like I mentioned in my original post, this all works locally, so I have reason to believe that something is blocking this on the Netlify servers.

export async function sendEmail(record: CONTACT_US) {
  const transporter = nodemailer.createTransport({
    service: "gmail",
    auth: {
      user: process.env.EMAIL_FROM_ADDRESS,
      pass: process.env.EMAIL_PASSWORD,
    },
  });

  const mailOptions = {
    from: process.env.EMAIL_FROM_ADDRESS,
    to: process.env.EMAIL_FROM_ADDRESS,
    subject: `New Contact Us Message`,
    text: `${record.first_name} ${record.last_name}\n${record.email}\n${record.company}\n${record.message}`,
    replyTo: record.email,
    priority: "high",
  };

  transporter.sendMail(mailOptions, function (error: any, info: any) {
    if (error) {
      console.log(error);
    } else {
      console.log("Contact Us Email sent: " + info.response);
    }
  });
}

I’ve tried to change service: "gmail" to

 host: "smtp.gmail.com",
    port: 587,
    secure: false, // use false for STARTTLS; true for SSL on port 465

and also

 host: "smtp.gmail.com",
    port: 465,
    secure: true, // use false for STARTTLS; true for SSL on port 465

but neither of those options are working when deployed on Netlify.

both of these alternates work immediately on my local computer environment.

Thanks for that extra info.

I doubt this is DNS related because as you said - with the current MX records - the app works fine locally. When troubleshooting prod-only issues, I’d double check all things Netlify has control over… e.g:

  • Environment Variables (make sure they are set to the correct scope and values match local .env file)
  • Lambda related limitations (specifically timeout and bandwidth)

Can you be clearer on where these errors are coming from? I’m not super familiar with proprietary Remix functions. Is this sendEmail function server side and using Lambda Serverless functions? (not edge functions).

Thanks

I double checked environment variables multiple times and they are being set correctly.

Remix handles both the server and client side code, and the code that is running this nodemailer function is happening on the server side. I guess it’s being used as a netlify function. My application was set up with Netlify functions not edge functions. I have my functions being set up here /my-remix-app/netlify/functions. According to Netlify, I have 2 Lambda functions running in production. But I have no clue what functions those are. Remix has a netlify template, and I used that set up my project. I’m not super familiar with what it sets up automatically for me. The sendMail function is definitely getting called (I seed some console log statements) but it keeps timing out. I feel like I need to take an alternate approach. I’m considering trying out a different tool. I’m also considering updating remix to use the latest version to see if that helps.

Small update. I tried to switch out nodemailer and use Postmark. Again, this worked fine locally. When I deployed to Netlify, it did not work. I don’t see any of the errors or anything. I know the function was called. I wonder if these messages are being swallowed or something. It’s definitely not sending the email. I believe this is going through a Postmark api, which is interesting because I am also using xata for a database service which works through an api and I have no problems with that. I also tried to enable Postmark through netlify’s integrations. That didn’t work either. Something is up. I’m thinking of updating remix to the latest version and see if that helps at all. Not sure if that’s my issue, but I guess it’s worth a shot.

I was finally able to get this working with Resend (another emailing service) ¯_(ツ)_/¯

1 Like

thanks for writing back in and providing your solution @danreale