In the digital age, providing a seamless way for users to contact you through your website is essential. An automated contact form email service can streamline communication, ensuring messages are delivered promptly and reliably. AWS Lambda and Amazon Simple Email Service (SES) offer a powerful combination for building such a service with minimal infrastructure management. This guide will walk you through setting up an automated contact form email service using AWS Lambda and SES.

Introduction: The Use Case for an Automated Contact Form Email Service

A contact form email service allows website visitors to send inquiries directly to your email, enhancing user experience and maintaining organized communication. Automating this process with AWS Lambda and SES ensures scalability, cost-effectiveness, and ease of maintenance, making it an ideal solution for businesses of all sizes.

Setting Up Your AWS Account and Environment

Step 1: Create an AWS Account

If you don’t already have an AWS account, sign up at AWS.

Step 2: Configure IAM Roles and Permissions

  1. Navigate to the IAM service in the AWS Management Console.
  2. Create a new role with the following policies:
    • AWSLambdaBasicExecutionRole
    • AmazonSESFullAccess

Step 3: Verify Your Email in SES

  1. Go to the SES service.
  2. Verify the email address from which you intend to send emails.
  3. (Optional) You can request production access if you’re out of the SES sandbox.

Coding the Lambda Function: Node.js and AWS SDK

Step 1: Initialize a Node.js Project

mkdir contact-form-service

cd contact-form-service

npm init -y

npm install aws-sdk

Step 2: Create the Lambda Function

Create a file named index.js and add the following code:

const AWS = require(‘aws-sdk’);

const ses = new AWS.SES();

exports.handler = async (event) => {

    const { name, email, message } = JSON.parse(event.body);

    

    const params = {

        Source: ‘your-verified-email@example.com’,

        Destination: {

            ToAddresses: [‘recipient@example.com’]

        },

        Message: {

            Subject: {

                Data: `New Contact Form Submission from ${name}`

            },

            Body: {

                Text: {

                    Data: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`

                }

            }

        }

    };

    try {

        await ses.sendEmail(params).promise();

        return {

            statusCode: 200,

            body: JSON.stringify({ message: ‘Email sent successfully’ })

        };

    } catch (error) {

        return {

            statusCode: 500,

            body: JSON.stringify({ message: ‘Failed to send email’, error })

        };

    }

};

Deploying the Lambda Function with Serverless Framework

Step 1: Install Serverless Framework

npm install -g serverless

Step 2: Configure Serverless Framework

Create a serverless.yml file:

service: contact-form-service

provider:

  name: aws

  runtime: nodejs14.x

  iamRoleStatements:

    – Effect: Allow

      Action:

        – ses:SendEmail

        – ses:SendRawEmail

      Resource: “*”

functions:

  sendContactEmail:

    handler: index.handler

    events:

      – http:

          path: send-email

          method: post

Step 3: Deploy the Lambda Function

serverless deploy

Creating an API Gateway for email-triggering

Step 1: Configure API Gateway

Serverless Framework automatically sets up an API Gateway for you. Verify this in the AWS Management Console under API Gateway.

Step 2: Retrieve the Invoke URL and Function Path

After deploying, note the URL provided by Serverless. This will be used to trigger the Lambda function.

Testing Your Email Service with Postman

Step 1: Set Up Postman

  1. Open Postman.
  2. Create a new POST request by invoking the URL from the API Gateway.

 

Step 2: Send a Test Request

  1. Set the request body to JSON format:

{

    “name”: “John Doe”,

    “email”: “john.doe@example.com”,

    “message”: “Hello, this is a test message.”

}

  1. Send the request and verify that the email is received.

Conclusion and Further Tips

Following this guide, you have a functional contact form email service powered by AWS Lambda and SES. This setup can be further enhanced by:

  • Adding validation for input fields.
  • Implementing spam protection measures.
  • Logging and monitoring using AWS CloudWatch.
  • Configuring additional email templates for different types of inquiries.

Building a reliable and scalable contact form email service with AWS Lambda and SES provides a robust solution for managing customer communications.

References

Create Dynamic Contact Forms for S3 Static Websites Using AWS Lambda, Amazon API Gateway, and Amazon SES

Sending emails through Amazon SES using an AWS SDK