且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过nodemailer向多个收件人发送电子邮件

更新时间:2022-06-05 07:46:58

您的问题是从异步代码引用相同的msg对象。
foreach在sendMail发送电子邮件之前完成。

Your problem is referencing the same msg object from async code. The foreach completes before the sendMail would send out the emails.

所以msg.to将成为maiilist对象中的最后一项。

So msg.to wil be the last item from the maiilist object.

尝试在maillist foreach中克隆/复制msg,或者只是将msg定义移到那里:

Try to clone/copy msg inside maillist foreach, or just move msg definition to there :

maillist.forEach(function (to, i , array) {


  var msg = {
        from: "******", // sender address
        subject: "Hello ✔", // Subject line
        text: "Hello This is an auto generated Email for testing  from node please ignore it  ✔", // plaintext body
        cc: "*******"    
        //  html: "<b>Hello world ✔</b>" // html body
    }
  msg.to = to;

  smtpTransport.sendMail(msg, function (err) {