TWIG JSON Payload in Send Email API

Your software
My Mautic version is: 4.1.1

Your problem
My problem is:

I am using the Twig Advanced Email Templates Plugin

I am using TWIG to JSON_DECODE an order confirmation payload.
As per the examples, I am successful in doing this by first writing the JSON payload to a custom field on the Contact i.e. {% set cart = contact.json_last_order| json_decode %}

Everything works successfully, but my question is whether I could eliminate the step of writing the JSON to the contact field, and instead post the JSON as part of the send email API ? (/api/emails/39/contact/689/send)

It seems that if I post JSON as part of the Send API payload body, e.g. my payload looks like

{
    "tokens": {
        "locale": "en_GB",
        "currency": "EUR",
        "entity_id": "36140"
}}

I can print within the Mautic email, values from the JSON e.g. {entity_id} will print in the email “36140”.
So it seems that the Mautic email somehow see’s the JSON attributes and can reference/print them.

What I can’t seem to figure out is whether I can use the same concept with the TWIG to just reference the JSON payload without referencing the contact.json_last_order field.

I have tried a simple TWIG of

{% set cart = tokens| json_decode %} 
{% if cart %} <!-- If Cart Not Null -->
THE JSON WAS PULLED !
{% endif %}

This doesn’t appear to work

Has anyone had any similar success?

My reasoning for eliminating the solution around the contact field, is simply because the contact field will contain the last order JSON, and if this wasn’t updated and the email triggered then it would print the previous order and perhaps not the most recent.

HI Gravitai

Have you apply the patch for this feature?

It’s this one:

Greetings
Sebastian

Thanks Sebastian,

We had first tried tried this plugin, but it wouldn’t work for us on Mautic 4.
We are using the Mautic Extendee plugin, which does work for Mautic 4.

The feature you mention for arbitrary tokens using email send api is precisely what we’re doing.
Zdeno Kuzmany from Mautic Extendee has reached out to me and I’ll sponsor a custom fix/feature for this Mautic Extendee plugin.

Thanks for your input.

Cheers
William

Hi William,

I am wondering if you ever got this to work? I am trying to replicate the exact same setup as you, passing a json array via the send email API so that TWIG can iterate through the array and print the details of each item. I am finding that normal tokens (non-json) can be referenced in the email just fine but for some reason the twig code isnt working on the json array. Any pointers would be greatly appreciated.

Thank you
Adam

You can use in last version of email tokens with the syntax {{ tokens['{webview_url}'] }}

Useful for example for custom content of email (abandoned cart, order confirmation, …)

Contact me personally If need a download link.