Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

node.js - Nodemailer email confirmation using Async/Await

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;

This is the route where I call it:

//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
             })
  })

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
   });
 })  

}

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);
    }
   });
   }
   })

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...