[英]Nodemailer throws "No recipients defined" error but email is actually sent
I am experiencing an unexpected behaviour from nodemailer:我遇到了来自 nodemailer 的意外行为:
here is my code:这是我的代码:
let transporter = nodemailer.createTransport({
host: 'outlook.office365.com',
auth: {
user: process.env.MAIL_USER, // generated ethereal user
pass: process.env.MAIL_PASS, // generated ethereal password
},
tls: {
rejectUnauthorized: false
}});
let info = transporter.sendMail({
from: '"Mark 👻"mymail@outlook.com',
to: "anothermailOfMine@gmail.com",
subject: "Hello ✔",
text: "Hello world?",
html: `<b>
<a href='http://localhost:3000/activate_user/${result._id}'>
<button>Activate
profile</button></a>
</b>`, // html body
});
transporter.sendMail(info, function (err, info) {
if (err) {
console.log(err);
res.json(err);
} else {
console.log(' mail sent');
res.json({success:true, message:''});
}})};
The sendMail method returns an error even if the mail is actually sent successfully, which means that the front-end never receives a positive response about the sending, that's the error i just mentioned:即使邮件实际发送成功,sendMail 方法也会返回错误,这意味着前端永远不会收到关于发送的肯定响应,这就是我刚才提到的错误:
Error: No recipients defined
at SMTPConnection._formatError
I wonder if I am doing something wrong.我想知道我是否做错了什么。
I did find out what the problem was eventually: the send mail method needed to be called in an asynchronous function.我最终确实找到了问题所在:需要在异步 function 中调用发送邮件方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.