Sending Emails using AWS, SES and SmartFlows
To send email from Lambda using Amazon SES, you need the following:
- AWS Identity and Access Management (IAM) permissions for Lambda to execute the API call.
- A verified Amazon SES identity (domain or email address).
- A Lambda function with logic for sending email via Amazon SES.
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.
Create an AWS Lambda Function
- Open the AWS Lambda Console.
- Choose Create a function.
- For Function name, enter my-email-function.
- Choose Create function.
- Paste the following into the code editor:
Copy
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
- Select Add Trigger.
- Select API Gateway from the dropdown.
- Select Create a New API from the API dropdown.
- Select the REST API template.
- Click Add.
Create an IAM Policy and Execution Role
- Open the IAM Management Console.
- In the left hand nav window, select Policies.
- Click Create Policy.
- Select the JSON tab.
- Paste this JSON policy document into the policy editor:
Copy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
}
]
}
- Click Review Policy.
- For policy name, enter my-email-policy, and click Create Policy.
- In the left hand nav window, select Roles.
- Click Create Role.
- Select AWS Services for Type of Trusted Entity and Lambda for Use Case. Click Next: Permissions.
- Select my-email-policy, and click Next: Tags.
- Add any desired tags and click Next: Review.
- Provide your role a name and click Create Role.
- Navigate to your lambda function.
- Under Execution Role, select Use an existing role and the role you just created.
Update Your SmartFlow
- Add an External Web Call action to your flow.
- Configure the action as follows:
METHOD: POST
URL OF SERVICE: URL created in API Gateway
CONTENT-TYPE: application/json
BODY:
Copy
{
"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.