Using (REST-API) tokens in template mails with newlines or HTML tags?

I make heavy use of the REST-API call POST /emails/ID/contact/CONTACT_ID/send of Mautic and one of my favorites is sending template E-Mails to single/several contacts and providing {tokens} in the request, which then fills up the token-aliases within the template itself when creating the E-Mail.

This works very well for simple text and keep all the formatting made in the builder for that alias.
But when it comes to create something more complex, like a list, i wish there could be a way to give format instructions within the replacement text also. Maybe as special builder-macros or simply by HTML tags. Currently this does not work, or i still don’t know how… any idea about that?

Hi @imog , Yesterday I discovered Mautic 4 allows the use of HTML in a rich text custom field,
So you can create a new custom field ( rich text with HTML enabled ) which you can maybe use as regular tokens, I have not tested this yet but I assume it will work, i’m planning to update the HTML custom field before an email is sent, send the email, then reset the html field to blank after an email is sent or do additional API tricks.

Let me know if you try this and it works. i’m also looking for a way to dynamically change email template via API call


@imog can you please send the syntax you use to provide tokens in the email post api call?

Hi @khalid_zamer , so sorry for the huge delay of my answer, i’ve completely missed your posts here!
Sending emails with custom tokens inside is fairly easy. Just use the endpoint as described here in the REST-API docs:
POST /emails/ID/contact/CONTACT_ID/send
and attach as JSON encoded payload containing a “tokens” array, having key/value pairs of the replacement tokens found in the email-template.

For example, you have an template-email containing a simple text like “You placed your last order at {last_order_date}” (i adopted the syntax of Mautic for tokens with those curly braces).

In a simulated CURL this would look like (replace values in double-curly-braces with your own data):

curl --location -g --request POST 'https://{{mautic_url}}/api/emails/{{email_id}}/contact/{{contact_id}}/send' \
--header 'Authorization: Basic {{auth_token}}' \
--header 'Content-Type: application/json' \
--data-raw '{"tokens":{"{last_order_date}":"2019-06-21"}}'

(see that JSON encoded array in data-raw?)

Doing the very same thing in PHP:


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://{{mautic_url}}/api/emails/%7B%7Bemail_id%7D%7D/contact/%7B%7Bcontact_id%7D%7D/send',
  CURLOPT_POSTFIELDS => json_encode([ "tokens" => [ "{last_order_date}" => "2019-06-21" ] ]);
    'Authorization: Basic {{auth_token}}',
    'Content-Type: application/json'
$response = curl_exec($curl);
echo $response;
1 Like

Thanks, @imog for your elaborate response, I will try it out. BTW I use n8n HTTP requests and Mautic automation node to trigger those emails, I will explore how to use the tokens there. Thanks a lot

1 Like

Yesterday i upgraded my Mautic 3.3.2 Installation to a 4.2.0 and also enabled the new GrapeJS editor. I now try to get a Richtext-field working with tokens, like you stated above.

I did’nt know this n8n-thing, but it looks somewhat interesting. Personally i use a bunch of self written tools in PHP to orchestrize my Mautic workflows. Can you tell how you use it? Maybe with an example?

1 Like

If you can write custom PHP to send Mautic emails, you will likely have no challenge with n8n. It’s designed to be “low code”, meaning it can be done with no coding experience, and coding can be used to increase capabilities.


@imog @khalid_zamer

I am reading in content in markdown, convert to HTML, then need to pass that into a Mautic segment email. I set up an API call (also within n8n) to create a new segment/list email. It seems to work although not 100% correctly.

If I pass the full email HTML through the “customHtml” item in the JSON to Mautic, the request fails. If I encode the HTML, it works and builds the HTML correctly in Mautic.

So my question is: How do I encode this HTML programmatically?

EDIT: I started a new thread on this here.