【问题标题】:Mandrill ignores line breaks (\n)Mandrill 忽略换行符 (\n)
【发布时间】:2015-07-01 19:32:55
【问题描述】:

使用send-template 方法和Lutung Mandrill Java API 实现,当模板变量包含\n 形式的换行符时,它们会在生成的电子邮件中被完全忽略或剥离。包含\n 字符的Strings 保持不变,直到执行GET send-template 方法。所以我猜想在 Mandrill 服务器上有一些String 分析和转换,它会删除特殊字符。

如何在 Mandrill 电子邮件上重新换行?

【问题讨论】:

    标签: mandrill


    【解决方案1】:

    在我的情况下,我将 Handlebars 用于山魈模板,我通过将 HTML 发送到模板 <br/> 而不是 \n 解决了这个问题,方法是将我的字符串中的 \n 替换为 <br/> 类似:.Description.Replace("\n", "<br/>");

    然后在山魈模板上,我将变量放入 {{{ variable }}} 而不是 {{ variable }}

    http://blog.mandrill.com/handlebars-for-templates-and-dynamic-content.html

    HTML 转义

    Handlebars HTML 转义返回的值 {{expression}}。如果您不希望 Handlebars 转义值,请使用 “三重存储”,“{{{.

    在您的模板中:

    <div> {{{html_tag_content}}} </div> 在您的 API 请求中:

       "global_merge_vars": [   {
          "name": "html_tag_content",
          "content": "This example<br>is all about<br>the magical world of handlebars"   } ]
    

    结果:

    这个例子
    都是关于
    车把的神奇世界

    【讨论】:

      【解决方案2】:

      如果你想发送更复杂的内容,无论是html,还是带有换行符之类的变量,你可以先渲染模板然后发送消息,而不是直接使用send-template

      调用templates.render渲染模板:

      {
          "key": "example key",
          "template_name": "Example Template",
          "template_content": [
              {
                  "name": "editable",
                  "content": "<div>content to inject *|MERGE1|*</div>"
              }
          ],
          "merge_vars": [
              {
                  "name": "merge1",
                  "content": "merge1 content\n contains line breaks"
              }
          ]
      }
      

      将结果保存到变量rendered_content

      使用带有messages.send的渲染模板发送消息:

      {
          "message": {
              "html": rendered_content,
              ...
          ...
      }
      

      【讨论】:

      • 这有点太模糊了。
      • @AlonDahari 我添加了一个简单的例子来更好地解释我的意思
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 2020-05-20
      • 2021-06-17
      • 2017-03-27
      相关资源
      最近更新 更多