AWS
Managing Contact form data with AWS SES
March 26, 2021
2 min

Managing Contact form data with AWS Simple Email Service and AWS Lambda

Let’s create an endpoint that will allow us to handle a contact form on our website by integrate with AWS API Gateway, AWS Lambda and AWS Simple Email Service

  1. Configure Simple Email Service Properly

AWS normally places all new accounts in the AWS sandbox. Due to that you need to verify both sending and receiving emails first of all. However if you verify domain, then you can use any email from that domain as sender and receiver. For that go to Amazon SES dashboard

verify-email-address
verify-email-address

  1. Create Lambda Function

To create your first lambda function go to AWS Labmda dashbaord and hit Create function.

create-lambda-function
create-lambda-function

Here we can give name for our function and choose language or technology we going to use to write the function.

Also you can choose creating new role or existing role (if you already created a role with lambda permissions)

  1. Write labmda function

You can write lambda function as below or as per your requirement

const AWS = require('aws-sdk');
const ses = new AWS.SES();

function sendEmail(subject, message, senderEmail) {
  const params = {
    Destination: {
        ToAddresses: [
            [add your receiver email here]
        ]
      },
    Message: {
        Body: {
            Text: {
                Data: message,
                Charset: 'UTF-8'
            }
        },
        Subject: {
            Data: subject,
            Charset: 'UTF-8'
        }
    },
    Source: [add your sender email here],
  }
  return ses.sendEmail(params).promise();
}

module.exports = sendEmail;

  1. Configuer test case

Then you can write test event to trigger our lambda function as follows

test-case
test-case

Also your index.js should be as follows

index-js
index-js

  1. Create AWS API Gateway

By using API Gateway we can build an API for our Lamba function. API Gateway can handle the task of accepting and processing API calls for us.

To do that you need to go to API Gateway dashboard and hit create API.

Then you need to choose REST API and hit Build

REST-API
REST-API

You can add name for the API and hit Create API

create-api
create-api

  1. Create Resource

Then hit Actions and create resource

create-resource
create-resource

Here you can add resource path. Carefull to tick the Enable API Gateway CORS

  1. Create Method

Then again hit Actions and create method and choose POST

create-method
create-method

Here you need to add your lambda function to the API

  1. Enable CORS

Then you need to hit Actions again and choose enable cors

enable-cors
enable-cors

  1. Deploy API

Lastly you need to hit Actions again and Deploy the API

deploy-api
deploy-api

Here you can set stage for the deployment.

Then you can find URL that point this created API endpoint as follows

url
url

  1. Check API with Postman

you can make a post request to this url endpoint using postman

postman
postman

Now we can use this API Endpoint to get our website contact form data to our company support email



Tags

AWSAWS LambdaAWS SES

Related Posts

AWS
Get to Know about OTPs
March 26, 2021
2 min