2

I'm just trying to set up a trial email for SendGrid, it's the first time I've used it so I'm sure this is simple, but I can't seem to get the placeholder data to replace.

I'm using NodeJS library like this:

sgMail.setApiKey(mailConfig.apiKey);

const msgConfig = {
  to: email,
  from: mailConfig.defaults.from,
  templateId: mailConfig.templates.registrationConfirmation,
  substitutions: {
    '--displayName--': original.displayName,
    '--companyName--': 'Hello world'
  }
};

console.log('Sending: ', msgConfig);

// now send the registration confirmation email.
return sgMail.send(msgConfig).then(() => {
  console.log('done.');
})
.catch((err) => {
  console.error(JSON.stringify(err));
});

And in the template there's a text block that I added using the visual editor:

Hello --displayName--

We would love to take this opportunity to welcome you to the store.

from

--companyName--

However when I run the test to send the email, it sends the mail okay, but doesn't substitute the placeholders.

What am I missing here?

Richard G
  • 5,243
  • 11
  • 53
  • 95
  • 1
    Check out [this question](https://stackoverflow.com/questions/51650080/variable-substitution-in-sendgrid-templates-with-nodejs-does-not-work/51653566#51653566), JohnnyMontana might be right regarding the "dynamic_template_data" tag instead of the "substitutions" tag. – emilal Sep 19 '19 at 10:45

4 Answers4

7

try changing 'substitutions' to 'dynamicTemplateData'. Looks like they changed the name in the new version.

how i figured it out: https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/mail/USE_CASES.md

JohnnyMontana
  • 84
  • 1
  • 2
5

It is not clear in Sendgrid documentation, but I think it is missing this line:

sgMail.setSubstitutionWrappers('--', '--'); // Configure the substitution tag wrappers globally

Then remove dashes in substitutions object keys

Look at this link: Sendgrid

So, your code should be:

sgMail.setApiKey(mailConfig.apiKey);

// Configure the substitution tag wrappers globally
sgMail.setSubstitutionWrappers('--', '--');

const msgConfig = {
  to: email,
  from: mailConfig.defaults.from,
  templateId: mailConfig.templates.registrationConfirmation,
  substitutions: {
    'displayName': original.displayName,
    'companyName': 'Hello world'
  }
};
...

I hope this helps you!

1

For anyone NOT using dynamic templates, use Sendgrid's helpers as described here: substitution use case

IMPORTANT: If using personalization helper, don't forget to set your setSubstitutionWrappers at the PERSONALIZATION level like so personalization.setSubstitutionWrappers(['%%', '%%']).

If not using personalization, just set it at the global helper:

import mailClient from '@sendgrid/mail';
mailClient.setSubstitutionWrappers('%%', '%%')
Abe Caymo
  • 193
  • 7
0
const msgConfig = {
  to: email,
  from: mailConfig.defaults.from,
  templateId: mailConfig.templates.registrationConfirmation,
};

msgConfig.addSubstitution('%displayName%', 'Something to display');

It seems user given variables don’t work when you have the angle brackets <% ... %> around them, these are reserved for the <%body%> and <%subject%> tags.

So now you can make your template that might look something like this - %displayName%

Pyae Sone
  • 1,574
  • 2
  • 14
  • 18