'Nodemailer says message sent and accepted, but the email was never sent // Process never completes

I am testing the ability for Nodemailer to send a test email to my personal email address. I used some of the sample code on Nodemailer's site to keep things simple and introduce as few variables as possible. However, I have two primary issues:

1) Although the response indicates the message was accepted by my email with a 250 status code, I can't find the message in my inbox or spam.

2) Although I have a return statement in my code, the function never completes -- that is, I have an ongoing "Loading" in my Postman and in my terminal it never returns me to the next line.

My nodemailer.js file (using the test accounts nodemailer recommends):

"use strict";
const nodemailer = require("nodemailer");

exports.mailer = async function() {
  try {
    let testAccount = await nodemailer.createTestAccount();
    console.log(testAccount);
    // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
      host: "smtp.ethereal.email",
      port: 587,
      secure: false, // true for 465, false for other ports
      auth: {
        user: testAccount.user, // generated ethereal user
        pass: testAccount.pass // generated ethereal password
      }
    });

    // send mail with defined transport object
    let info = await transporter.sendMail({
      from: '"Mike" <[email protected]>', // sender address
      to: "[email protected]", // list of receivers
      subject: "Hello ✔", // Subject line
      text: "Hello world?", // plain text body
      html: "<b>Hello world?</b>" // html body
    });

    console.log(info);

    console.log("Message sent: %s", info.messageId);
    // Message sent: <[email protected]>

    // Preview only available when sending through an Ethereal account
    console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
    // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
    return;
  }

  catch(err) {
    console.log('error!: ', err);
  }

}

The route that calls this function:

exports.reset = async function(req, res, next) {
  try {
    const user = await db.User.findOne({
      email: req.body.email
    });

    if (user) {
      mailer();
    }
  }
  catch(err) {
    console.log(err);
    return next({
      status: 400,
      message: 'No user was found with this email address'
    });
  }
};

Generated test account:

{ user: '[email protected]',
  pass: 'DySZuSDckx2Vb3zzRx',
  smtp: { host: 'smtp.ethereal.email', port: 587, secure: false },
  imap: { host: 'imap.ethereal.email', port: 993, secure: true },
  pop3: { host: 'pop3.ethereal.email', port: 995, secure: true },
  web: 'https://ethereal.email' }

Response:

{ accepted: [ '[email protected]' ],
  rejected: [],
  envelopeTime: 673,
  messageTime: 1157,
  messageSize: 562,
  response:
   '250 Accepted [STATUS=new MSGID=XRVjPdwpOWUvgMwHXRVjQcwUe2Vy-jP3AAAAAXNi-JPSS9eOph-N0f1.Anw]',
  envelope: { from: '[email protected]', to: [ '[email protected]' ] },
  messageId: '<[email protected]>' }


Solution 1:[1]

it probably is sending, but i see you're using a gmail address. Make sure you set your gmail account to be able to send these: https://codeburst.io/sending-an-email-using-nodemailer-gmail-7cfa0712a799

"Before sending your email using gmail you have to allow non secure apps to access gmail you can do this by going to your gmail settings"

you can learn more about that here: https://myaccount.google.com/lesssecureapps?pli=1

if you use AWS, i would recommend using SES. it's pretty easy if you're already setup, plus you don't have to worry about your emails going to spam or anything with your AWS verified email address :)

Solution 2:[2]

Maybe a bit late, but according to the ethereal website (https://ethereal.email/), messages are never sent. I wasted a quite some time with this.

Ethereal is a fake SMTP service, mostly aimed at Nodemailer users (but not limited to). It's a completely free anti-transactional email service where messages never get delivered.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Julian
Solution 2 gheinzer