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:

Steps to Reproduce
- Clone https://github.com/panayi/test-sg/
- Copy the template body from the example and create a transactional template on SendGrid UI.
- Edit
test.jsto insert your API key, your email address, andtemplateIdyou created above. - Run
node test.js.
Technical details:
- sendgrid-nodejs Version: @sendgrid/mail@6.2.1
- Node.js Version: 8.6.0
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 3
- Comments: 43 (14 by maintainers)
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:
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, usedynamicTemplateDatain the mainmsgobject instead:Reference: https://github.com/sendgrid/sendgrid-nodejs/pull/691
According to that PR, when the
templateIdis a dynamic template (prefixed withd-), thesubstitutionsisn’t sent at all, so thedynamicTemplateDataobject 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
I should note that the code below is not working:
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:
@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_nameto{{first_name}}andemployee_idto{{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