Mautic Community Forums

Dynamic list-content in email/newsletter

Hello,

is there a way to have dynamic content in an email/newsletter send with mautic?

For example:

I have a list with 20 items.

I want to send an email which contains 5 of these items per receiver, but this should be shuffled which items are displayed.

I just see kind of “programmatic” solution for this but I don’t have any idea how to solve this. Maybe with something like GitHub - Logicify/mautic-advanced-templates-bundle: Plugin extends default email template capabilities with TWIG block so you can use advanced scripting techniques like conditions, loops etc or Twig Templates for Mautic – Mautic Extendee?

But how can I “grab” the list-items for the newsletter? Set’s say these items are accessible with an rest api or an rss feed - how can I put this data to the email?

I event don’t know if dynamic content can used for this because I have a list of different items and I want do display some of them.

Hope someone can help me with this.

Best regards,

Timo

Hi, yeah I use Twig Templates for Mautic – Mautic Extendee plugin for a bunch of things. For example random content.

{{ random(['apple', 'orange', 'citrus']) }} will result in a random fruit for example.

You can also cycle through items:

{% set cart = contact.cart | json_decode %}
{% for item in cart.cart %}

<tr>
<td style="padding: 0 15px 0 0;"><a href="{{item.prod_link | raw}}"><img src="{{item.img_url | raw}}" width="100px"></a></td>
<td style="padding: 0 15px;"><a href="{{item.prod_link | raw}}">{{item.name}}</a></td>
</tr>

{% endfor %}

Hi joeyk,

thanks for your reply. Seems that with this extension I can handle the dynamic output very well.

But do you have any idea how I can get the dynamic list data into the process?

Where can I place the data? I don’t want to store the list in each contact because the list is the same for the whole segment.

For example:

I have a segment for contacts in berlin.

Then I want to grab 20 items which are relevant for this segment (for example I can access this data via rest api).

But where can I place this data to get it into the twig template?

Best regards,

Timo

Good question. As most of the things, you can do it different ways with Mautic.

Strategy 1: Use a Large Twig

You write the whole login in Twig:

  1. Create blocks based on location and cycle through the hardcoded options.
    (Btw we asked the developer, so he made it possible, that you can API in this whole twig when the content changes :slight_smile: )

  2. Content is saved as json inside the Twig template:

{% set berlinItems = [
    { "event":"Beer Drinking","activity":"Lift glass, empty content, refill","price":"200" },
    { "event":"Berlin Wall Tour","activity":"Climb on the wall, run from police","price":"300" },
... etc
] %}

The display part would look like this

// Loop for Berlin, random 5 items
{% if contact.location == 'Berlin' %}
{% for item in berlinItems|shuffle|slice(0,5) %}
{% endif %}
// Loop for München, random 5 items
{% if contact.location == 'München' %}
{% for item in munchenItems|shuffle|slice(0,5) %}
{% endif %}
// Loop for Frankfuhrt, random 5 items
{% if contact.location == 'Frankfuhrt' %}
{% for item in frankfuhrtItems|shuffle|slice(0,5) %}
{% endif %}

Of course you can have better array to store everything in 1 json, and have one loop, but I could come up only with this now.

Strategy 2: save the activity for every person

You can also API in the activities. It gives you a better chance to customize by person, but you would store the JSON in a text field (yourjsonfield). For example when they register for stuff, or when other activity happens.

Your twig would look like this:

{% set items = contact.yourjsonfield | json_decode %}
{% for item in items|shuffle|slice(0,5) %}

And 5 a random items would display from anyone’s json list.

I hope it helps.
I have a video about the Movie Newsletter, it functions a similar way.

3 Likes

Hi Joey,

thanks for your detailed answer and the great video.

Let’s explain my use case:

I want to send a daily newsletter with new job postings to each subscriber.

The subscriper are grouped into geographic cluster, so each cluster get’s jobs next to it.

Lets say we have 500 receiver in berlin an 50 new jobs in berlin. So my thought was to display random 10 of these 50 jobs for each receiver in this cluster.

When I have to code this on my own without mautic everything is clear. But we want to connect our system with mautic so it could be nice to send this daily newsletter also with mautic (but just if the efford is not much bigger than the own implementation).

These jobs change every day. So I don’t want to update all 500 contacts in mautic every day, especially the 50 jobs are the same for the whole cluster.

So maybe a solution could be that I update the items in the twig template with all the job data/list data. Important: This have to be done before the newsletter is sended.

This is your Strategy 1 with an dynamic update of the twig template using the API. I think this can work, but I’m still not really happy with this.

Another case: We also want to output individual job recommendations for each user. This data we also don’t have in the mautic contact but it should be available via api. So what should be a way to handle this? Some kind of twig filter which makes the api call?

Or maybe something based on rss feed? Just found GitHub - Logicify/mautic-advanced-templates-bundle: Plugin extends default email template capabilities with TWIG block so you can use advanced scripting techniques like conditions, loops etc and see the rss feature which looks interesting.

