2FA NOW REQUIRED: Twilio is requiring two-factor authentication (2FA) as an additional layer of protection for all paying Twilio customers. For more details, see our 2FA Setup Guide.

Create a Dynamic Email Subject Line with Mustache Templates

SendGrid email messages support dynamic subject lines to make sending messages easier and more convenient. This guide explains how to use the handlebars syntax to give your dynamic template a customizable subject line.

Dynamic templates

This guide uses dynamic templates. If you don't already have one setup, you'll need to create one from the Dynamic Templates page. To make sure the email template can be specified in your API request, you'll need to use mustache templates, and set your subject as {{{subject}}}.

Please note, when using the Code Editor to view or modify your dynamic template, you'll need to click the Settings bar on the left edge of the window to edit the subject.

Sending your email

Once you have the mustache template in your dynamic template, you can submit your email request with the text you want to replace the mustache template using the dynamic_template_data object -

"dynamic_template_data": {
"subject": "Your dynamic subject"
},

This tells us to replace {{{subject}}} in your dynamic template will with the quoted text here - "Your dynamic subject" in this example.

For full details on using Dynamic Templates with Mustache Templates, see How to send an email with Dynamic Transactional Templates (Sendgrid.com/docs).

Have more questions? Submit a request