Using Mandrill with LiveCode

Mandrill is a transactional email platform from MailChimp. MailChimp is for newsletter and other types of bulk email, Mandrill is an api that lets you send transaction based email such as password reminders, contact forms submissions, sign-up confirmations etc.

What I like about services like Mandrill is that it means you don’t have to worry about running you own mail service or using the SMTP services that may come with your hosting.

12000 free emails per month also makes it a perfect solution for the average website.

To get started with Mandrill, you will first need to sign-up for an account and setup an api key, this can be done via the settings option on the dashboard.

Mandrill provides a RESTful api and is easy to connect to with LiveCode. Data passed to and from the API can be done through a couple of different formats, but for this blog post, I am just going to use Mandrill’s default format, json. If you are not familiar with json, take a look at this tutorial.

Sending emails via the api is a pretty straight forward exercise.

Step 1) Include easyjson.lc (lets you can an array to json and json to an array)

include "includes/easyjson.lc"

Step 2) Create an array containing the details of the email

# set the key (get this from the Mandrill setting part of the dashboard)
    put "api-key-from-mandrill" into mandrillArray[key]
# set the senders details
    put "[email protected]" into mandrillArray[message][from_email]
    put "From Name" into mandrillArray[message][from_name]
    put "[email protected]" into mandrillArray[message][headers][ReplyTo]
# contents of the email
    put "Mandrill test" into mandrillArray[message][subject]
    put "This is a Mandrill Test" into mandrillArray[message][html]
# set the receivers details
    put "[email protected]" into mandrillArray[message][todetails][1][email] 
    put "Recipients Name" into mandrillArray[message][todetails][1][name]
    put "to" into mandrillArray[message][todetails][1][type]
# misc settings 
    put "false" into mandrillArray[message][mergesetting] 
    put "Mandrill Test" into mandrillArray[message][tags][1] 
    put "Main Pool" into mandrillArray[ip_pool]

Step 3) Convert the array to json

put jsonFromArray( mandrillArray ) into mandrillJSON

Unfortunately Mandrill naming requirements conflicted with LiveCodes array keys and these need to be fixed.

replace "ReplyTo" with "Reply-To" in mandrillJSON
replace "todetails" with "to" in mandrillJSON
replace "mergesetting" with "merge" in mandrillJSON

Step 4) Submit to the Mandrill api and convery the response to json

post mandrillJSON to URL "https://mandrillapp.com/api/1.0/messages/send.json"
put it into pJson
put arrayFromJSON( pJson ) into jsonArray

If everything goes smoothly, and there are no error messages, check your inbox for the test message.

Most of the time though, you will probably want to send a variety of emails from within your web app / site. Wrapping the above in a function or a command will make your life a bit easier.

<?lc
# include files
 include "includes/easyjson.lc"
 include "includes/displayarray.lc"

# Create the function to send the email via Mandrill
 function mandrillSend mKey, mFromEmail, mFromName, mReplyTo, mSubject, mHTML, mReceiverEmail, mReceiverName, mTag
 put mKey into mandrillArray[key]
 # set the senders details
 put mFromEmail into mandrillArray[message][from_email]
 put mFromName into mandrillArray[message][from_name]
 put mReplyTo into mandrillArray[message][headers][ReplyTo]
 # contents of the email
 put mSubject into mandrillArray[message][subject]
 put mHTML into mandrillArray[message][html]
 # set the receivers details
 put mReceiverEmail into mandrillArray[message][todetails][1][email]
 put mReceiverName into mandrillArray[message][todetails][1][name]
 put "to" into mandrillArray[message][todetails][1][type]
 # misc settings
 put "false" into mandrillArray[message][mergesetting]
 put mTag into mandrillArray[message][tags][1]
 put "Main Pool" into mandrillArray[ip_pool]
 # create the array
 put jsonFromArray( mandrillArray ) into mandrillJSON
 replace "ReplyTo" with "Reply-To" in mandrillJSON
 replace "todetails" with "to" in mandrillJSON
 replace "mergesetting" with "merge" in mandrillJSON
 # post to the api
 post mandrillJSON to URL "https://mandrillapp.com/api/1.0/messages/send.json"
 put it into pJson
 put arrayFromJSON( pJson ) into jsonArray
 return jsonArray[1][status]
 end mandrillSend

# Send an email

 put "put-your-key-here" into mKey
 put "[email protected]" into mFromEmail
 put "From Name" into mFromName
 put "[email protected]" into mReplyTo
 put "Mandrill test" into mSubject
 put "This is a Mandrill function Test" into mHTML
 put "[email protected]" into mReceiverEmail
 put "T Name" into mReceiverName
 put "Mandrill Test" into mTag --Tag the mail

 put mandrillSend(mKey, mFromEmail, mFromName, mReplyTo, mSubject, mHTML, mReceiverEmail, mReceiverName, mTag)
?>

The above is a very simple example of how to send an email, and Mandrill offers numerous other options. Read the api documentation for more information.

Download the above examples (zip)

Leave a Reply