You can personalize an email by adding a value from your database. For example, the first name or a salutation. You can apply any field you have as a database value in your campaign. If you use collections (a relational database), you can show more data in your message. Think of order information or account manager data.
Add personalization in text
Automatically add personalization
To add personalization, go to a text field and click on the person icon. Then choose the database field you wish to apply. The code will automatically be placed in the text.
Example of name-based personalization:
You type the word Dear and the comma as your own text. The piece between the accolades is the personalized part. Spotler MailPro knows from the database what value to show for a contact. See the examples below for more personalization options.
Write your own personalization
Instead of automatically generating the code for the database fields, you can also write it yourself. This gives you a lot more options.
For this, you use the fieldname of the database field.
Personalization based on a databasefield value looks like this:
{{ contact.fieldname }}
If you have a database field with field name "first_name", and wish to show the value of the field, it would look like this:
{{ contact.first_name }}
When the database field isn't filled out for every contact, use a fallback value to prevent empty spaces in your text. You add a fallback or default value like this:
{{ contact.first_name|default('customer') }}In this case, the personalized content will show "customer" when the first name of the contact is unknown.
You can also personalize based on date fields. Below, you find a few examples how to use a Twig filter to format/show the date with different notations:
{{ contact.event_date }} --> 2024-01-01
{{ contact.event_date | date('d-m-Y') }} --> 01-01-2024
{{ contact.event_date | date('d F Y') }} --> 01 January 2024
{{ contact.event_date | strftime('%e %B %Y') }} --> 1 januari 2024
Use the following code to only show a piece of text when the database field contains a value:
{% if contact.fieldname is not empty %}
[text]
{% endif %}How these fields are populated is shown in the personalized preview.
Make sure to test your email thoroughly before definitively sending it, especially when using personalization.
You can find the full Twig documentation here: https://twig.symfony.com/.
Personalization based on collections
You can also use collections to personalize the text based on relational data.
When using a One to One (1:1) or One to many (1:n) relation, simply select or write the collection field you wish to show, like this:
{{ collection().adresses.street }}In this example, we show a customer's full address:
The result will look like this:
When using a Many to one (n:1) relation, you can show all data from the collection, or use a filter to show specific collection items.
To show all items, select the collection field from the dropdown. Adjust the code as necessary. In the below example, the product names of all orders of a customer will be shown.
{% for key, orders in collection().orders %}
{{ orders.product_name}}
{% endfor %}
Show specific items
A contact can have multiple orders. You may not want to show every order in the email, but only orders that meet certain conditions. To do this, use a filter in combination with collectionSearch().
Please note: a filter used for a sendout, conditional content block or automation flow determines whether a contact meets the conditions. That filter is not automatically applied to the collection data displayed in the email. To show only specific orders in the email, you must also use the filter in the personalization code.
In this example, a customer has multiple orders. We only want to show orders containing furniture:
- Create a filter containing the conditions that the orders must meet.
- Use the gear icon to display the filter ID.
- Use this ID with the
collectionSearch()function in the email.
In this example, the filter “Order with furniture” has the ID 406891:
{% for key, collection in collectionSearch(406891) %}
{{ collection.orders.product_name }}
{% endfor %}collectionSearch() searches all collection items belonging to the current contact and returns the items that match the selected filter. As a result, only the product names from orders containing furniture are displayed.
Keep the number of matching collection items in mind:
- If one order matches the filter, one product name is displayed.
- If multiple orders match the filter, the loop displays all corresponding product names.
- If no orders match the filter, nothing is displayed.
The result will look something like this:
If you always need to display exactly one collection item, make sure the filter can return no more than one item per contact or apply an extra limit parameter as explained below.
Optional parameters of collectionSearch()
The full function call uses positional parameters:
collectionSearch(filterId, limit, orderColumn, orderDirection)
-
filterId(required): the ID of the filter used to select matching collection records. -
limit(optional): the maximum number of records returned. The default value is25. -
orderColumn(optional): the field on the matching collection record by which the results should be sorted. The default value isid. -
orderDirection(optional):ascfor ascending order ordescfor descending order. The default value isasc.
Enter the optional parameters in this exact order.
For example, the following code is intended to sort matching orders by the order_date field in ascending order and then return the first record:
{% for key, collection in collectionSearch(406882, 1, 'order_date', 'asc') %}
{{ collection.orders.product_name }}
{% endfor %}The intended processing order is: apply the filter, sort the matching collection records, and then apply the limit.
Conditional content and conditional sendouts
In addition to applying personalization fields in your message, it is also possible to personalize using filters. We call this conditional content. Using this, you can only show a block of content or only send the email to the relevant audience. Read more about this in the article: How to personalize emails based on data in my database.