Mandrill ignores line breaks (\n)

梦想的初衷 提交于 2019-12-23 11:20:14

问题


Using the send-template method and the Lutung Mandrill Java API implementation, when a template variable contains line breaks of the form \n, they are totally ignored or stripped on generated emails. The Strings containing \n characters are kept untouched untill the GET send-template method is executed. So I guess there is some String analysis and conversion on Mandrill servers which removes special characters.

How can I make work again line breaks on Mandrill emails?


回答1:


In my case I am using Handlebars for mandrill templates and I have solved this by sending HTML to the template <br/> instead of \n by replacing \n in my string with <br/> something like: .Description.Replace("\n", "<br/>");

And then on the mandrill template I put the variable inside {{{ variable }}} instead of {{ variable }}

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

HTML escaping

Handlebars HTML-escapes values returned by an {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash", "{{{.

In your template:

<div> {{{html_tag_content}}} </div> In your API request:

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

The result:

This example
is all about
the magical world of handlebars




回答2:


If you want to send more complex content, be it html, or variables with break lines and whatnot, you can first render the template and then send the message, instead of directly using send-template.

Render the template with a call to 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"
        }
    ]
}

Save the result to a variable rendered_content.

Send the message using the rendered template with messages.send:

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


来源:https://stackoverflow.com/questions/29826425/mandrill-ignores-line-breaks-n

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!