Mandrill Editable Template: mc:edit link href

核能气质少年 提交于 2019-12-05 09:43:53

I have an Email Dispatcher that uses MandripApp to send normal emails (as SMTP) as well to send emails using the template.

I do not know how to pass what you are asking, as I'm not using mc:edit attributes any longer (as my users will never edit the template themselves, me or a developer will) but I can provide you help with the global variables.

Global variables are the same as Mailchimp vars, like *|EMAIL|* and this is what I do:

var mergeVars = Dictionary<string, string>();
mergeVars.Add("ORDER_ID", orderId);
mergeVars.Add("CUSTOMER_NAME", fullname);
mergeVars.Add("CUSTOMER_FNAME", fullname.Contains(" ") ? fullname.Split(' ')[0] : fullname);
mergeVars.Add("CUSTOMER_EMAIL", email);

for example, a hole table:

StringBuilder sb = new StringBuilder();
foreach (ProductInfo pi in products)
{
    sb.Append("<tr>");
    sb.AppendFormat("<td style=\"text-align:left;\"><img src=\"http://dynassets1.gavekortet.dk/{2}/products/trans/{1}_1.png\" alt=\"{0}\" /></td>", pi.Title, pi.ID, shopId);
    sb.AppendFormat("<td style=\"text-align:left;\">{0} x {1}</td>", pi.Qty, pi.Title);
    sb.AppendFormat("<td style=\"text-align:right;\">{0:N2}</td>", double.Parse(pi.CardValue));
    sb.Append("</tr>");
}

mergeVars.Add("ITEMS_LIST", sb.ToString());

in my template in MandrillApp I simply have (for the table part):

<table style="width: 100%; padding: 0 30px;">
    <thead>
      <tr>
        <th style="width:75px; text-align:left;">Gavekort</th>
        <th style="width:75px; text-align:left;">Ordreoversigt</th>
        <th style="width:75px; text-align:right;">Værdi (kr.)</th>
      </tr>
    </thead>

    <tbody>
        *|ITEMS_LIST|*
    </tbody>
</table>

and in code you do:

var tmplMessage = new MandrillSendTemplateItem();
tmplMessage.key = password;

tmplMessage.message = new MessageItem();

// Email Destination
tmplMessage.message.to = new List<MessageToItem>();
tmplMessage.message.to.Add(new MessageToItem() { name = destinationName, email = destinationEmail, type = "to" });
tmplMessage.message.to.Add(new MessageToItem() { name = "Bruno Alexandre", email = "my_email@domain.com", type = "bcc" }); // always send me a copy so I know what's going on

// Global Variables
tmplMessage.message.global_merge_vars = new List<TemplateContentItem>();
tmplMessage.message.global_merge_vars.Add(
    new TemplateContentItem() { 
        name = "TASKCOMPLETE", 
        content = DateTime.UtcNow.ToString("dd MMM yyyy HH:mm") });

// Global Variables passed in properties
if (properties != null)
{
    foreach (var p in properties)
    {
        tmplMessage.message.global_merge_vars.Add(
            new TemplateContentItem() { name = p.Key, content = p.Value });
    }
}

and send the email.

I hope it helps you doing what you need.


Note that you only pass the name of the global variable in your code, but in the template you need to call it wrapping it with |* and *| so:

tmplMessage.message.global_merge_vars.Add(
    new TemplateContentItem() { 
        name = "TASKCOMPLETE", 
        content = DateTime.UtcNow.ToString("dd MMM yyyy HH:mm") });

will be accessible in the template as:

<span class="completed">*|TASKCOMPLETE|*</span>

This post is pretty old but I thought I'd share my answer to this in case someone else stumbles upon here.

If you are using the Mandrill API, you need to actually send the variable values in the "global_merge_vars" or "merge_vars" keys. DO NOT use the template_content. Mandrill API was rather unintuitive this way.

So your content would remain the same with a variable:

*|ITEMS_LIST|*

Then your JSON body should have something like:

        "global_merge_vars": [
            {
                "name": "ITEMS_LIST",
                "content": "This is a list"
            }
        ],

source: How to add params to all links in a mandrill template through API?

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