How to Send an Email with Dynamic Templates
Before you begin, complete the following tasks:
- Create a Twilio SendGrid account
- Create an API Key
- Add an unsubscribe group (optional)
If you use your own templating system, you can insert dynamic data using Substitution Tags.
Twilio SendGrid allows multiple versions of a dynamic template. Each version might offer different content on the same theme. This splits the process for creating dynamic templates into creating the dynamic template and creating a version of that dynamic template.
As a component, a dynamic templates provides a unique ID to the Mail APIs.
- Log in to the Twilio SendGrid app.
- Go the Dynamic Templates page.
- Click Create a Dynamic Template.
- Type a human-readable name into the Dynamic Template Name field.
- Click Create.
- The Dynamic Templates page displyas your template in the Template list.
- If you click the name of a dynamic template, a panel displays with the Template ID number, a list of template versions, and the Add Version button.
To begin editing your created template, complete the following steps.
- Log in to the Twilio SendGrid app.
- Go the Dynamic Templates page.
- Click the name of the template you want to design.
The details of the template display. - Click Add Version.
- Click either the Your Email Designs or the SendGrid Email Designs tab.
- Hover over the desired starting template and click Select.
- To modify the starting template, click Select for either the Design Editor or Code Editor.
- The Design Editor provides a visual interface for building email templates.
- The Code Editor provides a text editor for modifying HTML code.
- Complete the settings for this version:
- Add a human-readable label for this version in the Version Name box.
- Add a subject line for your email in the Subject box. This is optional. You can set a subject line when sending the email.
- Add an extended preview of your email in the Preheader box. This is also optional and can be set when you send your email.
- Click the Build tab.
The Design Editor provides three main features:- Add Modules: Drag and drop, then customize pre-built components into your email template.
- Global Styles: Set the broad design of your email body and content container of your email template. These settings include:
- Colors and fonts in the Email Body section, and
- Width of the email content box and background color outside that box in the Content Container section.
- Advanced: Import or export HTML and change the HTML head values.
- Design your template.
You can include personalization variables into your dynamic template. Twilio SendGrid supports the Handlebars templating language for the text, HTML, and subject lines of your template. Learn how to use Handlebars. - Include an unsubscribe module.
- Drag the Unsubscribe module into your email template.
- Change its formatting settings as needed.
- Choose which unsubscribe group should accept unsubscribes from this template in the Unsubscribe Group dropdown menu.
- Whenever you want to save progress on your template, click Save in the top navigation.
- When finished, click the left arrow icon in the top navigation.
- Click the name of the template you last saved to open the version list for that template.
- To set a specific version of the template as active, click Make Active from the three bullet icon at the right of the template.
To use sample templates, see the dynamic-template
directory in the Twilio SendGrid email-template GitHub repo. These samples include receipts, password resets, account activations, newsletters, and sale notifications.
To send mail using Dynamic Templates, use the Mail Send REST API resource. The API requests on this page use cURL with the receipt example template.
Add your dynamic content, the Handlebars variable values, to the data
payload of your API request.
- The payload accepts a JSON document.
- Add the dynamic data that your template uses into the
dynamic_template_data
object. The previous example highlights those lines for you. - Include the template ID for your dynamic template in the JSON body
data
.- The template ID starts with
d-
followed by 62 hexadecimal characters. - If you forget your template ID, find it in one of two ways:
-
Call the
templates
resource.Display the find all dynamic templates API -
Log in to Twilio SendGrid app then go to the Dynamic Templates page. Click the name of the template. The template ID displays under the template name.
-
- The template ID starts with
1curl -X "POST" "https://api.sendgrid.com/v3/mail/send" \2-H 'Authorization: Bearer <<YOUR_API_KEY>>' \3-H 'Content-Type: application/json' \4-d '{5"from":{6"email":"example@.sendgrid.net"7},8"personalizations":[9{10"to":[11{12"email":"example@sendgrid.net"13}14],15"dynamic_template_data":{16"total":"$ 239.85",17"items":[18{19"text":"New Line Sneakers",20"image":"https://marketing-image-production.s3.amazonaws.com/uploads/8dda1131320a6d978b515cc04ed479df259a458d5d45d58b6b381cae0bf9588113e80ef912f69e8c4cc1ef1a0297e8eefdb7b270064cc046b79a44e21b811802.png",21"price":"$ 79.95"22},23{24"text":"Old Line Sneakers",25"image":"https://marketing-image-production.s3.amazonaws.com/uploads/3629f54390ead663d4eb7c53702e492de63299d7c5f7239efdc693b09b9b28c82c924225dcd8dcb65732d5ca7b7b753c5f17e056405bbd4596e4e63a96ae5018.png",26"price":"$ 79.95"27},28{29"text":"Blue Line Sneakers",30"image":"https://marketing-image-production.s3.amazonaws.com/uploads/00731ed18eff0ad5da890d876c456c3124a4e44cb48196533e9b95fb2b959b7194c2dc7637b788341d1ff4f88d1dc88e23f7e3704726d313c57f350911dd2bd0.png",31"price":"$ 79.95"32}33],34"receipt":true,35"name":"Sample Name",36"address01":"1234 Fake St.",37"address02":"Apt. 123",38"city":"Place",39"state":"CO",40"zip":"80202"41}42}43],44"template_id":"[template_id]"45}'
Using the previous cURL command, you could customize your template in the following ways.
- Insert the person's name.
<div>Hello {{name}}!</div>
- Loop through the list of items ordered.
1{{#list items}}2{{image}} {{text}} {{price}}3{{/list}}
- Add a condition.
1{{#with name}}2<div>Hello {{name}}!</div>3{{/with}}
The Handlebars variables point to JSON keys in your JSON payload.