SendTemplate with MergeVars

Oct 30, 2012 at 2:05 PM
Edited Oct 30, 2012 at 8:39 PM

If I understand correctly,  I need to use the MergeVars to allow me to send data to fill out the Template.

I have spent a few hours on this and can not figure out how to format it.

I have managed to send an email using the Template, just fine, but the email comes though with *|NAME|* instead of the merged data.

PLEASE HELP.

 

Can someone please post (preferably in VB.net, but I'll convert from C# if I have to) a simple example.

Here is my simple Template in mandrill:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <title>*|SUBJECT|*</title>
    </head>
    <body>
        Dear *|NAME|*,<br>
        Your order number is *|ORDERID|* woohoo.<br>
<br>
<br>
How's it?<br>
<br>
Thanks,<br>
Me
    </body>
</html>

 

 


 

Jan 10, 2013 at 6:17 PM

Did you figure out by your variables are not merging?  I am running into the same issue and was hoping you could provide some insight.

I have another email that is sent out with the merge variables *|FNAME|* and that email works just fine.  When I use something like *|RESETURL|* it doesn't merge the values into me email.  Any thoughts?

At the moment, here is what my code looks like for an email I am sending...

 

var api = new MandrillApi(_mandrillApiKey);

var recipients = new List<MailChimp.Types.Mandrill.Messages.Recipient>();

recipients.Add(new Mandrill.Messages.Recipient(emailAddress, string.Empty));

var message = new Mandrill.Messages.Message()
{
	To = recipients.ToArray(),
	FromEmail = "email@somesite.com",
	Subject = "Password Reset...",
};

var templateContent = new List<Mandrill.NameContentPair<string>>();
templateContent.Add(new MailChimp.Types.Mandrill.NameContentPair<string>("*|RESETTOKEN|*", resetToken));
templateContent.Add(new MailChimp.Types.Mandrill.NameContentPair<string>("*|RESETURL|*", resetUrl));

var templateInfo = api.TemplateInfo("Password Reset");

var result = api.SendTemplate("Password Reset", templateContent, message);

Thanks,
Brian 

Coordinator
Jan 10, 2013 at 7:03 PM
Edited Jan 10, 2013 at 10:26 PM

I think you should remove "*|" from the name of a merge var:


templateContent.Add(new NameContentPair<string>("RESETTOKEN", resetToken));
templateContent.Add(new NameContentPair<string>("RESETURL", resetUrl));


just like in this example: https://mandrillapp.com/api/docs/templates.html#method=render

Jan 10, 2013 at 10:17 PM

@denisk1981, I think you are right but that still is not the solution to my problem.  I received a message from Mandrill Support that explains my problem a little better.  They went through the API logs and found that I was passing my mergevars into the template content instead of the message.

Hey Brian,
Thanks! It looks like you're using the send-template API endpoint, but the merge tag values are being passed as part of the 'template_content' parameter instead of the 'merge_vars' parameter. Mandrill will automatically fill in the first and last name, if they're provided in the 'to' parameter, which is why you're seeing FNAME and LNAME work. However, 'template_content' is intended for replacing mc:edit regions in your template, not merge tags.

So now I am trying to determine how I should create my list of mergevars and pass it to the message.  So it would be something like this but I haven't figured out the syntax to get the collection of mergeVars together...

var message = new Mandrill.Messages.Message()
{
	To = recipients.ToArray(),
	FromEmail = "email@somesite.com",
	Subject = "Password Reset...",
	MergeVars = mergeVars,
};
 

 

Brian

Coordinator
Jan 10, 2013 at 10:29 PM

MergeVars are defined per recipient whereas GlobalMergeVars are the same for all recipients.

GlobalMergeVars param is an array of NameContentPair<string>.

Jan 10, 2013 at 11:16 PM

@denisk1981 thank you for that tip.  I was able to get my code working using the GlobalMergeVars.

Jan 19, 2013 at 10:54 PM

bbehm,

would you be able to post your code for the GlobalMergeVars? I've encountered the same problem. 

Kind Regards

Jan 21, 2013 at 5:52 PM

@bewise, here you go...

public void SendWelcomeEmail(string emailAddress, string firstName, string lastName)
        {
            var api = new MandrillApi(_mandrillApiKey);

            var recipients = new List<MailChimp.Types.Mandrill.Messages.Recipient>();
            var name = string.Format("{0} {1}", firstName, lastName);
            recipients.Add(new Mandrill.Messages.Recipient(emailAddress, name));

            var globalMergeVars = new List<Mandrill.NameContentPair<string>>();
            globalMergeVars.Add(new Mandrill.NameContentPair<string>("FNAME", firstName));
            globalMergeVars.Add(new Mandrill.NameContentPair<string>("LNAME", lastName));
	    // add more global variables here as necessary

            var message = new Mandrill.Messages.Message()
            {
                To = recipients.ToArray(),
                FromEmail = "info@some.com",
                Subject = "SomeSite - Welcome...",
                GlobalMergeVars = globalMergeVars.ToArray(),
            };
            
            var templateContent = new List<Mandrill.NameContentPair<string>>();
            templateContent.Add(new MailChimp.Types.Mandrill.NameContentPair<string>("FNAME", firstName));
            templateContent.Add(new MailChimp.Types.Mandrill.NameContentPair<string>("LNAME", lastName));

            var result = api.SendTemplate("Welcome Email", templateContent, message);
        }

Mar 13, 2013 at 2:24 AM
Am I missing something here? I am getting this error when I try to set GlobalMergeVars with globalMergeVars.ToArray():

Connot convert source type 'MailChimp.Types.Mandrill.NameContentPair<string>[]' to target type 'MailChimp.Types.Opt<MailChimp.Types.Mandrill.Merges>'
Coordinator
Mar 13, 2013 at 8:51 AM
It's different now.
Mar 20, 2013 at 9:36 PM
@denisk1981 what has changed? I just started a new project trying to use my previous code and am having the same issues as @nathanielornate.

Thanks,
Brian
Coordinator
Mar 20, 2013 at 9:48 PM
Edited Mar 20, 2013 at 9:51 PM
Hi Brian,

I noticed from your sample code that there is some redundancy:
            var globalMergeVars = new List<Mandrill.NameContentPair<string>>();
            globalMergeVars.Add(new Mandrill.NameContentPair<string>("FNAME", firstName));
            globalMergeVars.Add(new Mandrill.NameContentPair<string>("LNAME", lastName));
So, I changed the structure of merges:
            var globalMergeVars = new Mandrill.Merges();
            globalMergeVars.Add("FNAME", firstName);
            globalMergeVars.Add("LNAME", lastName);
Mar 20, 2013 at 10:09 PM
@denisk1981,
So I changed my code to this using your example above...
public void SendWelcomeEmail(string emailAddress, string firstName, string lastName)
        {
            var api = new MandrillApi(_mandrillApiKey);

            var recipients = new List<MailChimp.Types.Mandrill.Messages.Recipient>();
            var name = string.Format("{0} {1}", firstName, lastName);
            recipients.Add(new Mandrill.Messages.Recipient(emailAddress, name));

            var globalMergeVars = new Mandrill.Merges();
            globalMergeVars.Add("FNAME", firstName);
            globalMergeVars.Add("LNAME", lastName);
        // add more global variables here as necessary

            var message = new Mandrill.Messages.Message()
            {
                To = recipients.ToArray(),
                FromEmail = "info@some.com",
                Subject = "SomeSite - Welcome...",
                GlobalMergeVars = globalMergeVars,
            };
            
            var templateContent = new List<Mandrill.NameContentPair<string>>();
            templateContent.Add(new MailChimp.Types.Mandrill.NameContentPair<string>("FNAME", firstName));
            templateContent.Add(new MailChimp.Types.Mandrill.NameContentPair<string>("LNAME", lastName));

            var result = api.SendTemplate("Welcome Email", templateContent, message);
        }
Coordinator
May 9, 2013 at 7:17 PM
Edited May 9, 2013 at 7:20 PM
Updated example (I hope this is the very last version):
        public void SendWelcomeEmail(string emailAddress, string firstName, string lastName)
        {
            var api = new MandrillApi(_mandrillApiKey);

            var recipients = new List<MailChimp.Types.Mandrill.Messages.Recipient>();
            var name = string.Format("{0} {1}", firstName, lastName);
            recipients.Add(new Mandrill.Messages.Recipient(emailAddress, name));

            var globalMergeVars = new Mandrill.NameContentList<string>();
            globalMergeVars.Add("FNAME", firstName);
            globalMergeVars.Add("LNAME", lastName);
            // add more global variables here as necessary

            var message = new Mandrill.Messages.Message()
            {
                To = recipients.ToArray(),
                FromEmail = "info@some.com",
                Subject = "SomeSite - Welcome...",
                GlobalMergeVars = globalMergeVars,
            };
            
            var templateContent = new Mandrill.NameContentList<string>();
            templateContent.Add("FNAME", firstName);
            templateContent.Add("LNAME", lastName);

            var result = api.SendTemplate("Welcome Email", templateContent, message);
        }
Jul 5, 2013 at 3:35 AM
I have written a block of code that sends recipient specific merge variables along with other details. Hope this block of code might be useful to some users.
// Send emails via Mandrill
static void SendEmail(Email emailFrom, List<Member> emailSubscribers, String emailSubject, String emailBody, String emailTags)
{
    try
    {
        MandrillApi mandrillAPI = new MandrillApi(Settings.MandrillAPIKey); // Read API Key from app.config
        MailChimp.Types.Mandrill.Messages.Message message = new MailChimp.Types.Mandrill.Messages.Message();

        // Email From
        message.FromEmail = emailFrom.Address;
        message.FromName = emailFrom.Name;
        message.PreserveRecipients = false;     // Don't expose To Email address to all receipient.
        message.Tags = emailTags.Split(',');    // Comma seperated tages should be converted to an array
        message.TrackOpens = true;              // Track if emails were opened by recipients
        message.TrackClicks = true;             // Track if the URLs in mail were clicked
        message.Merge = true;                   // Use merge variables

        Int32 recipientCount = emailSubscribers.Count();

        MailChimp.Types.Mandrill.Messages.Recipient[] recipientList = new MailChimp.Types.Mandrill.Messages.Recipient[recipientCount];  // Receipient List
        MailChimp.Types.Mandrill.Messages.MergeVars[] mergeVarList = new MailChimp.Types.Mandrill.Messages.MergeVars[recipientCount];   // Receipient specific Merge Vars

        for (Int32 counter = 0; counter < recipientCount; counter++)
        {
            // Add new recipient email and name
            recipientList[counter] = new MailChimp.Types.Mandrill.Messages.Recipient(emailSubscribers[counter].Email, emailSubscribers[counter].FirstName + " " + emailSubscribers[counter].LastName);

            // Add receipient specific information
            var mergeVars = new MailChimp.Types.Mandrill.NameContentList<string>();
            mergeVars.Add("ACTIVATIONCODE", emailSubscribers[counter].ActivationCode.ToString());
            mergeVarList[counter] = new MailChimp.Types.Mandrill.Messages.MergeVars(emailSubscribers[counter].Email, mergeVars);
        }

        // Assign receipient list to message
        message.To = recipientList;

        // Assign receipient related merge vars list to message
        message.MergeVars = mergeVarList;
                                
        // Email Subject
        message.Subject = emailSubject;

        // Email Body
        message.Html = emailBody;

        mandrillAPI.Send(message);
    }
    catch (MailChimp.Types.MCException ex)
    {
        throw;
    }
}
Feb 14, 2015 at 12:37 PM
Any updates in 2015 ? Nuget ?

Another API for Mandrill in C# , updated
https://github.com/shawnmclean/Mandrill-dotnet/issues/114