Using Parse Server to send emails via Mandrill

Introduction

 

Mandrill is an email infrastructure service offered as an add-on for MailChimp that you can use to send personalized, one-to-one e-commerce emails, or automated transactional emails.

This tutorial will walk you through creating a sample app for Android on how to set up and connect Mandrill API to your Parse Server application.

Prerequisites

 

You will need to have a verified email domain and Android Studio installed, preferably at the newer version. Before beginning this tutorial, you should have some familiarity with working with this tool together with Parse Server. Tutorials for downloading setting up Android Studio, Parse Server and others can be found in the links below.

Android Studio Official Website

Android Guide With Parse Server

Webhosting Guide

 

Step 1 — Create a MailChimp Account

 

Go to MailChimp Website to create an account and proceed to fill your information as needed. If it all went well you will end on the main dashboard page.

MailChimp Dashboard

Step 2 — Create and get your API key

 

First you need to add Mandrill to your MailChimp account by clicking on your Name (on the top left corner) > Account and go to Transactional option. In this menu, click on the Add Mandrill link. A popup will appear and you can click on Start Trial to test it for free.

Settings Page

Then, you will be redirected to the following page:

Transactional Page

Click on Launch Mandrill option.

Now you should be on Mandrill main dashboard page. To create your API key, go to the Settings option and you will see the following page:

Mandrill main page

Click on + Add API Key and a key should be created for you.

Your API Key should look like the image below:
API Key

Step 3 — Set up your Sending Domain

Refer to the yellow message on the top of your page and click on Set up your sending domain, this will redirect you to the Domains page.

Domains Set Up Page

On the field yourdomain.com, you need to paste the domain of your site that is being hosted on Back4App. You can configure your domain following the  Webhosting Guide.

Click on + Add and you will be redirected to the following page:

7

Now you’re going to need to setup a DKIN and an SPF settings to your domain, in addition to verify an email domain. Back4App already have these two settings done for you, so you just need to have a verified email domain.

If you don’t have one, you can ask for through our support website chat :)

However, you can use another webhosting service to host your Domain Name System (DNS). This allows you to create and manage your own domain email, but you would need to configure manually your DKIM and SPF records, besides of have to reference your back4app site.

You can find more information at the documentations of the webhosting service that was chosen and at the Mandrill official documentation of DNS Records.

When the setup is done, the following message should show on top of the page.

Success message

Step 4 — Implement Cloud Code

For sending emails, we are going to create a function in the Cloud Code named “sendEmail” and call it from the Application.

Parse.Cloud.define("sendMail", function(request, response) {

 var mandrill = require('mandrill-api/mandrill');

 var mandrill_client = new mandrill.Mandrill('your mandril API key here');

 var message = {

 "text": request.params.text,

 "subject": request.params.subject,

 "from_email": request.params.fromEmail,

 "from_name": request.params.fromName,

 "to": [{

 "email": request.params.toEmail,

 "name": request.params.toName,

 "type": "to"

 }],

 "headers": {

 "Reply-To": request.params.replyTo

 }

 };

 // If the process should be async or not

 var async = false;

 // The name of your request. Will appear in logs for debug

 var ip_pool = "Main Pool";

 mandrill_client.messages.send({"message": message, "async": async, "ip_pool": ip_pool, "send_at": new Date()}, function(result) {

 console.log(result);

 }, function(e) {

 // Mandrill returns the error as an object with name and message keys

 console.log('A mandrill error occurred: ' + e.name + ' - ' + e.message);

 // A mandrill error occurred: Unknown_Subaccount - No subaccount exists with the id 'customer-123'

 });

});

 

After that, you just need to call this function from your App. Below, there is an example on how to do that in an Android Application.

 

    public class Mandrill extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

    Parse.initialize(new Parse.Configuration.Builder(this)

    .applicationId("your back4app app id”)

    .clientKey(“your back4app client key")

    .server("https://parseapi.back4app.com/").build()

    );


    Map<String, String> params = new HashMap<>();

    params.put("text", "Sample mail body");

    params.put("subject", "Test Parse Push");

    params.put("fromEmail", "[email protected]");

    params.put("fromName", "Source User");

    params.put("toEmail", "[email protected]");

    params.put("toName", "Target user");

    params.put("replyTo", "[email protected]");

    ParseCloud.callFunctionInBackground("sendMail", params, new FunctionCallback<Object>() {

    @Override

    public void done(Object response, ParseException exc) {

    Log.e("cloud code example", "response: " + response);

    }

    });


    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_mandrill);

    }

    }

 

After changing all the fields to match the sender and the receiver emails information, just running the application will send the email.

Email test

Conclusion

Your basic configuration for using Mandrill API is now ready. Also, you can explore the complete Mandrill Documentation.

In any doubts, don’t be afraid to ask our support team through our website chat for some help.