sendgrid-nodejs icon indicating copy to clipboard operation
sendgrid-nodejs copied to clipboard

Substitution is not working

Open sydneyhenrard opened this issue 9 years ago • 17 comments

Issue Summary

I migrated from sendgrid 2.0.0 to @sendgrid/mail 6.1.2, the substitutions are not working anymore

Steps to Reproduce

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)
}

Technical details:

  • sendgrid-nodejs Version: master (latest commit: [commit number])
  • Node.js Version: 8.4.0

sydneyhenrard avatar Sep 07 '17 14:09 sydneyhenrard

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

thinkingserious avatar Sep 07 '17 16:09 thinkingserious

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

sydneyhenrard avatar Sep 07 '17 16:09 sydneyhenrard

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 avatar Sep 08 '17 19:09 thinkingserious

@thinkingserious is this on your end or an issue with the Node SDK?

adamreisnz avatar Sep 11 '17 02:09 adamreisnz

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.

thinkingserious avatar Sep 11 '17 03:09 thinkingserious

If I use camelCase variable, I have no problem with the substitutions even for substitution 0

sydneyhenrard avatar Sep 11 '17 05:09 sydneyhenrard

Thanks @sydneyhenrard,

That should help get to the root cause quicker :)

thinkingserious avatar Sep 11 '17 21:09 thinkingserious

I think I may know what the problem is, let me investigate.

adamreisnz avatar Sep 11 '17 22:09 adamreisnz

#442 should fix this 👍

adamreisnz avatar Sep 11 '17 22:09 adamreisnz

Merged as v6.1.3, thanks @adamreisnz!

thinkingserious avatar Sep 12 '17 04:09 thinkingserious

@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

thinkingserious avatar Sep 12 '17 04:09 thinkingserious

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.

greglbyv avatar Jan 11 '18 01:01 greglbyv

Hi @greglbyv,

What version of the SDK are you using? Thanks!

With Best Regards,

Elmer

thinkingserious avatar Jan 11 '18 18:01 thinkingserious

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.

enigram avatar Jun 01 '18 13:06 enigram

@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 avatar Jun 05 '18 01:06 thinkingserious

@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.

enigram avatar Jun 05 '18 13:06 enigram

@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
          }
        }

jay8t6 avatar Mar 25 '19 14:03 jay8t6