Skip to content
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

substitutions don't work in transactional templates #638

Closed
mckravchyk opened this issue Dec 8, 2017 · 20 comments
Closed

substitutions don't work in transactional templates #638

mckravchyk opened this issue Dec 8, 2017 · 20 comments
Labels
difficulty: medium fix is medium in difficulty status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap

Comments

@mckravchyk
Copy link

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
@kmcurry
Copy link

kmcurry commented Dec 12, 2017

@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

@mckravchyk
Copy link
Author

It works. Thank you!

@thinkingserious
Copy link
Contributor

Thanks for jumping in to help @kmcurry!

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

@LASkuma
Copy link

LASkuma commented Jan 25, 2018

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

@thinkingserious
Copy link
Contributor

@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

@LASkuma
Copy link

LASkuma commented Jan 26, 2018

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

@thinkingserious
Copy link
Contributor

Hi @LASkuma,

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

@LASkuma
Copy link

LASkuma commented Jan 26, 2018

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.

@thinkingserious
Copy link
Contributor

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

@mckravchyk
Copy link
Author

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

@thinkingserious
Copy link
Contributor

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 thinkingserious added type: community enhancement feature request not on Twilio's roadmap status: help wanted requesting help from the community difficulty: medium fix is medium in difficulty up-for-grabs labels Mar 5, 2018
@sriharrsha
Copy link

sriharrsha commented Mar 21, 2018

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

@thinkingserious
Copy link
Contributor

Hi @sriharrsha,

Have you tried @kmcurry's suggestion?

@harrshasri
Copy link

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

@tkafka
Copy link

tkafka commented Aug 7, 2018

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.

@nunsie
Copy link

nunsie commented Aug 17, 2018

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

@crysxd
Copy link

crysxd commented Aug 18, 2018

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

@thinkingserious
Copy link
Contributor

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

@xduseko
Copy link

xduseko commented Oct 18, 2018

Hah, dynamic_template_data. I solved your little puzzle! 😄

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

@kvarela
Copy link

kvarela commented Oct 22, 2018

Thank you @xduseko !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: medium fix is medium in difficulty status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

No branches or pull requests