Automate Incoming Email Processing with Amazon SES & AWS Lambda
![](https://www.qloudx.com/wp-content/uploads/2020/07/Blog-19-Software-Development.jpg)
If you use Amazon SES to receive email, it’s probably not the kind of email that you want to end up in someone’s personal inbox to be manually read using a traditional email client. The most common use case for receiving mail in SES is to handle it automatically. Emails like contact@example.com or unsubscribe@example.com fall in this category. This article describes how to set up SES to receive email & process it automatically.
Step 1 — Create a Rule Set
Start by creating a rule set as shown below:
![](https://www.qloudx.com/wp-content/uploads/2020/07/Screenshot-2020-07-08-at-15.04.36.png)
![](https://www.qloudx.com/wp-content/uploads/2020/07/Screenshot-2020-07-08-at-15.08.32.png)
![](https://www.qloudx.com/wp-content/uploads/2020/07/Screenshot-2020-07-08-at-15.12.13.png)
You can also create an S3 bucket from the above screen, which will also add a bucket policy to that bucket to allow SES to write to it. But if you select an existing bucket, remember to add a bucket policy to it to allow writes from SES.
The idea here is to first write all incoming emails to S3, then run a Lambda function to fetch those emails from S3 & process them.
![](https://www.qloudx.com/wp-content/uploads/2020/07/Screenshot-2020-07-08-at-15.13.29.png)
Step 2 — Create the Lambda Function
Next, create the Lambda function to fetch the email from S3 & process it as needed. Here’s a sample:
var AWS = require('aws-sdk');
var S3 = new AWS.S3();
exports.handler = function (event, context, callback) {
S3.getObject({
Bucket: 'my-bucket',
Key: event.Records[0].ses.mail.messageId
}, function (err, data) {
if (err) callback(err);
else {
// process email (data.Body) here
callback(null, null);
}
});
};
About the Author ✍🏻
![](https://www.qloudx.com/wp-content/uploads/2020/01/Harish-KM.png)
Harish KM is a Principal DevOps Engineer at QloudX. 👨🏻💻
With over a decade of industry experience as everything from a full-stack engineer to a cloud architect, Harish has built many world-class solutions for clients around the world! 👷🏻♂️
With over 20 certifications in cloud (AWS, Azure, GCP), containers (Kubernetes, Docker) & DevOps (Terraform, Ansible, Jenkins), Harish is an expert in a multitude of technologies. 📚
These days, his focus is on the fascinating world of DevOps & how it can transform the way we do things! 🚀