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

substitutions don't work in transactional templates

Open mckravchyk opened this issue 8 years ago • 20 comments

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

mckravchyk avatar Dec 08 '17 09:12 mckravchyk

@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

kmcurry avatar Dec 12 '17 00:12 kmcurry

It works. Thank you!

mckravchyk avatar Dec 12 '17 19:12 mckravchyk

Thanks for jumping in to help @kmcurry!

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

thinkingserious avatar Dec 14 '17 06:12 thinkingserious

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

jiaming-jx avatar Jan 25 '18 21:01 jiaming-jx

@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 avatar Jan 26 '18 03:01 thinkingserious

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

jiaming-jx avatar Jan 26 '18 04:01 jiaming-jx

Hi @LASkuma,

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

thinkingserious avatar Jan 26 '18 04:01 thinkingserious

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.

jiaming-jx avatar Jan 26 '18 20:01 jiaming-jx

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

thinkingserious avatar Jan 26 '18 22:01 thinkingserious

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

mckravchyk avatar Jan 28 '18 11:01 mckravchyk

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

thinkingserious avatar Jan 31 '18 00:01 thinkingserious

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

harshasoftware avatar Mar 21 '18 06:03 harshasoftware

Hi @sriharrsha,

Have you tried @kmcurry's suggestion?

thinkingserious avatar Mar 22 '18 20:03 thinkingserious

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

harrshasri avatar Mar 28 '18 04:03 harrshasri

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 avatar Aug 07 '18 07:08 tkafka

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

nunsie avatar Aug 17 '18 11:08 nunsie

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

crysxd avatar Aug 18 '18 17:08 crysxd

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

thinkingserious avatar Aug 21 '18 00:08 thinkingserious

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

xduseko avatar Oct 18 '18 12:10 xduseko

Thank you @xduseko !

kvarela avatar Oct 22 '18 18:10 kvarela