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

dynamic_template_data is not being passed to email template

Open sebscholl opened this issue 6 years ago • 6 comments

Issue Summary

When using the @sendgrid/mail client for Node, I'm unable to pass dynamic_template_data to my email templates. Additionally, I'm unable to find documentation around how to successfully accomplish this.

Code Snippet

  sgMail.setApiKey(SENDGRID_API_KEY);

  await sgMail.send({
    to: "[email protected]",
    from: 'my@email',
    templateId: "EMAIL_TEMPLATE_ID",
    dynamic_template_data: {
      variable1,
      variable2
    },
  });

Technical details:

  • sendgrid-nodejs version: ^6.5.5
  • node version: 10.16.0

sebscholl avatar Apr 15 '20 22:04 sebscholl

Hi @sebscholl, I wasn't able to recreate the issue. I followed the basic example outlined in the use case guide and was able to successfully send an email. Looking at your code snippet, it's possible the issue is with the way you're passing template data. You should be passing in key-value pairs for your variables.

eshanholtz avatar Apr 17 '20 22:04 eshanholtz

@sebscholl would you mind posting a snippet of your handlebars code? I had a similar issue and it turned out that I was iterating through an array that wasn't present in the JSON payload so it broke the whole template.

Example:

{{#if items.length}}
  <ul>
    {{#each items as |thing|}}
      <li>{{thing.title}}</li>
    {{/each}}
  </ul>
{{/if}}

items was undefined so obviously it didn't have .length as a property and the whole email went kaput.

millerm avatar Apr 20 '20 14:04 millerm

Hey there!

Here is an example of my template:

You've been assigned to {{ticketType}} #{{ticketNumber}} on the {{projectName}} project.

You can see the latest progress here:

https://app.projectz.com/{{projectSlug}}/{{ticketType}}s/{{ticketId}}

@eshanholtz - the syntax I'm using creates a key/value object using the variable name and value. For example:

let variable1 = "value"

const myObj = { variable1 }

console.log(myObj)

=> { variable1: "value" }

sebscholl avatar Apr 20 '20 15:04 sebscholl

Hey @sebscholl

I still have not been able to recreate this issue with sendgrid-node v6.5.5 or latest. Here is the exact code I used:

template:

<html>
<head>
</head>
<body>
You've been assigned to {{ticketType}} #{{ticketNumber}} on the {{projectName}} project.

You can see the latest progress here:

https://app.projectz.com/{{projectSlug}}/{{ticketType}}s/{{ticketId}}
</body>
</html>

node:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const projectSlug = 'foo';
const projectName = 'Project Foo';
const ticketNumber = 1234;
const ticketId = 'ID' + String(ticketNumber);

const msg = {
  personalizations: [{
    to: [{
      email: '[email protected]',
      name: 'myself',
    }],
  }],
  from: {
    email: '[email protected]',
    name: 'myself',
  },
  templateId: 'd-0123456789abcdef0123456789abcdef',
  dynamic_template_data: {
    ticketNumber,
    ticketId,
    projectSlug,
    projectName,
    ticketType: 'bar',
  },
};
sgMail.send(msg).then(([response, body]) => {
  console.log(response.statusCode);
  console.log(body);
}).catch(err => console.log(err));

And I received an email with this body:

You've been assigned to bar #1234 on the Project Foo project. You can see the latest progress here: https://app.projectz.com/foo/bars/ID1234  

Are you getting a 200 OK response and actually receiving the email, just without handlebar variables replaced? If not, do you see your emails in your recent activity?

eshanholtz avatar Apr 22 '20 19:04 eshanholtz

Thank you!

sebscholl avatar Apr 23 '20 20:04 sebscholl

Hey @sebscholl I'm going to go ahead and close this issue since it sounds like your problem was resolved. Feel free to reopen if you need more help

eshanholtz avatar Apr 28 '20 16:04 eshanholtz