当我通过Nodemailer发送电子邮件并且具有此功能时,我需要返回一些响应:
async function dispatchEmail(email) {
const transporter = nodemailer.createTransport({
service: `${nconf.get('EMAIL_SMTP_SERVICE')}`,
auth: {
user: nconf.get('EMAIL_ACCOUNT'),
pass: nconf.get('EMAIL_PASSWORD'),
},
});
const mailOptions = {
from: `"Shagrat Team "${nconf.get('EMAIL_ACCOUNT')}`,
to: email,
subject: 'Hi from Shagrat Team !',
text: '',
html: '',
};
const success = await transporter.sendMail(mailOptions, async (error, sent) => {
if (error) {
return error;
}
return {some_response};
});
return success;
}
我需要发送{some_response}
的某个值是true,false或其他值,但是我在同一函数内部或在外部调用了“ undefined”值:
const success = await dispatchEmail(emailConsumed);
我可以返回并捕捉到什么值?因为我需要测试此功能。
答案 0 :(得分:0)
退出是返回transporter.sendmail()本身,这是一个承诺,在返回contera中,发送或错误的数据最好使用trycatch来处理其他错误。
async function dispatchEmail(email) {
const transporter = nodemailer.createTransport({
service: `${nconf.get('EMAIL_SMTP_SERVICE')}`,
auth: {
user: nconf.get('EMAIL_ACCOUNT'),
pass: nconf.get('EMAIL_PASSWORD'),
},
});
const mailOptions = {
from: `"Shagrat Team "${nconf.get('EMAIL_ACCOUNT')}`,
to: email,
subject: 'Hi from Shagrat Team !',
text: '',
html: '',
};
return transporter.sendMail(mailOptions)
}
另一种选择是使用nodemailer-promise模块。
const success = await dispatchEmail(emailConsumed);
结果:
console.log(sucess);
{ envelope:
{ from: 'example@example.com',
to: [ 'example2@example.com' ] },
messageId:'01000167a4caf346-4ca62618-468f-4595-a117-8a3560703911' }