node.jsemailnodemailerdkimdmarc

Nodemailer - massive delay or missing emails in production


I currently have a site using Nodemailer & Gmail that works fine in my local development environment - any email sends instantly to the desired address.

Sadly, in production, only the admin notifications are being sent and the user ones are taking a very long time to deliver or not delivering at all. Ones that have arrived successfully took around 1 hour. The receiving email for admin emails is of the same domain as the URL of the website which makes me consider whether it's a domain verification issue. Only external recipients seem to get the delay.

My code is as follows:

const nodemailer = require('nodemailer')

const gmailUser = process.env.GMAIL_USER
const gmailPass = process.env.GMAIL_PASS
const appTitle = process.env.APP_TITLE
const receivingEmail = process.env.MAIL_RECEIVE

const smtpTransporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        user: gmailUser,
        pass: gmailPass
    }
})

const emailConfirmation = (userEmail) => {
    const userMailOptions = {
        from: appTitle,
        to: userEmail,
        subject: `${appTitle} - User Confirmation`
        text: 'User Confirmation'
    }
    const adminMailOptions = {
        from: appTitle,
        to: receivingEmail,
        subject: `${appTitle} - Admin Confirmation`,
        text: 'Admin Confirmation'
    }

    Promise.all([
        smtpTransporter.sendMail(userMailOptions),
        smtpTransporter.sendMail(adminMailOptions)
    ])
        .then((res) => { return true })
        .catch((err) => { console.log("Failed to send email confirmations: ", err); return false })
}

I then call the function in a POST handler as follows:

emailConfirmation(user.email)

Am I doing something wrong in my code, or is this likely to be some sort of domain verification error?


Solution

  • I ended up deciding to switch to a different mail provider and have since had no issues.