Skip to content

Commit 4b0eeda

Browse files
authored
docs: Add use case for substitutions (#1363)
1 parent 3d8e645 commit 4b0eeda

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

docs/use-cases/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This documentation provides examples for specific Twilio SendGrid v3 API use cas
55
* [Send a Single Email to Multiple Recipients](single-email-multiple-recipients.md)
66
* [Send Multiple Emails to Multiple Recipients](multiple-emails-multiple-recipients.md)
77
* [Send Multiple Emails with Personalizations](multiple-emails-personalizations.md)
8+
* [Send Multiple Emails with Personalizations and Substitutions](multiple-emails-personalizations-with-substitutions.md)
89
* [CC, BCC and Reply To](cc-bcc-reply-to.md)
910
* [Flexible Email Address Fields](flexible-address-fields.md)
1011
* [Handling Success/Failure/Errors](success-failure-errors.md)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Send Multiple Emails with Personalizations and Substitutions
2+
3+
Personalizations are an array of objects, each representing a separate email, that allow you to customize the metadata of each email sent within a request. With substitutions you can also have the email template dynamically be updated through [substitution tags](https://docs.sendgrid.com/for-developers/sending-email/substitution-tags).
4+
5+
The below example shows how multiple emails, each with varying metadata and substitutions, are sent with personalizations.
6+
7+
```js
8+
const sgMail = require('@sendgrid/mail');
9+
10+
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
11+
12+
const msg = {
13+
from: 'sender1@example.org',
14+
subject: 'Ahoy!',
15+
text: 'Ahoy {{name}}!',
16+
html: '<p>Ahoy {{name}}!</p>',
17+
personalizations: [
18+
{
19+
to: 'recipient1@example.org',
20+
substitutions: {
21+
name: 'Jon'
22+
}
23+
},
24+
{
25+
to: 'recipient2@example.org',
26+
substitutions: {
27+
name: 'Jane'
28+
}
29+
},
30+
{
31+
to: 'recipient3@example.org',
32+
substitutions: {
33+
name: 'Jack'
34+
}
35+
}
36+
],
37+
};
38+
39+
sgMail.send(msg);
40+
```
41+
42+
The default `substitutionWrappers` are `{{` and `}}` in the node.js library, but you can change it using the `substitutionWrappers` property.
43+
44+
You can also use the SendGrid helper classes to achieve the same outcome:
45+
46+
```js
47+
const sgMail = require('@sendgrid/mail');
48+
const sgHelpers = require('@sendgrid/helpers');
49+
const Mail = sgHelpers.classes.Mail;
50+
const Personalization = sgHelpers.classes.Personalization;
51+
52+
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
53+
const mail = new Mail();
54+
mail.setFrom('sender1@example.org');
55+
mail.setSubject('Ahoy');
56+
mail.addTextContent('Ahoy {{name}}!');
57+
mail.addHtmlContent('<p>Ahoy {{name}}!</p>');
58+
59+
const personalization1 = new Personalization();
60+
personalization1.setTo('recipient1@example.org');
61+
personalization1.addSubstitution('name', 'Jon');
62+
mail.addPersonalization(personalization1);
63+
64+
const personalization2 = new Personalization();
65+
personalization1.setTo('recipient2@example.org');
66+
personalization1.addSubstitution('name', 'Jane');
67+
mail.addPersonalization(personalization2);
68+
69+
const personalization3 = new Personalization();
70+
personalization1.setTo('recipient3@example.org');
71+
personalization1.addSubstitution('name', 'Jack');
72+
mail.addPersonalization(personalization3);
73+
74+
sgMail.send(mail);
75+
```
76+
77+
Refer to [the SendGrid documentation](https://docs.sendgrid.com/for-developers/sending-email/personalizations) for more details about personalizations.

0 commit comments

Comments
 (0)