Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
5 contributors

Users who have contributed to this file

@thinkingserious @houssem-yahiaoui @reedsa @shriyash @kylearoberts
97 lines (81 sloc) 2.04 KB

Transactional Templates

For this example, we assume you have created a transactional template in the UI or via the API. Following is the template content we used for testing.

Email Subject:

{{ subject }}

Template Body:

<html>
<head>
    <title></title>
</head>
<body>
Hello {{ name }},
<br /><br/>
I'm glad you are trying out the template feature!
<br /><br/>
I hope you are having a great day in {{ city }} :)
<br /><br/>
</body>
</html>
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'recipient@example.org',
  from: 'sender@example.org',
  templateId: 'd-f43daeeaef504760851f727007e0b5d0',
  dynamic_template_data: {
    subject: 'Testing Templates',
    name: 'Some One',
    city: 'Denver',
  },
};
sgMail.send(msg);

There's no need to specify the substitution wrappers as it will assume that you're using Handlebars templating by default.

Prevent Escaping Characters

Per Handlebars' documentation: If you don't want Handlebars to escape a value, use the "triple-stash", {{{

If you include the characters ', " or & in a subject line replacement be sure to use three brackets.

Email Subject:

{{{ subject }}}

Template Body:

<html>
<head>
    <title></title>
</head>
<body>
Hello {{{ name }}},
<br /><br/>
I'm glad you are trying out the template feature!
<br /><br/>
<%body%>
<br /><br/>
I hope you are having a great day in {{{ city }}} :)
<br /><br/>
</body>
</html>
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'recipient@example.org',
  from: 'sender@example.org',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: '<p>Hello HTML world!</p>',
  templateId: 'd-f43daeeaef504760851f727007e0b5d0',
  dynamic_template_data: {
    subject: 'Testing Templates & Stuff',
    name: 'Some "Testing" One',
    city: '<b>Denver<b>',
  },
};
sgMail.send(msg);
You can’t perform that action at this time.