Sending Mailgun Emails from SmartFlows via AWS

Description

To send email from SmartFlows using Mailgun, you need the following:

  • AWS Identity and Access Management (IAM) permissions for Lambda to execute the API call.
  • npm and Node.JS installed on your local machine.
  • A Mailgun Account.
  • A Lambda function with logic for sending email via Mailgun API.

Note: The example Lambda function code in Node.js is provided as-is. Adapt the example to your use case, or design your own in your preferred programming language.

How to

Configure Mailgun

  1. Follow Mailgun’s Quick Start Guide. Your API Key and Domain will be needed in future steps.

Install the Mailgun dependencies locally

  1. Download the Mailgun dependency files.
  2. Create a new Node.js package.
  3. Install the Mailgun package to your new package.
  4. Add the following code to the index.js file.
var aws = require('aws-sdk');
const https = require('https'),
   config = require('./config.json'),
   mailgun = require('mailgun-js')({apiKey: config.MAILGUN_API_TOKEN,        domain: config.MAILGUN_DOMAIN})
exports.handler = (event, context, callback) => {

     data = {
             from: event.from,
               to: event.to,
               subject: event.subject,
               text: event.text
           };

    mailgun.messages().send(data, (error, body) => {
                   console.log(body);

5.  Create a .zip file with your package.

Create an AWS Lambda function

  1. Open the AWS Lambda console.
  2. Choose Create a function.
  3. For Function name, enter my-email-function.
  4. Choose Create function.
  5. Select Upload a .zip file from Code Entry Type.

6.  Upload your Mailgun Package.

Add an API Trigger

  1. Select Add Trigger.

2.  Select API Gateway from the dropdown.

 

3.  Select Create a New APi from the API Dropdown.

 

4.  Select the REST API template.

 

5.  Click ADD.

Update your SmartFlow

1, Add an External Web Call action to your flow.

 

2. Configure the action as follows:

METHOD: POST

URL OF SERVICE: Url created in API Gateway

CONTENT-TYPE: application/json

{
"toEmailAddress": "Enter the email address of the recipient",
"fromEmailAddress": " A verified Amazon SES identity (domain or email address).",
"subject": "My test email subject",
"text": "Hello from SmartFlows"
}

3. Save and Deploy your flow.

 

You can now activate your flow and an email should be generated.