3

I’m working on an email template for Mailjet written in MJML that uses an array value provided through Vars to generate a list of items the sender wants to receive from the mail recipient. All values in the array are plain text values.

The data passed to the API request looks like this:

{
    "FromEmail":"sender@email.com",
    "FromName":"Chris Crumble",
    "Subject":"Data Request",
    "MJ-TemplateID":"200000",
    "MJ-TemplateLanguage":true,
    "Recipients":[
        {
            "Email":"recipient@email.com",
            "Name":"Hans Henson"
        }
    ],
    "Vars":{
        "mailTitle":"Data Request",
        "userName":"Chris Crumble",
        "imageUrl":"http://my.host.com/image.jpg",
        "userBirthDate":"1.3.1982",
        "recipientName":"Hans Henson",
        "uploadUrl":"https://my.upload.com/",
        "authVideoUrl":"https://my.authvideo.com",
        "records":["Document A","Document B"],
        "authPhone":"113777840097"
    }
}

The template uses var:records like this:

        ...
        </mj-text>
        <mj-raw> {% if var:records:false %} </mj-raw>
        <mj-text>
          <p>
            I, <strong>{{var:userName}}, born on {{var:userBirthDate}}</strong> am asking you to provide the following documents:
          </p>
        </mj-text>
        <mj-raw> {% for item in var:records %} </mj-raw>
        <mj-text>
          {{item}}
        </mj-text>
        <mj-raw> {% endfor %} </mj-raw>
        <mj-raw> {% else %} </mj-raw>
        <mj-text>
          <p>
            I, <strong>{{var:userName}}, born on {{var:userBirthDate}}</strong>, am asking you to provide all my existing documents.
          </p>
        </mj-text>
        <mj-raw> {% endif %} </mj-raw>
        <mj-text>
          ...

As long as var:records isn’t set in the data sent with the request, the mail is sent as expected. As soon as an (not empty) array value is provided with the request, the mail is blocked by Mailjet on sending without giving any further information on the reason.

No idea how to get this working.

UPDATE:

Thanks to Zhivko’s hint to the error reporting mechanism provided by Mailjet I was able to gain a little more insight into the problem.

The template produces the following error:

expression parsing error ## Unknown identifier: var:records:false ## near ## var:records:false ##

This still doesn’t make any sense to me as the line mentioned is an if condition with a default value of false defined for the case that no value for var:records is provided with the api request. Also the template only produces this error when the value is explicitely set in Vars and is not empty. My tests so far make me guess that it may have to do with the provided value being an array value as the line doesn’t cause any problems if the value is plain string.

Oliver
  • 507
  • 6
  • 12

4 Answers4

7

I had the same problem and after asking the MJML team on their Slack I add an answer. Just use the defined() method :

Example :

{% if defined(var:employees) %}
  My employees :
  <ul>
    {% for employee in var:employees %}
      <li>{{employee.firstname}} {{employee.lastname}}</li>
    {% endfor %}
  </ul>
{% endif %}

This method is correct and a core maintainer of MJML just says :

It's not publicly documented yet

PS : Their Slack is a good place to ask this kind of question and I had a response in minutes. (mjml.slack.com)

Andrei Matracaru
  • 3,511
  • 1
  • 25
  • 29
  • Yes @SweetTomato I still use it my templates – Florian Bezagu May 22 '20 at 15:18
  • @FlorianBezagu how do you join their Slack? It says "Contact the workspace administrator for an invitation", but I don't know how to contact the administrator... EDIT: nevermind, found it here: https://slacking-inviter.herokuapp.com/ – Thomas Levesque Jun 04 '20 at 10:05
  • Please don't advise anyone to join this slack for getting Mailjet Support. It's only for MJML related question. – iRyusa Feb 22 '21 at 11:47
3

The message is probably blocked due to an error in the template language. To receive details about the error, enable the error reporting mechanism. If you have troubles debugging the error message, open a support ticket with Mailjet for in depth investigation for the specific template.

Zhivko
  • 301
  • 1
  • 3
  • Thanks for the hint. Helped me to get some more information on the problem. Updated the question accodingly. Will also open a support ticket. – Oliver Sep 03 '17 at 14:03
0

As far as I know, Mailjet doesn't allow arrays as a personalized var.

DataType: the type of data that is being stored (this can be either a str, int, float or bool)

https://dev.mailjet.com/guides/#manage-contacts

-1

Solution:

<% if(typeof bar !== 'undefined') { %>
    variable 'bar' is defined in payload:
    <b><%= bar %></b>
<% } else {%>
    variable 'bar' is not defined in payload
<% }%>