通过nodemailer发送邮件给多个收件人

我正在尝试将电子邮件发送给多个收件人。 为此,我创build了一个收件人数组,但是使用我的代码,我只能将邮件发送到数组的最后一个电子邮件ID三次。 我的代码有什么问题?

var nodemailer = require("nodemailer"); var smtpTransport = nodemailer.createTransport( "SMTP",{ host: '', // secureConnection: true, // use SSL port: 25 }); var maillist = [ '****.sharma3@****.com', '****.bussa@****.com', '****.gawri@****.com', ]; 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 } maillist.forEach(function (to, i , array) { msg.to = to; smtpTransport.sendMail(msg, function (err) { if (err) { console.log('Sending to ' + to + ' failed: ' + err); return; } else { console.log('Sent to ' + to); } if (i === maillist.length - 1) { msg.transport.close(); } }); }); 

您的问题是从asynchronous代码引用相同的msg对象。 在sendmail发送邮件之前完成foreach。

所以msg.to将是从maiilist对象的最后一个项目。

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

 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) { 

据我所知,你将能够得到这样的多个收件人

 "mail1@mail.com,mail2@mail.com,mail3@mail.com,mail4@mail.com" 

那么为什么你不这样做

 var maillist = '****.sharma3@****.com, ****.bussa@****.com, ****.gawri@****.com'; var msg = { from: "******", // sender address to: maillist, subject: "Hello ✔", // Subject line text: "Hello This is an auto generated Email for ... ✔", // plaintext body cc: "*******" // html: "<b>Hello world ✔</b>" // html body } 

我已经尝试过,它正在工作。 此外,从我的angular度来看,如果您只有一次发送所有这些信息,而不会发生任何复杂情况,您为什么还要担心“asynchronous”或发送1K次电子邮件?

无论如何,希望这个帮助,回答你的问题,或者可以帮助别人

当然,我的答案可以改善..

nodemailer(v2.4.2) 文档说:

to逗号分隔的列表或将出现在“收件人:”字段中的收件人电子邮件地址数组

所以你可以做:

 var maillist = [ '****.sharma3@****.com', '****.bussa@****.com', '****.gawri@****.com', ]; 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: "*******", to: maillist } 
 var maillist = [ '****.sharma3@****.com', '****.bussa@****.com', '****.gawri@****.com', ]; maillist.toString(); var msg = { from: "******", // sender address to: maillist, 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 } 

asynchronous执行的一个好方法是使用asynchronous模块中的每个函数: 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 }); 

您正在asynchronous发送电子邮件,因此您需要一个等待所有邮件的等待函数,直到它们被发送,因为如果不是,您的程序退出并且一些请求未被满足。 所以你必须做一些超时function来检查邮件是否被发送。