-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add use case for substitutions (#1363)
- Loading branch information
1 parent
3d8e645
commit 4b0eeda
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
docs/use-cases/multiple-emails-personalizations-with-substitutions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Send Multiple Emails with Personalizations and Substitutions | ||
|
||
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). | ||
|
||
The below example shows how multiple emails, each with varying metadata and substitutions, are sent with personalizations. | ||
|
||
```js | ||
const sgMail = require('@sendgrid/mail'); | ||
|
||
sgMail.setApiKey(process.env.SENDGRID_API_KEY); | ||
|
||
const msg = { | ||
from: '[email protected]', | ||
subject: 'Ahoy!', | ||
text: 'Ahoy {{name}}!', | ||
html: '<p>Ahoy {{name}}!</p>', | ||
personalizations: [ | ||
{ | ||
to: '[email protected]', | ||
substitutions: { | ||
name: 'Jon' | ||
} | ||
}, | ||
{ | ||
to: '[email protected]', | ||
substitutions: { | ||
name: 'Jane' | ||
} | ||
}, | ||
{ | ||
to: '[email protected]', | ||
substitutions: { | ||
name: 'Jack' | ||
} | ||
} | ||
], | ||
}; | ||
|
||
sgMail.send(msg); | ||
``` | ||
|
||
The default `substitutionWrappers` are `{{` and `}}` in the node.js library, but you can change it using the `substitutionWrappers` property. | ||
|
||
You can also use the SendGrid helper classes to achieve the same outcome: | ||
|
||
```js | ||
const sgMail = require('@sendgrid/mail'); | ||
const sgHelpers = require('@sendgrid/helpers'); | ||
const Mail = sgHelpers.classes.Mail; | ||
const Personalization = sgHelpers.classes.Personalization; | ||
|
||
sgMail.setApiKey(process.env.SENDGRID_API_KEY); | ||
const mail = new Mail(); | ||
mail.setFrom('[email protected]'); | ||
mail.setSubject('Ahoy'); | ||
mail.addTextContent('Ahoy {{name}}!'); | ||
mail.addHtmlContent('<p>Ahoy {{name}}!</p>'); | ||
|
||
const personalization1 = new Personalization(); | ||
personalization1.setTo('[email protected]'); | ||
personalization1.addSubstitution('name', 'Jon'); | ||
mail.addPersonalization(personalization1); | ||
|
||
const personalization2 = new Personalization(); | ||
personalization1.setTo('[email protected]'); | ||
personalization1.addSubstitution('name', 'Jane'); | ||
mail.addPersonalization(personalization2); | ||
|
||
const personalization3 = new Personalization(); | ||
personalization1.setTo('[email protected]'); | ||
personalization1.addSubstitution('name', 'Jack'); | ||
mail.addPersonalization(personalization3); | ||
|
||
sgMail.send(mail); | ||
``` | ||
|
||
Refer to [the SendGrid documentation](https://docs.sendgrid.com/for-developers/sending-email/personalizations) for more details about personalizations. |