Sendgrid-nodejs: substitutions don't work in transactional templates

Created on 8 Dec 2017  ·  20Comments  ·  Source: sendgrid/sendgrid-nodejs

screenshot from 2017-12-08 10-36-46

Issue Summary

Substitutions don't work when using this library. It works when using curl.

Steps to Reproduce

  1. Create a transactional email template in your SendGrid account
  2. Have either <%body%> in the template, %firstName% or both (I tried both)
  3. Execute this node code (fill in the config)
`'use strict';

var config = {

    apiKey: '',
    from: '',
    to: '',
    templateId: ''

}

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(config.apiKey);

async function sendTest() {

 var newMsg = {

   "from": {
    "name": "Test Script",
    "email": config.from
   },
   "personalizations": [{
    "to": config.to,
    "substitutions": {
     "-firstName-": "Maciejs", 
     "%firstName%": "Maciej",
     "-first_name": "Maciej",
     "%first_name%": "Maciej"
    }
   }],
   "subject": "test subject",
   "content": [{
     "type": "text/plain",
     "value": "Hello, %firstName%!"
    },
    {
     "type": "text/html",
     "value": "<html><p>Hello, %firstName%!</html> "
    }],
    "template_id": config.templateId
 }

   await sgMail.send(newMsg);

}

sendTest();`
  1. Notice that nothing will be replaced in the delivered email. Again, it works perfectly with curl.

I'm also attaching a screenshot of the template in SendGrid editor and the email I get
screenshot from 2017-12-08 10-36-26

Technical details:

  • sendgrid-nodejs Version: 6.1.4
  • Node.js Version: 8.9.2
medium help wanted help wanted community enhancement up for grabs up-for-grabs

Most helpful comment

To everyone as desperate as me yesterday: in v3 API you need to use dynamic_template_data instead of substitutions, this isn't yet documented anywhere!

Then use {{var_name}} in email template and dynamic_template_data: { var_name: "var_value" } in API call.

All 20 comments

@MaciejKrawczyk Have you tried using setSubstitutionWrapper and omitting the wrappers from the substitutions?

Ex:
sgMail.setSubstitutionWrappers('-', '-');
"substitutions": { "firstName": "Maciejs" }

Not sure how you'd do multiple wrappers at once this way, but I was able to get subs working with this version by not specifying the wrappers in the substitution attribute. My subs look like
emailOptions.substitutions = {}
emailOptions.substitutions.CLIENT_NAME = myClientName

It works. Thank you!

Thanks for jumping in to help @kmcurry!

I'm glad you are up and running @MaciejKrawczyk!

This should be definitely put in the README! Cost me 2hrs to figure this out!

@LASkuma,

That's no good :(

Does this documentation help? If so, could you point me where you first looked for help so I can make sure we have a clear link there?

Thanks!

With Best Regards,

Elmer

@thinkingserious Yes. This doc did help a lot. However, since I came from the official website, where the substitution keys should include the wrapper, I didn't pay too much attention to the use cases.

Hi @LASkuma,

When you say you came from the official website, could you please let me know what URL you are referring to? Thanks!

Hi @thinkingserious,
I was referring to this page. Even the try it out section have substitution keys with the wrappers included. After reading the documentation, I thought the Node library was naturally consistent with the documentation.

Thanks @LASkuma!

That's really helpful feedback.

I'm not quite sure on the solution though. Those documents are for those who wish to interact with our API directly vs. use one of our helper libraries.

Earlier you mentioned that we should include this documentation in the README. Do you mean here? Should we specifically call out the substitution use case there?

Thanks again for your help!

With Best Regards,

Elmer

This should definitely be on the readme. I tried everything, I read all docs on the SendGrid api reference , everything that's on GitHub (I thought), contacted support and I would be making my own api wrapper if not for @kmcurry I think it would be much more easier and less confusing if the helper library would work exactly like the API itself (see my code at the beginning of the thread).

Thanks @MaciejKrawczyk,

This is very good feedback and I've added this to our team's backlog for a deeper dive. We will provide updates on this thread.

Thanks!

With Best Regards,

Elmer

Is this done? I'm having the same issue;

Hi @sriharrsha,

Have you tried @kmcurry's suggestion?

Not yet. I will update if it works or not soon.
Sorry for the late reply.

To everyone as desperate as me yesterday: in v3 API you need to use dynamic_template_data instead of substitutions, this isn't yet documented anywhere!

Then use {{var_name}} in email template and dynamic_template_data: { var_name: "var_value" } in API call.

@tkafka oh my god! thanks for this man, been pulling my hair out on this for the past 2 days.

@tkafka Thanks a lot! This is so frustrating! Took me 3hrs of my day...

Here is the entire code in case somebody is looking for the same:

var sendgrid = require('@sendgrid/mail');
sendgrid.setApiKey(env.sendgridApiKey);
sendgrid.setSubstitutionWrappers('{{', '}}');

var newMsg = {
      "from": {
       "name": "Your Company",
       "email": env.businessEmail
      },
      "personalizations": [{
       "to": order.email,
       "dynamic_template_data": {"order_id": "1234", "first_name": "Max", "last_name": "Mustermann"}
      }],
      "subject": "test subject",
      "content": [{
        "type": "text/plain",
        "value": "Hello, {{firstName}}!"
       },
       {
        "type": "text/html",
        "value": "<html><p>Hello, {{'firstName}}!</html> "
       }],
       "template_id": env.sendgridOrderConfirmationTemplateId
    };
    console.log("Sending confirmation email...")
    return sendgrid.send(newMsg);

The documentation of sendgrid is horrible to say the least.

Hello Everyone,

Please see this issue for progress. We are currently working on the update for C#, then Java, then this SDK is right after that. Thanks for your patience and my apologies for the delay.

With Best Regards,

Elmer

Hah, dynamic_template_data. I solved your little puzzle! :smile:

For other typescript users - Create file mail.d.ts:

import {MailData} from "@sendgrid/helpers/classes/mail";

// see https://github.com/sendgrid/sendgrid-nodejs/issues/638

declare module "@sendgrid/helpers/classes/mail" {
    export interface MailData {
        dynamic_template_data: {[key: string]: string};
    }
}

Thank you @xduseko !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

umarhussain15 picture umarhussain15  ·  3Comments

thidasapankaja picture thidasapankaja  ·  4Comments

prasoonjalan picture prasoonjalan  ·  3Comments

thinkingserious picture thinkingserious  ·  4Comments

wooyah picture wooyah  ·  4Comments