This project provides a serverless solution to automatically forward emails received on AWS Simple Email Service (SES) to a specified email address, using AWS Lambda and S3 for processing and storage. It's particularly useful for handling emails on a custom domain without setting up a full email server.
Any email to any possible address on your domain will be redirected to the email address specified in Lambda in the form in which it was sent. You will receive a letter from the address to which it was sent. The subject of the email will indicate the original sender. It is very convenient and does not require creating additional aliases and you can extend the message processing logic in Python.
It is very important not to create a bucket in advance. It will need to be created when you create a rule for processing incoming messages in SES. Otherwise, you may receive an error when adding processing for saving emails to S3
You may be wondering why you need to save emails to S3 at all? It would be good, but unfortunately the contents of the letter do not come to the Lambda event, there is only metadata.
![Screenshot 2024-03-12 at 22 47 25](https://private-user-images.githubusercontent.com/4953963/312297417-baeb15a9-761d-42b1-b117-61ac1562260f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1NzQxOTYsIm5iZiI6MTcyNDU3Mzg5NiwicGF0aCI6Ii80OTUzOTYzLzMxMjI5NzQxNy1iYWViMTVhOS03NjFkLTQyYjEtYjExNy02MWFjMTU2MjI2MGYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMDgxODE2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YTJmOTAwMDExYWRmOWQ3MzMzM2I0ODA2OWZlZWY1ZGY0ZjNiOGJjN2MzMmUxMzkwNDg0OTc3OWFlNGM0MDY2MCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.9xsplV00fDo1Fbrx6gNqzv4Qa2ZpzaM4vK54M_TebqM)
![Screenshot 2024-03-13 at 13 36 45](https://private-user-images.githubusercontent.com/4953963/312565156-b98778e9-84ca-4546-9fc8-d53d386c6ff5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1NzQxOTYsIm5iZiI6MTcyNDU3Mzg5NiwicGF0aCI6Ii80OTUzOTYzLzMxMjU2NTE1Ni1iOTg3NzhlOS04NGNhLTQ1NDYtOWZjOC1kNTNkMzg2YzZmZjUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMDgxODE2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MDE0NmNlYzNkZDgxMDgwYmFkNDM1NjNkOGRkMzAyM2Y0MWMwZTgwZDRlM2ViYzgyYjk2ZTUyYmJhYjk0MmQ4ZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.zBGHvGUka9cOBhL0cVz8d_Y-nOjewVoOk4AS3y9Sl1c)
- An AWS account
- A verified domain in AWS SES
- AWS CLI installed and configured (optional, but useful for deployment)
Before you begin, ensure your domain is verified in AWS SES. This process involves adding DNS records to your domain's DNS settings. Follow the AWS SES documentation for domain verification.
Create an S3 bucket where incoming emails will be stored. Note down the bucket name as it will be used in the Lambda function and SES rules.
The lambda_function.py contains the logic to read emails from S3 and forward them to your specified address. Deploy this Lambda function in the AWS Lambda console or use the AWS CLI.
aws lambda create-function --function-name EmailForwarder \
--runtime python3.12 --role <ROLE\_ARN> \
--handler lambda\_function.lambda\_handler \
--zip-file fileb://path/to/your/lambda/package.zip
Replace <ROLE_ARN> with your Lambda execution role ARN and adjust the file paths as necessary.
In the SES console, create a receipt rule for your domain to store incoming emails to the S3 bucket and trigger the Lambda function.
For Lambda functions triggered by SES, the event object structure looks something like this:
{
"Records": [
{
"eventSource": "aws:ses",
"eventVersion": "1.0",
"ses": {
"mail": {
"messageId": "example-id",
"source": "[email protected]",
"timestamp": "example-timestamp"
"destination": [
"[email protected]"
],
// Other metadata fields...
},
"receipt": {
// Receipt details...
}
}
}
]
}
Example IAM Policy for S3 Access
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-email-bucket/*"
}
]
}
Ensure the Lambda execution role has permissions for ses:SendRawEmail, and s3:GetObject. Refer to the policy examples provided in the troubleshooting section of this document.
Configure the lambda_function.py script with your source and destination email addresses, and ensure the S3 bucket name is correctly set.
Once deployed, any email sent to your domain will be automatically forwarded to the specified email address. The process is automated and requires no manual intervention.
- Access Denied Errors: Ensure your IAM roles and policies are correctly configured.
- Emails Not Forwarding: Check the SES receipt rule setup and ensure the Lambda function is triggered correctly.
Contributions are welcome! Please feel free to submit a pull request or create an issue for any bugs or feature requests.
MIT