Tag Archives: email

Back to blog home page

Deeper Mandrill integration using AWS Lambda

Posted by on Mar 21, 2017

Mandrill is a transactional email API built by MailChimp to facilitate programmatic communications with your app’s users. While we already have some demonstration JavaScript for sending a plain message via Mandrill, supporting attachments needs a server-side Node.JS Action. Below, we’ll create a server-side Node JS action (powered by AWS Lambda) that imports the Mandrill SDK, allowing you to easily send email with attachments through Mandrill in your Backand application.

Getting Started in Backand

To get started with a Mandrill action, you’ll need to first create a new Server-Side NodeJS action in Backand. To do so, open up your app’s dashboard at https://www.backand.com, and navigate to an object that will host the action. On the Actions tab of this object, create a new “On Demand” action. Name this action, then select “Server-Side Node.JS Action” as the action’s type. Follow the documentation provided to download the Backand CLI and initialize your action locally.

Backand’s Server-Side Node.js actions are implemented as Amazon Lambda functions. These functions are built using Node.js, and deployed as you would any other Node application. Backand provides a convenient CLI for managing these actions (documented in the action description), as well as easy-to-use tools to trigger execution of the Lambda function from the REST API.

Initializing the Mandrill Action

Once you have the Server-Side Node.js action ready to go, the next step is to create an action in Mandrill. Navigate to http://www.mandrill.com/ and create a new action in the MailChimp dashboard provided when you log in. Once the action is ready, copy down the Mandrill API Key value and save it somewhere safe. This API key will be used by the Node.js action to connect and communicate with Mandrill.

Updating the Code

Next, we’ll need to update the action’s code to communicate with Mandrill. We’ll start by configuring the mandrill SDK requirements in index.js. Add these lines into index.js in your Node.js action folder structure, before the function exports.backandCallback:

Note: Be sure to replace MANDRILL_API_KEY with the value copied down from the Mandrill dashboard.

Next, we’ll modify the backandCallback function to send a message with Mandrill. Replace the contents of this function with the following code:

This code does two things:

The initial code fetches the attachment data from the server on which it is stored. If this code succeeds, it calls sendEmail with the file data provided as a Base 64 string.
sendEmail then takes this data and contacts Mandrill to send the message.

Configuring the Code

To tie the project together and finalize the above code, simply replace each of the placeholders with the appropriate value:

  • PATH_TO_ATTACHMENT_INCLUDING_FILENAME – this is the URL to the attachment you wish to send. If fetching this fails, sending the message will not succeed.
  • FILENAME_OF_ATTACHMENT – this is the file name that will be given to the attachment.
  • RECIPIENT_EMAIL – This is the email for the message’s intended recipient.
  • RECIPIENT_DISPLAY_NAME – This is the recipient’s display name.
  • SENDER – This is the email from which the message was sent.
  • EMAIL_SUBJECT – This is the subject of the email.
  • MESSAGE_BODY – This is the content of the email.

You can also use the parameters argument to the action to send additional message data, whether that data originates in your app’s database or via the API call. Once these changes are made, your server-side action is ready to deploy!

Testing and Deployment

You can debug and run the action locally using the provided debug.js file. Simply enter node debug.js on the command line to debug. Once you’ve finished your local testing, you can then deploy the action via the documentation provided in the Server-Side Action’s UI in your app’s dashboard at Backand.com – head to the Actions tab for the relevant object, and follow the instructions on using backand action deploy to deploy your code.

Calling the Action

To call the action in your client-side code, simply use the Backand SDK’s action functionality as you would any other on-demand action:

Simply replace OBJECT_NAME with the name of the object controlling your action, and ACTION_NAME with the Server-Side Node.js Action’s name in Backand. You can provide any extra information or detail using the provided parameters object – this will be passed into the parameters argument of your action.

Conclusion

Many apps need to send additional supporting documents along with their transactional messages, such as images used in the message or related files. With the above Server-Side Node.js Action, you’re given the full capability necessary to enhance your transactional messages with any attachments you see fit. Learn more about Backand’s Server-Side Node.js actions in the documentation, or make use of our previous example on sending messages without an attachment from a custom JavaScript action.