Kitchen Sink - an example with all settings used

May 26, 2020 ยท View on GitHub

All other options from the API definition are supported (note that some settings can be used in multiple ways, see above for full details for each setting):

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'recipient@example.org',
  cc: 'someone@example.org',
  bcc: ['me@example.org', 'you@example.org'],
  from: 'sender@example.org',
  replyTo: 'othersender@example.org',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: '<p>Hello HTML world!</p>',
  templateId: 'd-12345678901234567890123456789012',
  substitutionWrappers: ['{{', '}}'],
  dynamicTemplateData: {
    name: 'Some One',
    id: '123',
  },
  attachments: [
    {
      content: 'Some attachment content',
      filename: 'some-attachment.txt',
    },
  ],
  categories: ['Transactional', 'My category'],
  sendAt: 1500077141,
  headers: {
    'X-CustomHeader': 'Custom header value',
  },
  sections: {},
  customArgs: {
    myCustomArg: 'some string', // must be a string
  },
  batchId: 'sendgrid-batch-id',
  asm: {
    groupId: 1
  },
  ipPoolName: 'sendgrid-ip-pool-name',
  mailSettings: {
    sandboxMode: {
      enable: true,
    },
  },
  trackingSettings: {},
};
sgMail
  .send(msg)
  .then(() => console.log('Mail sent successfully'))
  .catch(error => {
    console.error(error);

    if (error.response) {
      console.error(error.response.body)
    }
  });

Caveats:

As per issue #288, please note that the customArgs field must have a string value.