Search code examples
node.jssendgridsendgrid-api-v3

Adding Substitutions for BCC recipients using SendGrid's Node library


My use case is such that I need to send a single email with multiple BCC recipients, each with their own substitutions.

I'm attempting to use SendGrid's Node Library "Kitchen Sink" example with some static test data. I'm able to create a message with BCC recipients, however, each recipient is receiving the last set of substitution values.

Here are the relevant portions of my code:

const mail = new helper.Mail();
const personalization = new helper.Personalization();
const testData = [
  {
    email: 'recipient1@gmail.com',
    name: 'Bob Smith',
    dollars: '100',
    points_for_play: '2500',
  },
  {
    email: 'recipient2@gmail.com',
    name: 'Mary Smith',
    dollars: '50',
    points_for_play: '1000',
  },
];

testData.forEach(function(recipientData) {
  var substitutionValues = _.omit(recipientData, ['email', 'name']);
  var bccRecipient = new helper.Email(recipientData.email, recipientData.name);

  personalization.addBcc(bccRecipient);

  _.forOwn(substitutionValues, function(value, key) {
    var substitution = new helper.Substitution('%' + key + '%', value);
    personalization.addSubstitution(substitution);
  });
});

mail.addPersonalization(personalization);

The result is that each bcc recipient will receive the last substitution values:

%dollars% = 50
%points_for_play% = 1000

I've also tried creating a new Personalization for each BCC recipient, but that results in an error stating the personalization instance needs a "to" array, which doesn't seem correct... where am I going wrong?


Solution

  • BCC substitutions aren't supported yet in the v3 API as of 7/2017, but they are in the SMTP API according to SendGrid support.