11

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('emailUser1@gmail.com,emailUser2@gmail.com,emailUser3@gmail.com');
    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 emailUser1@gmail.com receives the email; emailUser2@gmail.com and emailUser3@gmail.com do not receive the email.

Can someone help?

Thanks in advance!

Trung Tran
  • 13,141
  • 42
  • 113
  • 200
  • Does this answer your question? [NodeJS Sendrgrid Issue in sending email to multiple recepients](https://stackoverflow.com/questions/17455137/nodejs-sendrgrid-issue-in-sending-email-to-multiple-recepients) – foba Dec 04 '19 at 21:44

5 Answers5

17

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: ['recipient1@example.org', 'recipient2@example.org'],
  from: 'sender@example.org',
  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: ['recipient1@example.org', 'recipient2@example.org'],
  from: 'sender@example.org',
  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/

TreeAndLeaf
  • 1,064
  • 1
  • 13
  • 15
9
const sgMail = require('@sendgrid/mail');
module.exports.send = function () {
    sgMail.setApiKey('XYZ');
    const msg = {
        to: ['abc@gmal.com', 'xyz@gmail.com'],
        cc: ['test@gmail.com', 'testing@gmail.com'],
        from: 'no-reply@mail.total.fr',
        subject: 'Subject of mail',
        html: 'html body',
        text: 'text message'
    };
    // console.log('message in mail :: ', msg);
    sgMail.send(msg).catch(console.error);
};
vimmi
  • 407
  • 1
  • 5
  • 14
1

Are you using SendGrid's Helper Library? You're going to want to leverage Personalizations.

If you want the recipients to see each other, you should name & populate each of the recipients within a single Personalization object. If you don't want them to see each other, and want them to each receive the message distinctly, you'll want to make a new Personalization object for each distinct recipient group.

Ahmet Şimşek
  • 1,391
  • 1
  • 14
  • 24
jacobmovingfwd
  • 2,185
  • 14
  • 11
  • 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: email1@gmail.com; email2@gmail.com; email3@gmail.com`? – Trung Tran Dec 05 '16 at 22:12
  • 2
    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. – jacobmovingfwd Dec 06 '16 at 23:36
0

Sendgrid API V3

Hope this helps.

https://www.npmjs.com/package/sendgrid-v3-node

Example: https://runkit.com/embed/ne9asbfj59fr

var sendgrid = require("sendgrid-v3-node")

const mailOptions = {
    sendgrid_key: 'SENDGRID_KEY',
    from_email: 'FROM_EMAIL',
    from_name: 'FROM_NAME',
    to: ['TO_EMAIL1', 'TO_EMAIL2']
};

mailOptions.subject = 'SUBJECT';
mailOptions.content = 'CONTENT';
sendgrid.send_via_sendgrid(mailOptions).then(response => {
    console.log(response);
});
404
  • 8,022
  • 2
  • 27
  • 47
ruin3936
  • 535
  • 4
  • 13
0
{
  "from": "sender@yourdomain.com",
  "template_id": "YOUR TEMPLATE ID",
  "personalizations": [
    {
      "to": [
        {
          "email": "john@example.com"
        }
      ],
      "send_at": 1600188812
    },
    {
      "to": [
        {
          "email": "jane@example.com"
        }
      ],
      "send_at": 1600275471
    }
  ]
}