Search code examples
next.jssendgridnodemailervercel

Nodemailer in vercel not sending email in production


I'm using Nodemailer to send emails in my serverless Next.js project, deployed in Vercel, which works perfectly in development mode. But I'm having problems in production. No error returned, everything works the same way as is development mode, except I don't receive any email.

I have another project built with React and deployed in Heroku where I send emails the same way and it works fine, development and production, so I understand the problem is with Vercel.

Yes, I enabled "Allow Less Secured Apps" in Google account and yes, I enabled Captcha.

I also read this https://vercel.com/docs/solutions/email but it doesn't really make me understand what I should do in my case. I can see it's a matter of SMTP but I don't know what exactly.

Anybody experienced this kind of problem? How can I fix this?

const transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    port: 465,
    auth: {
        user: [email protected],
        pass: myEmailPass
    }
});
            
const mailOptions = {
    from: `${req.body.name} ${req.body.email}`,
    to: [email protected],
    subject: `${req.body.subject}`,
    text: `Text: ${req.body.text}`
}
            
transporter.sendMail(mailOptions, (err, res) => {
    if(err) {
          console.log(err);
    } else {
          console.log("success");
    }
});

UPDATE

I changed to SendGrid: made an account, created an API Key, and changed the code like so(instead the one above):

sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
    to: `[email protected]`,
    from: `[email protected]`,
    subject: `${req.body.subject}`,
    text: `${req.body.text}`
};
sgMail
.send(msg)
.then(() => {
     console.log('email sent')
})
.catch((error) => {
     console.error("error", error)
});

It logs out "email sent" but I don't receive any email. It's the same problem like with Nodemailer. I'm confused now...


Solution

  • I have already encountered the same problem, nodemailer was not working on vercel but on heroku everything worked perfectly. it is specified in the doc that vercel does not block stmp connections but according to what I have experienced, in practice stmp connections are blocked. what you can do is use an alternative to nodemailer. use sendgrid and it works fine

    An article on how integrating Sendgrid with Next.js