How to email multiple recipients in sendgrid v3 node.js
Solution 1
The node js mail helper allows you to send to multiple recipients by specifying the to
property as an array. Then depending on whether you want the recipients to be able to see each other's addresses, you send the message in slightly different ways:
To allow seeing, use sgMail.send(msg)
:
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: ['[email protected]', '[email protected]'],
from: '[email protected]',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
};
sgMail.send(msg);
To prevent seeing, use sgMail.sendMultiple(msg)
or sgMail.send(msg, true)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: ['[email protected]', '[email protected]'],
from: '[email protected]',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
};
sgMail.sendMultiple(msg);
Under the covers the helper uses Personalizations
which you can use for greater control:
https://sendgrid.com/docs/for-developers/sending-email/personalizations/
Solution 2
const sgMail = require('@sendgrid/mail');
module.exports.send = function () {
sgMail.setApiKey('XYZ');
const msg = {
to: ['[email protected]', '[email protected]'],
cc: ['[email protected]', '[email protected]'],
from: '[email protected]',
subject: 'Subject of mail',
html: 'html body',
text: 'text message'
};
// console.log('message in mail :: ', msg);
sgMail.send(msg).catch(console.error);
};
Trung Tran
Updated on June 09, 2022Comments
-
Trung Tran almost 2 years
Can someone help me send an email to multiple recipients in sendgrid v3 + node.js? I've noticed that when I enter several email addresses in the
to
field, only the first email address receives the email. The email addresses after the first one do not receive the email:send: function(email, callback) { var from_email = new helper.Email(email.from); var to_email = new helper.Email('[email protected],[email protected],[email protected]'); var subject = email.subject; var content = email.content var mail = new helper.Mail(from_email, subject, to_email, content); var sg = require('sendgrid')(process.env.SENDGRID_API_KEY); var request = sg.emptyRequest({ method: 'POST', path: '/v3/mail/send', body: mail.toJSON(), }); sg.API(request, function(err, res) { console.log(res); if(err) { console.log('---error sending email:---'); console.log(err); console.log(err.response.body); callback(500); } else { callback(200); } }); }
In the example above, only
[email protected]
receives the email;[email protected]
and[email protected]
do not receive the email.Can someone help?
Thanks in advance!
-
foba over 4 yearsDoes this answer your question? NodeJS Sendrgrid Issue in sending email to multiple recepients
-
-
Trung Tran over 7 yearsYes, I'm using SendGrid's helper library - so you're saying that listing everyone's email in a single string wont work? such as:
to: [email protected]; [email protected]; [email protected]
? -
jacobmovingfwd over 7 yearsCorrect. As a safety against someone accidentally sending
TO
everyone without meaning to, SendGrid doesn't build off the native To: header that way. They want you to use the Personalizations object, to that your intentions of recipient cross-visibility are clear.