I migrated from sendgrid 2.0.0
to @sendgrid/mail 6.1.2
, the substitutions are not working anymore
SendgridService.prototype.sendTestEmail = function () {
var text = 'Hi %DISPLAY_NAME%\n\n'
const msg = {
to: '[email protected]',
subject: 'Test Email',
from: {email: '[email protected]', name: 'Test'},
text: text,
substitutions: {
"%DISPLAY_NAME%": 'Test'
}
}
return sendgrid.send(msg)
}
Hello @sydneyhenrard,
Here is a complete example.
It appears that you need to set the substitutionWrappers
. In your case that would be %
. Then in the substitutions
object, you would leave the %
characters out.
With Best Regards,
Elmer
Is there a convention on variable?
SendgridService.prototype.sendTestEmail = function () {
var text = 'Hi %DISPLAY_NAME% - %displayname%\n\n'
const msg = {
to: '[email protected]',
subject: 'Test Email',
from: {email: '[email protected]', name: 'Test'},
text: text,
substitutionWrappers: ['%', '%'],
substitutions: {
DISPLAY_NAME: 'Test',
displayname: 'Test'
}
}
return sendgrid.send(msg)
}
The received email:
Hi %DISPLAY_NAME% – Test
Hello @sydneyhenrard,
I was able to reproduce this issue. I have added this to our backlog for a fix. It looks like given n substitution, substitution 0 is not getting set properly.
With Best Regards,
Elmer
@thinkingserious is this on your end or an issue with the Node SDK?
It's an issue with the SDK. I'm not quite sure exactly what the issue is though. I plan to examine the payload to help troubleshoot.
If I use camelCase variable, I have no problem with the substitutions even for substitution 0
Thanks @sydneyhenrard,
That should help get to the root cause quicker :)
I think I may know what the problem is, let me investigate.
Merged as v6.1.3, thanks @adamreisnz!
@sydneyhenrard,
For helping us solve this issue, we would like to send you some swag as a token of our gratitude.
Thanks for supporting SendGrid!
With Best Regards,
Elmer
had the same problem in 01/2018. changed our substitution variables from %companyname% to %company_name% because single word variables were not working. fixed immediately.
Hi @greglbyv,
What version of the SDK are you using? Thanks!
With Best Regards,
Elmer
Are substitutions supposed to work when using design built template? I can get them working only when I build the template by hand, the ones built with the design tool never work.
@airstofather,
To help you out I will need further detail. 1) Which template are you referring to? 2) Could you provide sample code? Both the template HTML and the Node.js code. Thank you!
Also, it might be best to open a separate issue. Thanks!
With Best Regards,
Elmer
@thinkingserious
Please ignore my previous comment. After your reply I went back to look at it with fresh eyes and it was an extra space in the interpolated variable that was causing the issue which I didn't catch the first time.
All is working as advertised. Thank you.
@thinkingserious I have tried everything, camelcase, underscore. The variable still would not populate, when I do the testing via the sendgrid UI and pass in the variable, it works fine. It just wouldnt work through firebase cloud functions. Here's my code
I am using "@sendgrid/mail": "^6.3.1"
const msgbody = {
to: userData.email,
from: '[email protected]',
subject: 'Registration Complete',
templateId: 'd-dffc1e7c8a4a4ed5a4384eded1701f65',
substitutionWrappers: ['{{', '}}'],
substitutions: {
first_name: userData.firstName
}
}
Most helpful comment
@sydneyhenrard,
For helping us solve this issue, we would like to send you some swag as a token of our gratitude.
Thanks for supporting SendGrid!
With Best Regards,
Elmer