I’m happy to get feedback and thoughts from you!

Best regards,

Timo

Hey,

I was thinking of the RSS also. But it will generate ONE email template based on the RSS, so it’s no good for you.

Mautic Advanced Templates Bundle is M2 only, won’t work on M3.

We have a client, who is doing the same thing for another industry. The mtcextendee twig template plugin didn’t come with the API originally, so we sponsored the development, and now it is available for everyone. You can use it to update TWIG blocks easily. I hear you - you are not happy, but I think it is the best solution.

Individual recommendations can be pushed into Mautic by API, but you’ll need a bunch of API calls to do so. Maybe a mass-import would help?

Hi Joey,

thanks again for your quick reply. Your feedback is very valueable because we don’t have much experience with mautic.

I was thinking of the RSS also. But it will generate ONE email template based on the RSS, so it’s no good for you.

What do you mean with “ONE e-mail template based on the RSS”? In my case I need an rss-feed for each cluster (like the segments in the example) and then output the items based on the cluster/segment. I like this idea and this approach, but if it’s not compatible with mautic 3 (and mautic 4) its out.

I don’t know if mass import helps. Also it should be a mass update - don’t know if this is possible too. It still feels like too much write operations.

If you think the the template approach is the best one for your case we take this one ;-).

Thanks a lot!

Best regards,

Timo

Yes, you can do a bunch of emails by industry for example and update the content via RSS.
The plugin is here, works with M3

Here is a video about how the RSS creation works (I also demo the twig plugin here)

The other road is the Twig one. :slight_smile:

Hi Joey,

again thanks a lot for your answer.

I asked ChrisRAoW about the performance regarding the number of requests for the rss-feed if the same feed is used in emails for different receivers (Question about performance/number of feed requests · Issue #42 · ChrisRAoW/mautic-rss-to-email-bundle · GitHub).

I think rss is an option but I think it could be a better performance if we write the complete list data into the twig template as you suggested.

Another idea and question: Can mautic be used to send the complete email with an rest api call?

In api documentation I just find a way to send a predefined email to an contact (Mautic Developer Documentation).

But when I’m in the mautic frontend in the detail view of a contact I can chose the “send e-mail” action where I can insert an individual email subject and content. Is this also possible with the api?

My idea was: Should it be an good option to generate the html code in out application and send this email via mautic to the contact (so the email is assigned to the contact in mautic and we can see activity for the email like opens and clicks also in mautic). Do you think this is possible (and a good idea)?

Thanks for some feedback!

Best regards,

Timo

Hi,
I have no experience with sending one to one email in Mautic. I also saw that you can do it, but I think you loose the automation you need. You would need to manually inject all the email instead of just creating one huge twig, that manages your whole daily send.
I wouldn’t do it, but because my experties are on the twig side. I hope someone who has done mass-api-injection can answer this.

Thanks for your reply.

So is it right that I can’t send an email with the api and directly send the html-code of the email in the request? I mean to send an email without create an new email in mautic before.

As I said: I mean a similar function like the action “send email” with an individual content/code for the email, but via the api. This could be a very interesting feature for my use-case (and some other use-cases I have in mind).

Next to this I found something interesting about tokens in emails (How send emails with Mautic with a custom text using API? - Stack Overflow and Mautic Developer Documentation). It seems that I can add any additional content to an email when I send it via rest api by adding these tokens into the rest request. I think I have to try this - it sounds very interesting for different thoughts and use cases.

Best regards,

Timo

So is it right that I can’t send an email with the api and directly send the html-code of the email in the request? I mean to send an email without create an new email in mautic before.

You can. I meant, that I don’t do it.

Here is the docs:
https://developer.mautic.org/#send-email-to-contact

Hi Joey,

thanks for your reply. I mean that I can’t set the html body/content of the email with this api request.

I have to create an email in mautic if I want to send an email with the rest api to a single contact (as in api documention: Send a predefined email to existing contact).

I’m now in evaluation of the tokens in the send api request. It looks very interesting but I have problems with the output of html in tokens. I found some github issues for this (WIPApi/send html tokens via sendtocontact by PurHur · Pull Request #3 · PurHur/mautic · GitHub, Add html:// prefix for tokens to send raw html via api by PurHur · Pull Request #8586 · mautic/mautic · GitHub) but I think these pull requests are not merged into the stable version. Do you have experiences with the tokens?

I don’t have, sorry.

No problem ;-).

Which Rss 2 email plugin would you recommend?

In you video it’s this one: https://mauticapps.com/

But I see that there are some other solutions for this:

I just was a litte bit wondering that there is no kind of contact data on the website mauticapps.com.

The first one.
I purchased the second one and found, that it’s the same as the first one :slight_smile: Don’t be a fool like me.

So you mean the one from ChrisRAoW? Ok - when it makes no difference I will try this one.