Building a Serverless Web Mailer with AWS
Learn how to build a secure, scalable serverless web mailer using AWS SAM, Lambda, and Amazon SES. This guide covers dynamic email processing, CORS configuration, and API Gateway throttling to protect your application from unauthorized access and abuse.
The ability to receive, process, and send emails based on user input is a cornerstone of many web applications. From “Contact Us” forms to feedback tools, these seemingly simple features provide a direct line of communication between businesses and their users. But building them can quickly become a tangle of backend logic, infrastructure management, and scaling concerns.
Enter Dynamic Email SES, a serverless solution I’ve built to simplify this process. In this post, I’ll walk you through the thinking behind the project, the architecture, and how it works—all while keeping it approachable for developers of any level.
⚠️ A Warning to heed ⚠️
As always with any source code you may download and make use of, apply caution as a implementing this solution incorrectly may result in unwanted spam. Make sure that you consider your personal security requirements.
Also make sure that you have configured your SES service with accepted domains and verified email addresses you want to communicate with. Check out the link below for more information on Amazon Simple Email Service.
Simple Needs, Complex Solutions
Let’s set the stage: You have a website and need to implement a form that lets users get in touch with you. The form fields can vary. Sometimes it’s just a name and a message, other times it’s a detailed survey. You want the submitted data sent to your email, formatted neatly, and without worrying about scaling when the number of submissions grows.
Sounds simple, right? Yet, when you start considering spam protection, template formatting, error handling, and infrastructure, it suddenly doesn’t feel so simple.
That’s where AWS’s serverless stack comes to the rescue. Using Amazon Simple Email Service (SES), AWS Lambda, and API Gateway, I’ve created a solution that’s scalable, cost-effective, and—most importantly easy to extend.
Dynamic Email SES
Dynamic Email SES is a Lambda function designed to process form submissions dynamically. It uses SES to send emails, creating and deleting email templates on the fly based on the submitted form data.
Here’s the architecture in a nutshell:
- API Gateway: Accepts form submissions as POST requests.
- AWS Lambda: Processes the form data, generates an email template, and sends the email using SES.
- Amazon SES: Handles the actual email delivery, ensuring reliability and compliance.
How It Works
1. Form Submission
Users fill out a form on your website, which is configured to send a POST request to the API Gateway endpoint. The form data is URL-encoded, ensuring compatibility with the Lambda function.
2. Dynamic Template Creation
The Lambda function parses the form data and generates both an HTML and plain-text version of the email. These are used to create an SES template dynamically, ensuring each submission is neatly formatted.
3. Email Sending
SES sends the email using the generated template, delivering it to the recipient configured in the Lambda environment variables.
4. Cleanup
After the email is sent, the Lambda function deletes the SES template, keeping the SES environment tidy.
Why Serverless?
You might wonder: Why bother with Lambda and SES? Couldn’t I just handle this on a traditional server?
Sure, but the serverless approach offers several advantages:
- Scalability: Whether it’s one form submission or a thousand, the architecture scales seamlessly.
- Cost-Effectiveness: You only pay for the resources you use, which makes this ideal for low-traffic or bursty workloads.
- Maintenance-Free: No servers to manage, no patches to apply—just deploy and focus on your application.
Real-World Use Case
Imagine a “Contact Us” form on a small business website. Dynamic Email SES processes each submission, formatting the data into a neat email delivered to the business owner. The form could be as simple as:
<form action="https://your-api-endpoint.amazonaws.com/send" method="POST">
<input type="text" name="name" placeholder="Your Name" required>
<input type="email" name="email" placeholder="Your Email" required>
<textarea name="message" placeholder="Your Message" required></textarea>
<button type="submit">Send</button>
</form>
With just a few lines of HTML and no server maintenance, you’ve implemented a robust, scalable web mailer.
Getting Started
Dynamic Email SES is built using the AWS Serverless Application Model (SAM), making it easy to deploy and customize. Here’s a quick rundown of the steps:
- Clone the Repository:
git clone https://github.com/alanblockley/dynamic-email-ses
cd dynamic-email-ses
- Deploy the Solution:
sam build
sam deploy --guided
- Integrate the Form:
Point your website’s form action to the deployed API Gateway endpoint, and you’re good to go.
Final Thoughts
Dynamic Email SES is more than just a tool—it’s an example of how serverless technologies can simplify traditionally complex tasks. It’s lightweight, extensible, and ideal for any project that needs to process form submissions without the overhead of traditional infrastructure.
If you’re interested, check out the sample forms in the project repository and give it a try. Let me know how you use it, I’d love to hear your feedback!
Happy coding! 🚀