Add Dynamic Template Support
Issue Summary
On 7/24/2018, our team publicly launched dynamic content for transactional templates. It is now available for all customers sending over v3 of our Mail Send API. Iterate over lists, handle conditionals and more, thanks to native support for a subset of Handlebars syntax!
More information can be found in our blog post announcement.
You can currently use this feature by manually creating the request body as shown here.
Now, we need to create helper code (this is complete) and examples for this SDK.
Acceptance Criteria
- [COMPLETE] Implement a helper similar to what we have for the legacy templates
- Update the USE_CASES.md example to demonstrate the new Dynamic Templates using the helper and re-name the current example to Legacy
Documentation
Please update your documentation- I just spent an hour trying to figure out why substitutions were not working with the v3 API.
My apologies for the poor experience @jharris-code.
I've added your vote to this issue to help it gain priority. I think it will be updated soon because we sone have PR #711.
It is very silly that the absense of documentation blocks the release of the actual code.
Hi @catamphetamine,
The code was released in v6.3.1. I hope that helps, thanks!
With Best Regards,
Elmer
@thinkingserious Oh, cool, actually it didn't cross my mind to update the version of the library. I'll try that out, thx.
Note to users: when using templates pass dynamic_template_data instead of substitutions.
Now you have to change "substitutions:" to "dynamic_template_data:"
And the templates uses handlebars no more need to specify "substitutionWrappers"
This example in your use cases still uses substitutions rather than dynamic_template_data. Plz update, it definitely took me a couple hours of playing with the SDK and searching before I found this thread. (Also your api docs don't mention anything about this, which didn't help either.
Also, substitutionWrappers doesn't seem to work with dynamic_template_data at all. Regardless of including the key value pair substitutionWrappers: ['*|', '|*'] in my message object, only template vars wrapped in curly braces were filled in. (Are you guys now intentionally forcing everybody to use handlebar syntax for templates?)
My apologies @josh-yonomi,
I've updated the documentation based on your feedback.
For our new templates, they use handlebars syntax. The legacy templates still work as before.
With Best Regards,
Elmer
I receive the email but the substitutions does not work. What can cause the problem?
const msg = {
to: email,
from: sendGridMail,
templateId: emailTemplate.confirmationEmail,
dynamic_template_data: {
firstName: firstName,
lastName: lastName,
link: link
}
};
EDIT via @thinkingserious
Do you mind letting us know where the problem is? We'd love to understand your frustrations so that we can improve.
@drav96,
Do you mind sharing what your template looks like?
With Best Regards,
Elmer
why do you strip special chars in substitutionWrappers ??
dynamic_template_data: {
'foo-bar': 'wtf',
'bar_baz': 'wtf',
'baz.bro': 'wtf',
'foo': 'wtf'
}
only {{foo}} return wtf string in emails.
I'm not sure @larafale, but that certainly does not seem reasonable. Looking at the source code for this SDK, I don't see where those keys are getting modified.
Do you mind creating a separate issue for this issue and include what your HTML template looks like? I'll mark the new issue as a bug and try to reproduce and fix if necessary.
Hello,
So, I am trying to send a dynamic template email, however, I am not been able to use substitutions or dynamic_template_data.
My template has tags such as {{fullname}} or {{date}} and these properties are been sent to the send function:
const msg = {
to,
from,
templateId: template.id,
dynamic_template_data: substitutions,
};
return sgMail.send(msg)
I confirmed that the substitutions object has the right properties with the right values but the substitution doesn't work.
Any idea what I am doing wrong?
Thanks.
@gianfelipe93 The structure is correct. I had the same problem. My solution was to uninstall the @sendgrid package from my project and install it again Let me know if it works for you
const msg= {
to: email,
from: sendGridMail,
templateId: emailTemplate.requestDemoEmail,
dynamic_template_data: {
name: data.name,
email: data.email,
}
};
@drav96 thanks mate, it's working now
Adding what I hope will be some definitive documentation after wasting another hour of my time on this. (First, thanks to everyone who wasted hours of their time before me.)
- If your template id starts with
d-, thensubstitutionswon't work, and you should use CAMEL-CASEdynamicTemplateData(see here, where they convert snake_case keys to camelCase anyway) - If your template starts with
d-, thensetSubstitutionWrappersis silently ignored, and you must use{{and}}in your templates
Adding what I hope will be some definitive documentation after wasting another hour of my time on this. (First, thanks to everyone who wasted hours of their time before me.)
- If your template id starts with
d-, thensubstitutionswon't work, and you should use CAMEL-CASEdynamicTemplateData(see here, where they convert snake_case keys to camelCase anyway)- If your template starts with
d-, thensetSubstitutionWrappersis silently ignored, and you must use{{and}}in your templates
In my case it works with dynamic_template_data even if has in template id the letter d-
Yes, it does work with snake case keys, but it appears as though the devs have committed internally to camel case (see the line I linked to in my report above). Thus, I would recommend all new code use camel case.