Sending email using Amazon SES API
Email notification is one of the popular channel used for interaction. Amazon Simple Email Service (SES) API enables to send email from your application. In this post, let’s explore how to create lambda function which consume message from Amazon SNS topic and send formatted custom email to recipients.
Though Amazon SNS allows to create email subscription, it dose not have inbuilt feature to send customised email. Amazon SES allows to send customised email.
In case, your application is already using SMTP to send email, Amazon SES provides SMTP interface to integrate with your application. This post focuses on sending email using Amazon SES API only.
Let’s consider a use case. Your organisation has a fraudulent data processing application which is deployed in AWS Fargate environment. It needs to be enhanced to send email notification to support team if any error occurred in data processing pipeline. Email should contain detailed error message and custom headers.
First let’s see the best solution for the above requirements. Best practice is to decouple email code from application using SNS topic as depicted in the architecture diagram below. Here, we have to modify the application to send notification to Amazon SNS topic and create a AWS Lambda function to send email using Amazon SES API.
Let’s see how to implement the above solution.
Assume data processing application sends notification message in the following format.
First we need to modify data processing application to send notification to Amazon SNS topic.
Next create AWS Lambda function as below. It reads notification message from event records and filter error message based on message type. Then it creates custom HTML message and send email using Amazon SES boto3 API.
Here is the complete AWS CloudFormation template to automates the resource creation and AWS Lambda function deployment. You can customise it as per your need.
All new SES accounts are placed in a sandbox to protect your reputation. Accounts in sandbox has following restrictions.
- allows to send mail to verified email addresses and domains.
- allows to send mail from verified email addresses and domains.
- allows to send maximum of 200 messages per 24-hour period.
- allows to send send a maximum of 1 message per second.Sender and recipient emails address should be verified manually using console as below. Otherwise email notification will fail.
To send email to any recipient, regardless of whether the recipient’s address or domain is verified, your account should be out of the sandbox. Still you have to verify from address whether it is in sandbox or not.
To remove the accounts from sandbox, you need to edit the account details as follows and enable production access.
This post has covered how to automate sending email using Amazon SES API with AWS CloudFormation.
For more details, please refer https://aws.amazon.com/ses/faqs/.