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!
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);
https://github.com/sendgrid/sendgrid-nodejs/blob/b3b2092b7a150ffc5d68c9bb6575810a5827f69e/docs/use-cases/single-email-multiple-recipients.md
Under the covers the helper uses Personalizations
which you can use for greater control:
https://sendgrid.com/docs/for-developers/sending-email/personalizations/
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