Mandrill is a transactional email API for MailChimp users. If you wish to send an email when a specific event in your mobile application occurs, you can write a KScript method to do this, that your mobile application can then call.

Ingredients

This recipe assumes that you have KScripts enabled for your app and that you have your own Mailchimp account

Recipe

Click the 'Create Some KScript' button and include the following JavaScript into your method:

var client = K.http.createClient(
    'https://mandrillapp.com/api/1.0/');

var headers = {
    'Content-Type': 'application/json'
};

var payload = {
    "key": "YOUR-MANDRILL-API-KEY",
    "message": {
        "html": "<p>Some HTML content</p>",
        "text": "Some content",
        "subject": "Some subject",
        "from_email": "[email protected]",
        "from_name": "No-Reply",
        "to": [
            {
                "email": "[email protected]",
                "name": "Someone",
                "type": "to"
            }
        ],
        "images": [
            {
                "type": "image/png",
                "name": "IMAGECID",
                "content": "ZXhhbXBsZSBmaWxl"
            }
        ]
        "bcc_address": "[email protected]",
        "subaccount": "customer-123",
        "google_analytics_domains": [
            "somedomain.com"
        ],
        "google_analytics_campaign": "[email protected]",
        "metadata": {
            "website": "www.example.com"
        },
    }
    "async": false,
    "ip_pool": "Main Pool",
    "send_at": "example send_at"
};

var response = client.post('messages/send.json', K.JSON.stringify(payload), headers);

var result = K.JSON.parse(response.getBody());

if (result === null) {
    K.log('sending email failed');
    success = false;
} else if (result[0].status!="sent") {
    K.log("sending email failed with reason: "+result[0].reject_reason);
    success = false;
} else {
    K.log('sending email success');
    success = true;
}

K.setResponse({
    success: success
});

Not all of the above parameters (for example 'google_analytics_campaign') are required in the payload. Please see Mandrill API Reference for further details.

Your mobile application can then call your new KScript method when required or, if you wish to send a recurring email (for example a digest of recent activity), your KScript method can be scheduled to run once per hour or day.

If your application needs to send multiple emails for different events (i.e. from different KScript methods), then we would recommend encapsulating the mandrill integration in a helper method (for example '_mailer_') that takes in generic email parameters (to, from, subject, html, text etc). This helper method can then be included in your KScript methods with an include( _mailer_ ) call. This will prevent duplicating the same code in multiple methods and make it easier to switch email provider in the future.

Further reading

A full reference guide to the Mandrill API service can be found on the Mandrill website