TMTP is not working properly when deployed to netlify

Hello Netlify community,

I’ve been using SMTP to send emails from web my app. It works flawlessly on my localhost environment but when I go to the netlify environment it’s unpredictable. Rarely it works and sends email (10% of the times) and most of the times it doesn’t work. Any idea what might be happening?
Here is the link to the site
https://63dcafd8d134110008ff5b4f--valuecumulation.netlify.app/

Here is how I am doing it:

import * as nodemailer from 'nodemailer'

export async function sendEmail({ to, subject, text, html }) {
  console.log('Inside sendEmail function')
  console.log('process.env.SMTP_HOST: ', process.env.SMTP_HOST)
  console.log('process.env.SMTP_PORT: ', process.env.SMTP_PORT)
  console.log('process.env.SMTP_USER: ', process.env.SMTP_USER)

  const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: process.env.SMTP_PORT,
    secure: false,
    auth: {
      user: process.env.SMTP_USER,
      pass: process.env.SMTP_PASS,
    },
  })
  console.log('Before transporter.sendMail')

  transporter.sendMail(
    {
      from: process.env.AUTH_EMAIL_FROM,
      to: Array.isArray(to) ? to : [to],
      subject,
      text,
      html,
    },
    (err, info) => {
      if (err) {
        console.log('Error sending email: ', err)
      } else {
        console.log('Email sent envelope:', info.envelope)
        console.log('Email sent messageId:', info.messageId)
      }
    }
  )
  console.log('After transporter.sendMail')
}

and when I check the function log, I see the following logs:

Feb 2, 10:59:04 PM: 04d1a94f INFO   Inside sendEmail function
Feb 2, 10:59:04 PM: 04d1a94f INFO   process.env.SMTP_HOST:  smtp-relay.sendinblue.com
Feb 2, 10:59:04 PM: 04d1a94f INFO   process.env.SMTP_PORT:  587
Feb 2, 10:59:04 PM: 04d1a94f INFO   process.env.SMTP_USER:  I_HIDE_MY_EMAIL
Feb 2, 10:59:04 PM: 04d1a94f INFO   Before transporter.sendMail
Feb 2, 10:59:04 PM: 04d1a94f INFO   After transporter.sendMail

Any idea what might be happening?

Thanks in advance.

This can possibly be debugged, but based on my attempts with Nodemailer (about 2 years ago), it did not play well. It could very much be a user error (mine in that case) as I wasn’t as fluent in JavaScript as I’m today. However, I have since found alternative solutions and did not have to look back:

Unless you’ve a strong reason to use Nodemailer, I’d recommend taking a look at the above thread and switching to that. If that doesn’t work, we can continue debugging this.

@hrishikesh Thanks for the suggestion. I was able to make nodemailer work but adding await statement before calling the async function. However, I do not like it this way. I’d like it to happen in the background and I don’t want my app to have a bottleneck like this. Can your approach run without await statement?

If it’s a Promise, it must use await.

I see - thanks @hrishikesh.

For future readers, if you’re using nodemailer the solution is using await before calling the sendEmail method.

If your sendEmail method is something like

import * as nodemailer from 'nodemailer'

export async function sendEmail({ to, subject, text, html }) {
  const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: process.env.SMTP_PORT,
    secure: false,
    auth: {
      user: process.env.SMTP_USER,
      pass: process.env.SMTP_PASS,
    },
  })

  const info = await transporter.sendMail({
    from: process.env.AUTH_EMAIL_FROM,
    to: Array.isArray(to) ? to : [to],
    subject,
    text,
    html,
  })

  if (info.messageId) {
    return {
      statusCode: 200,
      body: nodemailer.getTestMessageUrl(info),
    }
  }

  return {
    statusCode: 400,
    body: 'Oops',
  }
}

When you call this method later on, you’d have to call it with

await sendEmail