How to email multiple recipients in sendgrid v3 node.js

15,735

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);

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/

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);
};
Share:
15,735
Trung Tran
Author by

Trung Tran

Updated on June 09, 2022

Comments

  • Trung Tran
    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!

  • Trung Tran
    Trung Tran over 7 years
    Yes, 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
    jacobmovingfwd over 7 years
    Correct. 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.