2

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.

4

4 回答 4

3

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(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)

于 2017-10-25T19:31:31.467 回答
3

该消息可能由于模板语言错误而被阻止。要接收有关错误的详细信息,请启用错误报告机制。如果您在调试错误消息时遇到问题,请使用 Mailjet 开具支持票,以深入调查特定模板。

于 2017-08-29T10:57:17.153 回答
0

据我所知,Mailjet 不允许将数组作为个性化变量。

DataType:正在存储的数据类型(可以是 str、int、float 或 bool)

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

于 2017-09-28T10:33:08.537 回答
-1

解决方案:

<% if(typeof bar !== 'undefined') { %>
    variable 'bar' is defined in payload:
    <b><%= bar %></b>
<% } else {%>
    variable 'bar' is not defined in payload
<% }%>
于 2021-11-11T15:19:31.457 回答