|
| 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