Nodemailer说消息已发送并被接受,但是电子邮件从未发送过//进程从未完成

时间:2019-06-28 00:51:27

标签: nodemailer forgot-password

我正在测试Nodemailer将测试电子邮件发送到我的个人电子邮件地址的能力。我使用了Nodemailer网站上的一些示例代码来使事情变得简单,并引入尽可能少的变量。但是,我有两个主要问题:

1)尽管响应表明该邮件已被我的250状态代码的电子邮件接受,但我在收件箱或垃圾邮件中找不到该邮件。

2)尽管我的代码中有return语句,但该函数从未完成-也就是说,我的邮递员中正在进行“加载”,并且在终端中,它从未使我返回到下一行。

我的nodemailer.js文件(使用nodemailer建议的测试帐户):

"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" <foo@gmail.com>', // sender address
      to: "myemail@gmail.com", // 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: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>

    // 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);
  }

}

调用此函数的路由:

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'
    });
  }
};

生成的测试帐户:

{ user: 'ffjxqtgqtjcyvnro@ethereal.email',
  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' }

响应:

{ accepted: [ 'myemail@gmail.com' ],
  rejected: [],
  envelopeTime: 673,
  messageTime: 1157,
  messageSize: 562,
  response:
   '250 Accepted [STATUS=new MSGID=XRVjPdwpOWUvgMwHXRVjQcwUe2Vy-jP3AAAAAXNi-JPSS9eOph-N0f1.Anw]',
  envelope: { from: 'myemail@gmail.com', to: [ 'myemail@gmail.com' ] },
  messageId: '<ed8b9a0c-e04c-5a5b-62f9-c2ebea5b5f59@gmail.com>' }

1 个答案:

答案 0 :(得分:0)

它可能正在发送,但我看到您使用的是Gmail地址。确保将您的gmail帐户设置为能够发送以下信息:https://codeburst.io/sending-an-email-using-nodemailer-gmail-7cfa0712a799

“在使用gmail发送电子邮件之前,您必须允许不安全的应用访问gmail,您可以通过进入gmail设置来做到这一点”

您可以在此处了解更多信息:https://myaccount.google.com/lesssecureapps?pli=1

如果您使用AWS,我建议您使用SES。如果您已经进行了设置,那将非常容易,而且您不必担心电子邮件将成为垃圾邮件或带有经过AWS验证的电子邮件地址的任何内容:)