This example shows how to receive an email with SES, store the email including the body on S3 and have S3 trigger a lambda function.
- Postprocess of email body.
- Create a SES verified Domain but do not setup the "Rule Set"
- Edit
serverless.yml
and choose a unique S3 bucket name but follow the normalizing Rules to allow to use the name for thebucketRef
. To keep it working use a name which contains only the characters a-z. ThebucketRef
is the constant stringS3Bucket
plus thebucket
name with the first letter uppercase. - if you change the region check if SES receiving exists in your region
In order to deploy the example, simply run:
serverless deploy
The output should look similar to:
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (2.69 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
........................
Serverless: Stack update finished...
Service Information
service: aws-node-ses-receive-email-body
stage: dev
region: eu-west-1
stack: aws-node-ses-receive-email-body-dev
api keys:
None
endpoints:
None
functions:
postprocess: aws-node-ses-receive-email-body-dev-postprocess
- Open the Amazon SES console at https://console.aws.amazon.com/ses/
- In the navigation pane, under Email Receiving, choose Rule Sets.
- Choose Create a Receipt Rule.
- On the Recipients page, choose Next Step. (Without a adding any recipients, Amazon SES applies this rule to all recipients)
- For Add action, choose S3.
- For S3 bucket,choose Enter a bucket name and select the bucket with the name you defined in
serverless.yml
- Choose Next Step
- On the Rule Details page, for Rule name, type my-rule. Select the check box next to Enabled, and then choose Next Step.
- On the Review page, choose Create Rule.
Send a test email to the receipient.
You should see a new S3 object in the bucket which contains the whole email body.
After a while, the postprocess function gets triggerd by an S3 event:
serverless logs --function postprocess
START RequestId: 695a6fa8-a711e8-ab5d-0fdb1ebfe5ea Version: $LATEST
<date> <RequestId> date: 2003T18:46:47.000Z
<date> <RequestId> subject: Test Subject
<date> <RequestId> body: Hello World
<date> <RequestId> from: Tim Turbo <[email protected]>
<date> <RequestId> attachments: []
END RequestId: 695a6fa8-a711e8-ab5d-0fdb1ebfe5ea
REPORT RequestId: 695a6fa8-a711e8-ab5d-0fdb1ebfe5ea Duration: 55.12 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 42 MB