Simon Miller Team : Web Development Tags : Web Development Tips & Tricks

Mandrill, .NET and Handlebars

Simon Miller Team : Web Development Tags : Web Development Tips & Tricks

I was recently tasked with researching how to use the Mandrill API for sending transactional emails, versus our traditional method of constructing the email server side and using the Mandrill SMTP server to send the whole thing. The prospect was interesting and on the surface it did not look difficult; the templating language is very similar to parent MailChimp, so there was already some familiarity.

The email templates were already sliced directly into the Mandrill dashboard and the required properties already added (e.g. *|NAME|*) and ready to be merged via server code. What I was unsure about was how to send repeating blocks, for example, a list of products.

Mandrill had rudimentary support for this structure using editable regions, but it wasn’t very clean. In December 2014, Mandrill added support for Handlebars in their templates – great news. This provides a clean and intuitive structure for merging fields server side, including a logical way of specifying loops:

The {{#each var}} .. {{/each}} container specifies that the containing variables are part of a loop. Via the DIY methods that Mandrill describe this simple entails providing a child array of JSON data for the merge_var. The problem that I found was that, at the time of writing, the .NET API had not yet caught up to Handlebars support. That’s when I stumbled across this article that demonstrates what needs to change in the Mandrill C# library to support dynamic content.

By downloading the Mandrill API source code and changing the highlighted properties from string to dynamic, the AddRecipientVariable method (and this can also be applied to AddGlobalVariable method) will now accept complex types such as Lists. These are translated by the API into JSON and produce the desired result – repeated rows.

            MandrillApi api = new MandrillApi(AppConfiguration.MandrillApiKey);
            UserInfo info = await api.UserInfo();

            var email = new EmailMessage();
            var recipients = new List();
            recipients.Add(new EmailAddress(""));

            email.MergeLanguage = "handlebars";
            email.FromEmail = "";
            email.To = recipients;
            email.Subject = "Test email 123";

            email.AddGlobalVariable("name", "Simon Blah");
            email.AddGlobalVariable("quotes", new List() {
                new MailQuote() { DealerName = "Dealer 123", Fitting = "Fitting 1", Comments = "This is a 1st comment", PriceRange = "$120" },
                new MailQuote() { DealerName = "Dealer 456", Fitting = "Fitting 2", Comments = "This is a 2nd comment", PriceRange = "$240" },
                new MailQuote() { DealerName = "Dealer 789", Fitting = "Fitting 3", Comments = "This is a 3rd comment", PriceRange = "$999" } 

            var result = await api.SendMessageTemplate(new Mandrill.Requests.Messages.SendMessageTemplateRequest(email, "template_name", null));