-
Notifications
You must be signed in to change notification settings - Fork 778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sendgrid v3 Nodejs substitutions in templates not working #680
Comments
It seems like you're missing the substitution wrappers. I just used the example that they used in this use case with a simple template, and it works.
My template consists only of this snippet of code:
Apparently, the Maybe you can try setting your wrappers to something like Try with something like this:
|
Fantastic. It worked. Thanks a ton. Substitution Wrapper is what was missing. |
Thanks for the full answer. Happy coding! |
Perhaps we should call |
@thinkingserious We do precisely that though; see https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/mail/src/classes/mail-service.js#L21 |
Maybe we try to detect a few common wrappers, such as % or _? |
That will be awesome. When the user through Sendgrid portal creates/designs the template the portal by default inserts substitution %body% and %name%. So naturally the user will think not to change the % character and will continue with the template as he is not fully aware of the functionality. If Sendgrid can suggest that % can be changed to any character or the feature prefers {{}} then the user will use it or the template that loads default settings replaces % with {{ in the first place. It will avoid the issue I faced. |
I am not to keen on automatic detection, unless it's a very robust algorithm. The trouble with trying to make the process too smart is that it can lead to potentially hard to debug errors. What if someone uses Since Sendgrid is moving to handlebars templates, which uses I think it's just a matter of documenting this well to make it clear. With the option of specifying your own wrapper, the user should have everything they need to make it work. If anyone does have an idea for robust automatic detection I'd love to see a pseudo algorithm that can handle all scenario's and all possible wrappers well, while avoiding false positives. Thoughts? |
Agree. Documentation updates will be the best option. |
@adamreisnz @vinvantest agreed, please see #681 for progress. |
Sendgrid v3 requires to replace substitutions with dynamic_template_data something like this:
|
Very confusing that you have to use |
I tried following @goelmk most recent example. Doesnt work for me :( |
@shawnkoh Did you solve it? I also created the template with the visual designer and substitution is not working. |
Hi,
In sendgrid nodejs npm package I encountered an issue. I am able to send emails with substitutions and template but the issue is in email at client end the %name%, %company% etc. substitutions do not get replaced with the value.
Code snippet
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(sendgrid_key);
var msg =
{
"personalizations": [{
"to": [{
"email": "your_email@gmail.com",
"name": "Some Company"
}],
"substitutions": {
"%name%": "John",
"%company%": "Some Company",
"%business%": "Faster Pheny"
},
"subject": "Invoice For Some Company!"
}],
"from": {
"email": "CompanyA customerservice@companya.com",
"name": "CompanyA"
},
"reply_to": {
"email": "customerservice@companya.com",
"name": "Customer Service"
},
"subject": "Invoice For Some Company!",
"template_id": "93f052da-08d4-46f2-8f0c-f2a193bceea0"
};
sgMail
.send(msg)
.then(() => {
console.log('SendGrid :: Response after sending email successfully !');
})
.catch(error => {
console.error('Error SendGrid ::: ' + error.toString());
});
Code gets executed and receive email without substitutions for %name%, %business%, %company%
Now I tried using sengrid website https://sendgrid.com/docs/API_Reference/api_v3.html
It sends email perfect without any issues and all substitutions are made correctly.
What is wrong in the javascript npm code that I am using to set the values in sgMail.send(msg)?
Request your help.
The text was updated successfully, but these errors were encountered: