Skip to main content

How templates work

Email templates let you send flexible, custom emails to many different recipients in a single request. MailChannels currently supports Mustache templates.

Enabling templates

Two changes turn a regular send into a template send. 1. Mark the content part as a template.
The template type field must be set in each content part that contains a template. Otherwise, that content part will not be modified, and any templating within will be sent as normal text. The JavaScript SDK avoids this by applying template.type to all content parts.
2. Supply per-recipient data. Add per-recipient template data to each personalization. The keys must match the placeholders in your template:
Placeholders without a matching key in dynamic_template_data render as empty strings.

Full example

Alice receives Hello Alice and Bob receives Hello Bob.

Testing checklist

  • Render the template with typical, empty, and unusually long values.
  • Verify links and unsubscribe behavior.
  • Preview in common email clients.
  • Check message size before adding attachments.
  • Confirm Gmail does not clip long messages.
  • Send a test through a real domain with SPF, DKIM, and DMARC configured.

Mustache features

Mustache is a simple and flexible templating language that allows you to create custom messages for all your recipients. See the Mustache spec for the complete reference; features not in the supported features list at the bottom of this page are not available.

Nested fields

Use . to access nested fields. Data
Template
Result

Lists

Wrap a section in {{#list}}…{{/list}} to repeat it for each item. Inside the section, unqualified keys refer to fields of the current item. Data
Template
Result

Conditionals

The same {{#key}}…{{/key}} syntax renders the block only when key is truthy: boolean true, a non-empty list, or any non-false value. Other fields in the data can be referenced inside the block. Template
With "anniversary": true, "years": 3:
With "anniversary": false:

Negated conditionals

The inverse — {{^key}}…{{/key}} — renders only when key is falsy or missing. Template
With "verified": true:
With "verified": false:

HTML escaping

{{value}} HTML-escapes its output so injected content can’t break the layout. To insert content as raw HTML, use either triple braces or {{&value}}. Data
Template
Result
Only emit raw HTML for content you control. Substituting recipient-supplied values without escaping is a common HTML injection vector.

Comments

{{! ... }} is stripped from the rendered output. Template
Result

Supported features

The Mustache features MailChannels supports, using the names from the Mustache spec:
  • Variables
  • Dotted names
  • Implicit iterator
  • Non-empty lists
  • Non-false values
  • Inverted sections
  • Comments
  • Set delimiter