Learn how to email multiple recipients with SendGrid. Our Server Management Support team is here to help you with your questions and concerns.
Email Multiple Recipients with SendGrid | Guide
If you are looking for a way to send bulk emails with SendGrid, you have come to the right place. Today, our experts are going to demonstrate how to get this done via different methods.
Send Multiple Emails with sendMultiple
This method involves sending bulk emails by adding via an array of addresses in the to field and then calling sendMultiple with a single message object.
For instance, the following code can be copied into index.js. Then we can run node index.js from the terminal.
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: ['john@bobcares.com', 'jane@bobcaresmail.com'],
from: 'David Builler <david@thequint.com>',
subject: 'Test mail',
text:’This is a test mail.’
};
sgMail.sendMultiple(msg).then(() => {
console.log('emails sent successfully!');
}).catch(error => {
console.log(error);
});
The console will let us know if the script ran successfully.
Our experts would like to point out that the email recipients won’t be able to see one another’s email addresses. We can change that by replacing sgMail.sendMultiple with sgMail.send in the code.
Send Multiple Emails with Personalization
Personalizations can be described as an array of objects. It allows us to define elements like:
- “to”, “cc”, “bcc”
- “from”
- “subject”
- “headers”
- “substitutions”
Our experts would like to point out that we have to include at least one object in the personalizations array.
Send Multiple Emails with Array of Message Objects
An array of message objects comes in handy when we want to send multiple different emails with various subject lines, text, etc to different recipients.
Furthermore, the .send method accepts an array of email message objects. Unlike using an array of addresses in the ‘to’ field, the code below will not cc recipients.
For instance:
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const messages = [
{
to: 'bob@mail.com', // replace this with your email address
from: 'David Muiller <david@thequint.com>',
subject: 'Test Mail’,
text: 'This is a test mail’.
},
{
to: 'john@mail.com', // replace this with your email address
from: 'Paul Simon <paul@thequint.com>',
subject: ' Test mail 2',
text: ' This is a test mail with different content.',
},
];
sgMail.send(messages).then(() => {
console.log('emails sent successfully!');
}).catch(error => {
console.log(error);
});
Once we run the above code, we will be able to see two different emails sent to different recipients.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
To conclude, our Support Techs demonstrated how to send emails to multiple recipients via SendGrid.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments