Sending email to multiple recipients via nodemailer

后端 未结 8 1810
青春惊慌失措
青春惊慌失措 2020-12-25 11:19

I am trying to send email to multiple recipients. For this I have created an array of recipients, but with my code I am only able to send mail to last email ID of the array

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 11:38

    A good way to do it asynchronously would be to use the each function in the async module: https://caolan.github.io/async/docs.html#each

    var async = require("async");
    
    async.each(maillist, function(to, callback){
    
        msg.to = to;
    
        smtpTransport.sendMail(msg, function (err) {
            if (err) { 
                console.log('Sending to ' + to + ' failed: ' + err);
                callback(err);
            } else { 
                console.log('Sent to ' + to);
                callback();
            }
        });
    }, function(err){
        if(err){
            console.log("Sending to all emails failed:" + err);
        }
    
        //Do other stuff or return appropriate value here
    });
    

提交回复
热议问题