Sending email to multiple recipients via nodemailer
Solution 1
Your problem is referencing the same msg object from async code. The foreach completes before the sendMail would send out the emails.
So msg.to wil be the last item from the maiilist object.
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) {
Solution 2
nodemailer (v2.4.2) docs say:
to
- Comma separated list or an array of recipients e-mail addresses that will appear on the To: field
so you can just do:
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
}
Solution 3
As far as I know you will be able to get multiple recipients like this
"[email protected],[email protected],[email protected],[email protected]"
So why you don't do something like
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
}
I have already tried and it is working. Furthermore, from my point of view, why you have to worry about "asynchronously" or sending emails 1K times if you have the capability of sending all of them only in once without any complication?
Anyway hope this help, answer your question or it may help somebody else
Surely, my answer can be improved..
Solution 4
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
}
Solution 5
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
});
Related videos on Youtube
Atul Sharma
Updated on July 09, 2022Comments
-
Atul Sharma almost 2 years
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 three times. What's wrong with my code?
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(); } }); });
-
Atul Sharma about 9 yearsyes this also be possible but , I don't know in advance the TO list of recipient , there can be any number of recipients .
-
ackuser about 9 yearsBut you are getting these recipients from somewhere else...I mean you are getting for a form or a variable I think...By the way what I think you could add some kind of pre-process on the emailist before you are sending any emails so you get all them in once...I am putting a plunkr with the idea and format of how getting the variable maillist completely format and just to put on "msg" before send the email plnkr.co/edit/giNSNHJTlrEi5nj8WmG5
-
Pardeep Jain about 6 yearsand how pass variable from js to Html file ??
-
Ben Steward over 5 yearsThese are the droids you are looking for.
-
sɐunıɔןɐqɐp about 4 yearsFrom Review: Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks
-
jtlindsey almost 4 yearsKeep in mind this method (
to
field with multiple addresses) exposes all email recipients to all receivers. Privacy issues. -
Nick Song about 3 yearsWould you consider adding some explanations for this code snippet?
-
Abhishek Kumar about 3 yearsThanks FilipVeličković I have added the explanation.
-
SalahAdDin almost 2 yearsNodemailer simply don't work if we pass an array.
-
SalahAdDin almost 2 yearsNodemailer also does not get array as input, it must be a string.