Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodemailer not working in prod lambda function but works in development (local) #1712

Open
thsanki opened this issue Jan 22, 2025 · 1 comment

Comments

@thsanki
Copy link

thsanki commented Jan 22, 2025

I'm using Nodemailer to send emails in my backend, which is based on a serverless Lambda function. The setup works perfectly in my local environment and even in a cron job running on an EC2 instance. However, when deployed to a Lambda function, it works sporadically but frequently fails with the error SMTP connection timeout exceeded.

Below is the relevant code:


const nodemailer = require('nodemailer');

const smtpTransporter = nodemailer.createTransport({
  host: process.env.HOST,
  port: 465,
  secure: true, // Use SSL
  auth: {
    user: process.env.SMTP_EMAIL,
    pass: process.env.SMTP_PASSWORD,
  },
  tls: {
    rejectUnauthorized: false,
  },
  logger: true,
  debug: true,
});

const sendMailTransporter = (mailOptions) => {
  try {
    console.log("Attempting to send email...");

    return smtpTransporter.sendMail(mailOptions, (error, info) => {
      console.log('info: ', info);
      console.log('error: ', error);
      if (error) {
        console.error('Sending Email Error:', error);
        return;
      }
      console.log('Message sent to: %s', info.accepted);
      console.log('Message sent from: %s', info.envelope);
    });
  } catch (err) {
    console.error('Unexpected Error:', err);
  }
};

Error Details:

INFO  Sending Email Error   Error: Invalid login: Host Error: timeout exceeded
    at SMTPConnection._formatError (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:790:19)
    at SMTPConnection._actionAUTHComplete (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:1564:34)
    at SMTPConnection.<anonymous> (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:546:26)
    at SMTPConnection._processResponse (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:969:20)
    at SMTPConnection._onData (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:755:14)
    at SMTPConnection._onSocketData (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:193:44)
    at TLSSocket.emit (node:events:517:28)
    at TLSSocket.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:368:12)
    at readableAddChunk (node:internal/streams/readable:341:9) {
  code: 'EAUTH',
  response: 'Host Error: timeout exceeded',
  responseCode: 421,
  command: 'AUTH PLAIN'
}

Steps I've Taken:

Increased the connectionTimeout to 4 minutes.

Ensured that the Lambda function's timeout is set to 100 seconds.

Verified that the environment variables (HOST, SMTP_EMAIL, SMTP_PASSWORD) are correct.

Despite these efforts, the issue persists. Emails sometimes send successfully but often fail with a timeout error.

@achintha-weerasinghe
Copy link

achintha-weerasinghe commented Jan 27, 2025

I'm also using SES transport. The email is delivered successfully, but the process hangs, and eventually, I get a timeout from lambda.

const ses = new aws.SES({ region: process.env.REGION });

const transporter = nodemailer.createTransport({
    SES: { ses, aws },
    sendingRate: 1, // max 1 messages/second
    maxConnections: 1,
  });

const result = await transporter.sendMail({
    from: `Admin ${process.env.FROM_EMAIL}`,
    to: body.toAddresses,
    subject: `Order Successful (${body.templateData.id})`,
    html: template(body.templateData),
  });

console.log(`Processed message ${result.messageId}`);

transporter.close();

I have intentionally copied only the relevant parts from my function code. The console log never gets printed. I tried using the callback instead of awaiting the promise. The callback never gets called (there are no errors either).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants