使用Async/Await的Nodemailer电子邮件确认 [英] Nodemailer email confirmation using Async/Await

查看:198
本文介绍了使用Async/Await的Nodemailer电子邮件确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nodemailer发送邮件.我需要知道是否发送了邮件,然后更新了数据库,但是邮件是在传输器中发送的(我认为它不返回诺言),这很花时间,因此即使发送了邮件,返回也始终为false

I am sending mails using nodemailer. I need to know if the mail is sent or not and then update my database but the mail is sent in the transporter(which I do not think returns promises) which takes time and hence the return is always false, even if the mail is sent.

这是我从其他路线调用的邮件发送文件

This is my mail sending file which I call from other routes

//mail_file.js
//imports

sendmail= async(req)=>{
      let transporter = nodemailer.createTransport({
           //settings
      });
      var mailOptions = {
          //mailoptions
      };
      let resp=false;
      await transporter.sendMail(mailOptions, function(error, info){
          if (error) {
              console.log("error is "+error);
              resp =false;
           } 
          else {
              console.log('Email sent: ' + info.response);
              resp=true;
           }
          });
     return resp
 }

module.exports = sendmail;

这是我称之为的路线:

//imports including the mail_file.js

//somepath.js
router.post('/somepath',(req,res,next)=>{
      sendresetmail=async()=>
            {
                parameters={
                         //some email parameters
                }
                return await sendmail(params);

            }   
       sendmail()
       .then(response=>{
            //response is always false even when the mail is sent
             })
        .catch(err=>{
           //error
             })
  })

当我记录邮件信息时,它是在somepath.js中记录sendmail函数的响应之后

When I log the info of the mail, it is after the logging of the response of the sendmail function in the somepath.js

推荐答案

transporter.sendMail不返回promise,它使用回调函数.更改您的代码

transporter.sendMail does not return a promise, it uses a callback function. change your code

  return new Promise((resolve,reject)=>{
 let transporter = nodemailer.createTransport({
    //settings
 });
var mailOptions = {
   //mailoptions
};
let resp=false;

transporter.sendMail(mailOptions, function(error, info){
    if (error) {
        console.log("error is "+error);
       resolve(false); // or use rejcet(false) but then you will have to handle errors
    } 
   else {
       console.log('Email sent: ' + info.response);
       resolve(true);
    }
   });
 })  

}

正如我之前说的,transport.sendMail()函数使用回调,这就是为什么您不能在那里使用await的原因,但是您可以围绕它编写包装函数,以便可以在需要更多可读性和可操作性的函数中使用await干净的代码.只要考虑下面的例子

as I said earlier, transport.sendMail() function uses call back that's why you can not use await there.But you can write a wrapper function around it so that you can use await in your functions where you need more readble and clean code. just consider the following example

async function wrapedSendMail(mailOptions){
    return new Promise((resolve,reject)=>{
    let transporter = nodemailer.createTransport({//settings});

 transporter.sendMail(mailOptions, function(error, info){
    if (error) {
        console.log("error is "+error);
       resolve(false); // or use rejcet(false) but then you will have to handle errors
    } 
   else {
       console.log('Email sent: ' + info.response);
       resolve(true);
    }
   });
   }
   })

现在您可以在下面的其他功能中使用这个wrapdSendMail函数

Now you can use this wrappedSendMail function in your other functions like below,

 sendmail= async(req)=>{      
  var mailOptions = {
      //mailoptions
  };
  let resp= await wrapedSendMail(mailOptions);
  // lo or process resp;
   return resp;
} 

这篇关于使用Async/Await的Nodemailer电子邮件确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