Receive an email, trigger a lambda function to process header
This example shows how to receive an email header with SES, trigger a lambda function, process headers or accept or reject emails.
Use-cases
- Postprocess of email header.
- accept or reject emails
Setup
- Create a SES verified Domain but do not setup the "Rule Set"
- if you change the region check if SES receiving exists in your region
- if you change the function names you will need to update the normalized function name used in the resource section - e.g. processacceptreject => ProcessacceptrejectLambdaFunction
Deploy
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 Informationservice: aws-node-ses-receive-email-headerstage: devregion: eu-west-1stack: aws-node-ses-receive-email-header-devapi keys: Noneendpoints: Nonefunctions: processheader: aws-node-ses-receive-email-header-dev-processheader
Setup SNS Email Receiving Rule for process header
- 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 lambda.
- For Lambda function, choose the lambda function with the name aws-node-ses-receive-email-header-dev-processheader you defined in
serverless.yml
- Invocation type choose Event
- 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.
Setup SNS Email Receiving Rule for accept or reject emails
Schritte 1-5 sind identisch dann:
- For Lambda function, choose the lambda function with the name aws-node-ses-receive-email-header-dev-processacceptreject you defined in
serverless.yml
- Invocation type choose RequestResponse (Lambda function will be called synchronously to control mail flow)
- 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.
Usage
Send a test email to the receipient.
serverless logs -t --function processheader
START RequestId: eada06fc-c76a-11a8-bffd389a883292 Version: $LATEST<date> <RequestId> { from: 'Tim Turbo <tim.turbo@domain.test>', to: 'ses-in@domain.test', subject: 'Testsubject', date: 'Thu, 4 Oct 2018 01:33:06 +0200' }END RequestId: eada06fc-c76a-11a8-bffd389a883292REPORT RequestId: eada06fc-c76a-11a8-bffd389a883292 Duration: 5.62 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 19 MB