Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mandrill Editable Template: mc:edit link href

I'm trying to use Mandrill templates to send order tracking emails.

Using the mc:edit works well for simple text like <span mc:edit="ship_id">ship_id</span><br>

I was wondering if there was a way to pass the href link in a variable i.e. tracking_url

<a class="mcnButton " title="Track order" href=tracking_url target="_blank" style="font-weight: bold;text-align: center;">Track Order</a>

I'm using Djrill for Django and here's the code which sends the email so far, and I'd like to add tracking_url as a template_content variable or something similar

msg = EmailMessage(subject="Track your order", from_email="[email protected]", to=[user.email])
    msg.template_name = "order-sent"
    msg.template_content = {'order_id' : order_id, 'order_date' : order_date, 'order_type' : order_type, 'first_name' : user.first_name, 'last_name' : user.last_name, 'phone' : user.info.phone,
    'd_street' : d.street, 'd_zipcode' : d.zipcode, 'd_city' : d.city, 'd_country' : d.country}
    msg.send()

It seems possible using the AddGlobalVariable method (read here) but I can't figure out how to use it..

like image 526
bpipat Avatar asked Jun 02 '14 15:06

bpipat


2 Answers

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 = "[email protected]", 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>
like image 52
balexandre Avatar answered Nov 05 '22 07:11

balexandre


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?

like image 30
Cyrois Avatar answered Nov 05 '22 08:11

Cyrois