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="admin@example.com", 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..
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?
来源:https://stackoverflow.com/questions/23998219/mandrill-editable-template-mcedit-link-href