Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email to multiple recipients via nodemailer

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(); }   }); }); 
like image 758
Atul Sharma Avatar asked Feb 15 '15 15:02

Atul Sharma


People also ask

How many emails can be sent using Nodemailer?

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.

How do I send to multiple email addresses in SMTP?

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.

How do I send an email to multiple recipients in Sendgrid?

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.


2 Answers

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 } 
like image 56
Philipp Kyeck Avatar answered Sep 19 '22 18:09

Philipp Kyeck


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..

like image 23
ackuser Avatar answered Sep 20 '22 18:09

ackuser