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(); } }); });
The default value is 100 which means that once a connection is used to send 100 messages it is removed from the pool and a new connection is created. Set maxConnections to whatever your system can handle.
If you want to use smtplib to send email to multiple recipients, use email. Message. add_header('To', eachRecipientAsString) to add them, and then when you invoke the sendmail method, use email.
The most straightforward way to send bulk emails is to have an array of addresses in the to field, and then call sendMultiple with a single message object. Copy this code into index. js and replace the emails in the to array with your email addresses. const sgMail = require('@sendgrid/mail'); sgMail.
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 }
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With