You can send multiple distinct emails in a single API request by using the personalizations array. Each object in the personalizations array allows you to customize metadata such as recipients (setTo), CC recipients (setCc), the sender (setFrom), and the subject line (setSubject) for that specific email.
To implement this, use the Personalization class from @sendgrid/helpers to create individual personalization objects and push them into the personalizations array of your message object.
const sgMail = require('@sendgrid/mail');
const sgHelpers = require('@sendgrid/helpers');
const Personalization = sgHelpers.classes.Personalization;
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
from: 'sender1@example.org',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
personalizations: []
};
const personalization1 = new Personalization();
personalization1.setTo(['recipient2@example.org', 'recipient3@example.org']);
personalization1.setCc('recipient4@example.org');
msg.personalizations.push(personalization1);
const personalization2 = new Personalization();
personalization2.setTo(['recipient5@example.org', 'recipient6@example.org', 'recipient7@example.org']);
personalization2.setFrom('sender2@example.org');
personalization2.setCc('recipient8@example.org');
msg.personalizations.push(personalization2);
const personalization3 = new Personalization();
personalization3.setTo('recipient9@example.org');
personalization3.setFrom('sender3@example.org');
personalization3.setCc('recipient10@example.org');
personalization3.setSubject('Greetings world');
msg.personalizations.push(personalization3);
sgMail.send(msg);