How To Receive Email Using SES, S3, And Lambda

vaibhav kulKarni
4 min readMay 30, 2022

Introduction of SES.

SES is a Simple Email Service of Amazon. This will help you to manage your application’s email. This Service allows you to send and receive emails through your application. This article will learn how you can receive emails using SES, S3, Lambda services.

Use Case:

  • When a user sends an email to support@your-domain.com, we will store email content in the s3 bucket (instead of support you can use anything you want)

Description: To receive inbound emails using SES follow the below steps. You need to complete the below steps to work with inbound emails in your application.

Steps:

1) Verify your email domain on the SES domain section

2) Create an S3 bucket and set the policy to that bucket

3) Create Lambada function

4) Create Receipt Rule and set S3 and lambda function to receive event store in s3

5) Send a test email to check

Solution: In Detail solution to these steps is listed below. This will help you to set up or configure all the requirements to work fine with SES. We will see all the above steps in detail.

  1. Domain Verification Step

— Login to your AWS account

— Select Simple Email Service to verify your domain

— Left-hand side click on Domain menu

Domain Verification

— Click on Verify a New Domain (check the above image, after click follow the below step)

Verify New Domain

— Enter your email domain and click on verify this domain button

(It will give you Recordset, which you need to set in your Domain’s DNS settings)

Domain verification Record and Email Receiving Record

— To verify the domain you need to add a TXT record in the domain’s DNS settings and to automatically route your domain’s incoming mail to Amazon SES, add the MX record to your domain’s DNS settings

—After this step, you can see verification is pending

Domain Verification Pending

NOTE: Once your Domain’s DNS setting is done then it will show you as verified

Domain is verified

2) Create Bucket Step

— You need to create the bucket with the region

Create Bucket

— Create a bucket policy, it will hold the SES email

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowSESPuts",
"Effect": "Allow",
"Principal": {
"Service": "ses.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "<bucket-arn>/*",
"Condition": {
"StringEquals": {
"aws:Referer": "<account- ID>"
}
}
}
]
}

3) Create lambda function Step

Create Lambda

You can use any programming language I am using ruby 2.7

require 'json'require 'net/http'require 'uri'def lambda_handler(event:, context:)  ses_mail = event["Records"][0]["ses"]["mail"]  puts ses_mail  url = "<your-url>"  uri = URI.parse(url)  response = Net::HTTP.post URI(uri),  { "mail" => ses_mail['messageId']}.to_json,  "Content-Type" => "application/json"end

4) Create Receipt Rule (In SES) Step

Select SES service > (left-hand Menu ) Under Email Receiving > select Rule Sets

Select Rule Sets

— Create New Rule Set (Let’s define the rule by selecting the create rule) You can give a name to the rule set

New Rule Set

— Step 1 (in rule set)SKIP will not add any recipients click (Next Step)

Recipients: (You can skip this because currently, we are receiving email through a verified domain)

— Select Action

Select Action

Select S3 followed by the bucket which we had created in step 2

Click again on Action and select type as Lambda which we created in Step 3

— Now save the SES Rule, we are done with the SES setup.

5) Testing step:

Now You can check by sending an email to your desired email id of the registered email ID support@your-domain.com it will be stored in your S3 bucket

Additionally, You need a mail parser to parse your mail content

composer require php-mime-mail-parser/php-mime-mail-parser

sudo apt install php-cli php-mailparse

sudo service php7.0-fpm reload # <- reload it

sudo phpenmod mailparse

--

--