sendgrid-nodejs: Substitutions not working

Issue Summary

Followed the example for using substitutions in transactional emails, without making any changes. The substitution tags are removed, but the text is not inserted. Here is a screenshot of the email I receive:

image

Steps to Reproduce

  1. Clone https://github.com/panayi/test-sg/
  2. Copy the template body from the example and create a transactional template on SendGrid UI.
  3. Edit test.js to insert your API key, your email address, and templateId you created above.
  4. Run node test.js.

Technical details:

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 43 (14 by maintainers)

Most upvoted comments

Hi, after reading @himanshupnt 's comment, I tested with a legacy template and it worked. Thanks @himanshupnt .

Later I found Sendgrid documentation Mail Send with Dynamic Transactional Templates, I’ve kind of got the new template (not legacy one) working. I used the same HTML template and my code was updated to:

  const sgMail = require("@sendgrid/mail");
  sgMail.setApiKey(process.env.SENDGRID_API_KEY);
  sgMail.setSubstitutionWrappers("{{", "}}"); // Configure the substitution tag wrappers globally

  const msg = {
    from: "xxx@gmail.com",
    templateId: "d-27e7d954368c46519eddc806e5cf8156",
    personalizations: [
      {
        to: [
          {
            email: "yyy@gmail.com"
          }
        ],
        dynamic_template_data: {
          name: "Someone",
          city: "Somewhere",
          subject: "Hello new world"
        }
      }
    ]
  };

  sgMail.send(msg).catch(err => console.error(err.message));

Notice that I replaced “substitutions” with “dynamic_template_data”, and placed “to” inside “personalizations” as required by the docs. And I also treated subject as one of the “dynamic_template_data” and it works. (put {{subject}} in the template subject field)

Thanks.

Hi all, spent a few hours on this issue as well.

Instead of using substitutions, use dynamicTemplateData in the main msg object instead:

const sgMail = require('@sendgrid/mail')

sgMail.setApiKey(process.env.SENDGRID_API_KEY)
sgMail.setSubstitutionWrappers('{{', '}}')

const msg = {
  to: 'recipient@example.org',
  from: 'sender@example.org',
  templateId: 'd-27e7d954368c46519eddc806e5cf8156',
  dynamicTemplateData: {
    name: 'Some One',
    city: 'Denver',
  },
}

sgMail.send(msg)

Reference: https://github.com/sendgrid/sendgrid-nodejs/pull/691

According to that PR, when the templateId is a dynamic template (prefixed with d-), the substitutions isn’t sent at all, so the dynamicTemplateData object should be used.

They’ve created an issue here to improve the add dynamic templates usage to the docs, but until then I hope this helps. 🎉

At first it didn’t work on my side either. I’ve noticed that my version was a bit out of date (I was using @sendgrid/mail@6.2.1) I’ve updated the package, change my code and now it is working:

using @sendgrid/mail@6.3.1

const result = await this.sgMail.send({
                personalizations: [{
                    to: this._to
                }],
                dynamicTemplateData: {
                    test: 'test' <== working (code in template: {{ test }})
                },
                templateId: this._templateId,
            });

I should note that the code below is not working:

const result = await this.sgMail.send({
                personalizations: [{
                    to: this._to,
                dynamicTemplateData: {
                    test: 'test' <== NOT working (code in template: {{ test }})
                }
                }],
                templateId: this._templateId,
            });

Hi @pankaja92 , I couldn’t get <%subject%> working as well. Now my way of dealing subject is treating it as one of the “dynamic_template_data”, which means I wrap it in {{ }} instead of <% %> and handle it inside “dynamic_template_data”.

I’ve found this sendgrid docs which may help. The subject param doesn’t seem working though.

the variables really need double brackets? i’ll go ahead and try it but a different post by the same person showed the substitutions working with dashes, not double brackets: screenshot 2018-06-26 14 08 12

@tjstlekr,

I think your solution is to get whoever can change the HTML template code to wrap the variables with a substitution character(s).

With Best Regards,

Elmer

Hello @tjstlekr,

In your HTML code, please change first_name to {{first_name}} and employee_id to {{employee_id}}.

Thanks!

With Best Regards,

Elmer

@roymilder , thank you so much. Your answer made my day - the substitutions for dynamic templates are very unclear. Kudos!

@thinkingserious is correct. The template variables MUST be wrapped in double brackets {{name}}. Otherwise SendGrid wont know where to drop the substitutions you’ve sent.

Sadly! I do not have access to the template code. I’ll stick with another version. Thanks anyways @thinkingserious