MailerSend
Email API

Email personalization

Add personalization to the subject, HTML and text fields of a template using the Emails API endpoint. Learn how to use personalization variables and their rules.

Personalization

MailerSend's Personalization feature allows you to personalize email messages by generating dynamic content for each recipient when sending emails using the Email endpoint.

  • Personalization only compiles the template if personalization data is passed via Email API
  • Personalization is available for subject, html and text fields
  • It is cached server-side, but the hash is calculated based on content so it will reset with every change

A limited version of the Twig v3.x templating engine is used as a template language.

Template language

Example of personalization passed to Email API:

{
  "personalization": [
    {
      "email": "test@mailersend.com",
      "data": {
        "var": "value",
        "boolean": true,
        "object": {
          "key" : "object-value"
        },
        "number": 2,
        "array": [
          1,
          2,
          3
        ]
      }
    }
  ]
}

Variables

Variables are used to print basic data. The variable should look like {{var}}. It starts with double curly brackets {{, followed by the variable name and is closed with double curly brackets }}.

Note

Variables are available in subject, html and text fields.

General rules:

  • Must be surrounded by double curly brackets {{var}}
  • May contain alphanumeric characters and underscores (_)
  • Must not start with a number or underscore
  • Are case sensitive, meaning that {{VAR}} is different from {{var}}

Examples of combinations:

SyntaxOutputDetails
{{var}}valueDisplay a simple variable (no array or object).
{{object.key}}object-valueDisplay the key value of an object.
{{number + number}}4A simple calculation of two simple variables (no array or object).

Conditional statement

A conditional statement (i.e. if/elseif/else) appears inside {% %} block. A conditional block always starts with the if keyword followed by the statement that is being tested, and ends with the endif keyword.

Note

Conditional statements are only available in html and text fields. Additional filters are also available in: escape, default, length, lower, upper and keys.

Example:

{% if boolean == true %}
   Available
{% elseif number > 0 %}
   Only {{number}} left!
{% else %}
   Sold-out!
{% endif %}

Output:

Available

Foreach

A collection of data (an array) can be printed by looping through all the items in this collection. A loop should appear inside the {% %} block. A loop block always starts with the for keyword, and ends with the endfor keyword.

Note

Foreach is only available in html and text fields. Additional filters are also available in: escape, length, lower, upper and keys.

Example:

{% if array|length > 0 %}
    <ul>
        {% for value in array %}
            <li>{{value}}</li>
        {% endfor %}
    </ul>
{% endif %}

Output:

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
</ul>

More details about iterating over keys, getting loop info, etc. can be found here.

On this page