Mailjet: Array value provided through vars makes email being blocked on sending

5.5k views Asked by At

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":"[email protected]",
    "FromName":"Chris Crumble",
    "Subject":"Data Request",
    "MJ-TemplateID":"200000",
    "MJ-TemplateLanguage":true,
    "Recipients":[
        {
            "Email":"[email protected]",
            "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.

4

There are 4 answers

3
Florian Bezagu On BEST ANSWER

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)

1
Zhivko On

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.

1
Daniel Z. On

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

0
Vlad Repkin On

Solution:

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