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

**URL:** <https://forum.mautic.org/t/using-rest-api-tokens-in-template-mails-with-newlines-or-html-tags/22589>\
**Category:** Product Support\
**Tags:** mautic-3\
**Created:** [February 4, 2022, 7:26am UTC](https://forum.mautic.org/t/using-rest-api-tokens-in-template-mails-with-newlines-or-html-tags/22589 "2022-02-04T07:26:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![imog](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/imog/32/3813_2.png) [@imog](https://forum.mautic.org/u/imog)\
**Post date:** [February 4, 2022, 7:26am UTC](https://forum.mautic.org/t/using-rest-api-tokens-in-template-mails-with-newlines-or-html-tags/22589/1 "2022-02-04T07:26:41Z")

</div>

I make heavy use of the REST-API call [POST /emails/ID/contact/CONTACT\_ID/send](https://developer.mautic.org/#send-email-to-contact) 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?

---

<div class="post-metadata">

**Author:** ![khalid\_zamer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/khalid_zamer/32/587_2.png) [@khalid\_zamer](https://forum.mautic.org/u/khalid_zamer)\
**Post date:** [February 5, 2022, 4:01pm UTC](https://forum.mautic.org/t/using-rest-api-tokens-in-template-mails-with-newlines-or-html-tags/22589/2 "2022-02-05T16:01:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![khalid\_zamer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/khalid_zamer/32/587_2.png) [@khalid\_zamer](https://forum.mautic.org/u/khalid_zamer)\
**Post date:** [February 5, 2022, 4:05pm UTC](https://forum.mautic.org/t/using-rest-api-tokens-in-template-mails-with-newlines-or-html-tags/22589/3 "2022-02-05T16:05:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imog](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/imog/32/3813_2.png) [@imog](https://forum.mautic.org/u/imog)\
**Post date:** [March 21, 2022, 11:34am UTC](https://forum.mautic.org/t/using-rest-api-tokens-in-template-mails-with-newlines-or-html-tags/22589/4 "2022-03-21T11:34:48Z")

</div>

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](https://developer.mautic.org/#send-email-to-contact)  
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):

```auto
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:

```auto
<?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_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => json_encode(["tokens" => [ "{last_order_date}" => "2019-06-21"] ]);
  CURLOPT_HTTPHEADER => array(
    'Authorization: Basic {{auth_token}}',
    'Content-Type: application/json'
  ),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

```

---

<div class="post-metadata">

**Author:** ![khalid\_zamer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/khalid_zamer/32/587_2.png) [@khalid\_zamer](https://forum.mautic.org/u/khalid_zamer)\
**Post date:** [March 21, 2022, 3:34pm UTC](https://forum.mautic.org/t/using-rest-api-tokens-in-template-mails-with-newlines-or-html-tags/22589/5 "2022-03-21T15:34:26Z")

</div>

Thanks, @imog for your elaborate response, I will try it out. BTW I use [n8n](https://n8n.io/integrations/n8n-nodes-base.mautic) HTTP requests and Mautic automation node to trigger those emails, I will explore how to use the tokens there. Thanks a lot

---

<div class="post-metadata">

**Author:** ![imog](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/imog/32/3813_2.png) [@imog](https://forum.mautic.org/u/imog)\
**Post date:** [March 22, 2022, 6:53am UTC](https://forum.mautic.org/t/using-rest-api-tokens-in-template-mails-with-newlines-or-html-tags/22589/6 "2022-03-22T06:53:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![entrepositive](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/entrepositive/32/9645_2.png) [@entrepositive](https://forum.mautic.org/u/entrepositive)\
**Post date:** [November 23, 2022, 4:22am UTC](https://forum.mautic.org/t/using-rest-api-tokens-in-template-mails-with-newlines-or-html-tags/22589/7 "2022-11-23T04:22:25Z")

</div>

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.

EP

---

<div class="post-metadata">

**Author:** ![entrepositive](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/entrepositive/32/9645_2.png) [@entrepositive](https://forum.mautic.org/u/entrepositive)\
**Post date:** [November 23, 2022, 4:30am UTC](https://forum.mautic.org/t/using-rest-api-tokens-in-template-mails-with-newlines-or-html-tags/22589/8 "2022-11-23T04:30:15Z")

</div>

@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](https://forum.mautic.org/t/encoding-html-to-pass-through-customhtml-item-in-api-call/26166).

EP
