gpt4 book ai didi

node.js - Expressjs 中的 Nodemailer 出错?

转载 作者:太空宇宙 更新时间:2023-11-04 00:26:13 26 4
gpt4 key购买 nike

我正在使用 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 封,因此您应该考虑使用其他提供商。

关于node.js - Expressjs 中的 Nodemailer 出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42674022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com