Mandrill is a transactional email API for MailChimp users.

If you wish to send an email when a specific event in your mobile app occurs, you can write a KScript method to do this, that you can call from your mobile app or use as an action in an Automation.

Ingredients

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

Recipe

Expand "Backend" in the left hand menu, select "API" and then click the "KScript" button. Create a new method called something like _mailer_ and include the following JavaScript function:

// {
//     "key": "example key",
//     "message": {
//         "html": "<p>Example HTML content</p>",
//         "text": "Example text content",
//         "subject": "example subject",
//         "from_email": "[email protected]",
//         "from_name": "Example Name",
//         "to": [
//             {
//                 "email": "[email protected]",
//                 "name": "Recipient Name",
//                 "type": "to"
//             }
//         ],
//         "headers": {
//             "Reply-To": "[email protected]"
//         },
//         "important": false,
//         "track_opens": null,
//         "track_clicks": null,
//         "auto_text": null,
//         "auto_html": null,
//         "inline_css": null,
//         "url_strip_qs": null,
//         "preserve_recipients": null,
//         "view_content_link": null,
//         "bcc_address": "[email protected]",
//         "tracking_domain": null,
//         "signing_domain": null,
//         "return_path_domain": null,
//         "merge": true,
//         "merge_language": "mailchimp",
//         "global_merge_vars": [
//             {
//                 "name": "merge1",
//                 "content": "merge1 content"
//             }
//         ],
//         "merge_vars": [
//             {
//                 "rcpt": "[email protected]",
//                 "vars": [
//                     {
//                         "name": "merge2",
//                         "content": "merge2 content"
//                     }
//                 ]
//             }
//         ],
//         "tags": [
//             "password-resets"
//         ],
//         "subaccount": "customer-123",
//         "google_analytics_domains": [
//             "example.com"
//         ],
//         "google_analytics_campaign": "[email protected]",
//         "metadata": {
//             "website": "www.example.com"
//         },
//         "recipient_metadata": [
//             {
//                 "rcpt": "[email protected]",
//                 "values": {
//                     "user_id": 123456
//                 }
//             }
//         ],
//         "attachments": [
//             {
//                 "type": "text/plain",
//                 "name": "myfile.txt",
//                 "content": "ZXhhbXBsZSBmaWxl"
//             }
//         ],
//         "images": [
//             {
//                 "type": "image/png",
//                 "name": "IMAGECID",
//                 "content": "ZXhhbXBsZSBmaWxl"
//             }
//         ]
//     },
//     "async": false,
//     "ip_pool": "Main Pool",
//     "send_at": "example send_at"
// }
function sendEmail(fromName, fromEmail, toName, toEmail, subject, bodyHtml, bodyText) {
    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": contentHtml,
            "text": contentText,
            "subject": subject,
            "from_email": fromEmail,
            "from_name": fromName,
            "to": [
                {
                    "email": toEmail,
                    "name": toName,
                    "type": "to"
                }
            ],
        }
        "async": false,
        "ip_pool": "Main Pool",
    };

    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
    });
}

You can then include your _mailer_ helper method and call the sendEmail() function from any scripts that need to send an email. To try it out, create another Kscript method called something like sendEmail, add input parameters(strings) for the from name and address, to name and address, subject and body and then paste in the following code:

include("_mailer_");

sendEmail(
    K.params.fromName,
    K.params.fromAddress,
    K.params.toName,
    K.params.toAddress,
    K.params.subject,
    K.params.bodyHtml,
    K.params.bodyText
);

You can now test your new method and use as an action in an Automation.

There are many more parameters shown in the comment block above (e.g. 'bcc_address') that can be added to the payload. Please see Mandrill API Reference for further details.

If you are using our Backend feature, you can also call this method from your mobile app or, if you wish to send a recurring email (for example a digest of recent activity), this can be scheduled to run once per hour or day.

Further reading

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