Back to Sendgrid Nodejs

Customization Per Recipient

docs/use-cases/customization.md

8.1.61.3 KB
Original Source

Customization Per Recipient

To send multiple individual emails to multiple recipients with additional customization (like a different subject), use the personalizations field as per the API definition instead of to, leveraging all customization options:

js
const msg = {
  personalizations: [
    {
      to: '[email protected]',
      subject: 'Hello recipient 1',
      dynamicTemplateData: {
        name: 'Recipient 1',
        id: '123',
      },
      headers: {
        'X-Custom-Header': 'Recipient 1',
      },
      customArgs: {
        myArg: 'Recipient 1', // must be a string
      },
    },
    {
      to: '[email protected]',
      subject: 'Hello recipient 2',
      dynamicTemplateData: {
        name: 'Recipient 2',
        id: '456',
      },
      headers: {
        'X-Custom-Header': 'Recipient 2',
      },
      customArgs: {
        myArg: 'Recipient 1', // must be a string
      },
      sendAt: 1500077141,
    }
  ],
  from: '[email protected]',
  templateId: 'd-12345678901234567890123456789012',
};

If the dynamicTemplateData field is provided globally as well, these substitutions will be merged with any custom dynamicTemplateData you provide in the personalizations.