expressjs 中的 Nodemailer 出错?

Error with Nodemailer in expressjs?

我正在使用 Nodemailer 设置忘记密码功能。出于某种原因,我的代码挂在 smtpTrans.sendMail 部分。我是否正确设置了 Nodemailer?

更新:查看下面的完整代码,包括 async.waterfall 代码

app.post('/forgot', function(req, res, next) {
  async.waterfall([
    function(done) {
      crypto.randomBytes(20, function(err, buf) {
        var token = buf.toString('hex');
        done(err, token);
      });
    },
    function(token, done) {
      User.findOne({ email: req.body.email }, function(err, user) {
        if (!user) {
          console.log('error', 'No account with that email address exists.');
          return res.redirect('/forgot');
        }
console.log('step 1')
        user.resetPasswordToken = token;
        user.resetPasswordExpires = Date.now() + 3600000; // 1 hour

        user.save(function(err) {
          done(err, token, user);
        });
      });
    },
    function(token, user, done) {
        console.log('step 2')
      var smtpTrans = nodemailer.createTransport({
         service: 'Hotmail', 
        auth: {
          user: 'myemailinfo@email.com',
          pass: '**********'
        }
      });
      var mailOptions = {

        to: user.email,
        from: 'myemailinfo@email.com',
        subject: 'Node.js Password Reset',
        text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
          'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
          'http://' + req.headers.host + '/reset/' + token + '\n\n' +
          'If you did not request this, please ignore this email and your password will remain unchanged.\n'

      };
      console.log('step 3')
   smtpTrans.sendMail(mailOptions, function(err) {
       console.log(err)
        req.flash('info', 'An e-mail has been sent to ' + user.email + ' with further instructions.');
        done(err, 'done');
      });
    }
  ], function(err) {
    if (err) return next(err);
    console.log(err)
    res.redirect('/forgot');
  });
});

app.get('/forgot', function(req, res) {
  res.render('forgot', {
    user: req.user
  });
});

nodemailer documentation 表示提供给 sendMailcallback 接受第二个参数 info。所以你可以尝试这样的事情:

smtpTrans.sendMail(mailOptions, function(err, info) {
    if (err) {
        console.log(err)
    }
    if (info) {
        console.log(info.response)
    }

    req.flash('info', 'An e-mail has been sent to ' + user.email + ' with further instructions.');
    done(err, 'done');
});

如果回调被调用,info.response 可能会填充一些来自 SMTP 邮件程序的响应,以向您说明它为何未按预期工作。

如果从未调用回调,则可能是服务没有响应并且调用没有超时。您还可以在 SMTP options

中提供自定义超时值

最后一点:Hotmail 每天通过 SMTP 发送的邮件数量上限为 100 封,因此您应该考虑使用其他提供商。