首页 > 解决方案 > 用于在 Handlebars 表达式中使用收件人变量批量发送的 Mailgun 模板

问题描述

我正在尝试为批量发送创建 Mailgun 模板,并且该模板需要使用Handlebars条件中的接收者变量来确定要使用的语言,如下所示:

<div>
  {{#if %recipient.eng%}}
    Hello
  {{else}}
     Bonjour
  {{/if}}
</div>

但是,在进行 POST 调用以创建上述模板时,我收到错误响应:

 "message": "template parse error: Parse error on line 2:\nLexer error\nToken: Error{\"Unexpected character in expression: '%'\"}"

Mailgun 网站上没有任何使用 Handlebars 批量发送的文档,因此它可能是不可能的。
有没有人成功地做到这一点?

标签: handlebars.jsmailgun

解决方案


您需要将参与者变量复制为电子邮件的标准变量。请注意以 -F v:eng 开头的行。

curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
-F from='Excited User <YOU@YOUR_DOMAIN_NAME>' \
-F to=alice@example.com \
-F to=bob@example.com \
-F recipient-variables='{"bob@example.com": {"first":"Bob", "id":1, "eng": true}, "alice@example.com": {"first":"Alice", "id": 2, "eng": false}}' \
-F v:eng='%recipient.eng%'
-F subject='Hey, %recipient.first%' \
-F text='If you wish to unsubscribe, click http://mailgun/unsubscribe/%recipient.id%'

然后,您将能够使用车把模板中的标准变量访问它

<div>
  {{#if %eng%}}
    Hello
  {{else}}
     Bonjour
  {{/if}}
</div>

推荐阅读