Sending Emails using AWS, SES, and SmartFlows

To send email from Lambda using Amazon SES, you need the following:

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

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. Paste the following into the code editor:
var aws = require('aws-sdk'); 
var ses = new aws.SES({region: 'us-east-1'});

exports.handler = (event, context, callback) => {

var params = {
Destination: {
ToAddresses: [event.toEmailAddress]
},
Message: {
Body: {
Text: { Data: event.body}
},

Subject: { Data: event.subject}
},
Source: event.fromEmailAddress
};


ses.sendEmail(params, function (err, data) {
callback(null, {err: err, data: data});
if (err) {
console.log(err);
}
});
};

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.

Create an IAM policy and execution role

  1. Open the IAM Management Console.
  2. Select Policies in the left hand nav window.

3.  Click Create Policy.

 

4.  Select the JSON Tab.

5.  Paste this JSON policy document into the policy editor:

{
   "Version": "2012-10-17",
   "Statement": [
       {
           "Effect": "Allow",
           "Action": [
               "ses:SendEmail",
               "ses:SendRawEmail"
           ],
           "Resource": "*"
       }
   ]
}

6.  Click Review Policy.

 

7.  For policy name enter my-email-policy and click and click Create Policy.

 

8.  Select Roles from the left hand nav window.

 

9.  Click Create Role.

 

10.  Select AWS Services for Type of Trusted Entity and Lambda for Use Case. Click Next:Permissions.

 

11.  Select my-email-policy and click Next: Tags.

 

12.  Add any desired tags and click Next: Review.

 

13.  Provide your role a name and click Create Role.

 

14.  Navigate to your lambda function.

 

15.  Under Execution Role select Use an existing role and the role you just created.

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

BODY:

{
"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"
}

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