问题链接https://github.com/koajs/koa/issues/433 //smtp.js
var cSmtp = require('../server/config').smtp,
nodemailer = require("nodemailer"),
smtpTransport = require('nodemailer-smtp-transport');
module.exports = function (mailOptions) {
cSmtp.secure = true;
cSmtp.debug = true;
var transporter = nodemailer.createTransport(smtpTransport(cSmtp));
mailOptions && (mailOptions.sender = cSmtp.host);
mailOptions && (mailOptions.subject = '[通知]Docs.ren提醒邮件');
var smtp = new Promise(function (resolve, reject) {
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
reject(err);
} else {
resolve(info);
}
});
});
return smtp;
}
//Calling
//failure prompted 500????
//当用yield ,在reject的时候会报500错误,如何解决
var sendMail = yield smtpMail({to: email, html: yield ctx.render('smtp', content)});
console.log(sendMail)
//notes block, this.body not executed
//当用这种方法时,this.body 不会执行,直接404
//smtpMail({
// to: email,
// html: yield ctx.render('smtp', content)
//}).then(function(){
// console.log(1)
// ctx.session.pwdKey = content.key;
// ctx.session.sendemail = content.email;
// ctx.body = {
// msg:'邮件发送成功,请尽快查收,更改密码',
// status:1
// }
// return;
//},function(){
// console.log(2)
// ctx.body = {
// msg:'邮件发送失败,检查网络并重试',
// status:0
// }
//});
1 回复
没人遇到过吗? 只能先这样处理了,nodemailer 不支持 Nodemailer does not include out of the box support for promises or generators.
try{
yield smtpMail({to: email, html: yield ctx.render('smtp', content)});
ctx.session.pwdKey = content.key;
ctx.session.sendemail = content.email;
ctx.body = {
msg:'邮件发送成功,请尽快查收,更改密码',
status:1
}
}catch(e){
ctx.body = {
msg:'邮件发送失败,检查网络并重试',
status:0
}
}