Additional features

Advanced personalization

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

  • Advanced personalization only compiles the template if personalization data is passed via Email API
  • Advanced 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 Twigopen in new window v3.x templating engine is used as a template language.

Template language

Example of advanced 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
        ]
      }
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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 filtersopen in new window 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 %}
1
2
3
4
5
6
7

Output:

Available
1

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 filtersopen in new window 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 %}
1
2
3
4
5
6
7

Output:

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

More details about iterating over keys, getting loop info, etc. can be found hereopen in new window.

Simple personalization

A simple personalization allows you to print basic data using variables in {$var} format for each recipient when sending emails using the Email endpoint. It starts with a {$, followed by the variable name and is closed with a }. Rules for using simple personalization include:

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

Note

Variables are available in subject, html and text fields.

Template language

Example of simple personalization passed to Email API:

{
 "variables": [
    {
      "email": "test@mailersend.com",
      "substitutions": [
        {
          "var": "test",
          "value": "value"
        }
      ]
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13

Example of combination:

SyntaxOutputDetails
{$test}valueDisplays a simple variable (no array or object).
Last Updated: