Sending Email using AWS Lambda and Amazon SES

Souvik Paul
Oct 30 · 6 min read
Image for post
Image for post
Photo by Kate Macate on Unsplash

In this article, we’ll discuss the procedure to build a Lambda function that will send an email to the specified recipient from the specified sender using Amazon Simple Email Service (SES). In this project, we’ll do everything in AWS console.

Before beginning, let’s understand some key concepts.

Amazon Simple Email Service (SES)

Amazon SES is an email platform that provides an easy, cost-effective way for us to send and receive email using our own email addresses and domains.

Using SES, we can send marketing emails such as special offers, transactional emails such as order confirmations, and other types of correspondence such as newsletters. When we use Amazon SES to receive mail, we can develop software solutions such as email autoresponders, email unsubscribe systems, and applications that generate customer support tickets from incoming emails.

Amazon SES eliminates infrastructure challenges such as email server management, network configuration etc.

When we send an email, we send it through an outbound email server. The outbound email server accepts our email content, formats it according to the email standards, and then sends the email out over the Internet. The email may pass through other servers until it reaches a recipient server. The recipient server then delivers the email to the recipient.

Image for post
Image for post

When we use Amazon SES, we can do two things —

Image for post
Image for post
Image for post
Image for post

Note — If an organization already has an email server up and running, then there is no need of using Amazon SES. But let’s say

  1. If the organization is migrating its infrastructure to AWS cloud and it has a separate physical email server, then it can keep this email server and configure this to send the emails through Amazon SES. And if the organization don’t want to send the emails through SES, then also there is no problem at all.
  2. And, if the organization is a startup, then it is very convenient to use Amazon SES, as it is not required to setup a physical email server, which will be expensive.

A sender can generate the email content in different ways. A sender can create the email by using an email client application, or use a program that automatically generates emails, like an application that sends order confirmations in response to purchase transactions.

In our project, we will use a program (Lambda function) that will generate email for us.

AWS Lambda

AWS Lambda is a serverless computing service that lets the user to run code without provisioning or managing servers and the user needs to pay for how much they use. The user can also scale it up and down according to their needs.

Now we’re done with the basics, let’s jump into the project.

Step 1: First of all, we’ll create the policy document, where we’ll specify the permissions for our lambda function.

We’ll go to IAM console and click on Policies to create a new policy, which will be named as myPolicy. In the JSON Policy editor, we’ll paste the following-

{     
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
}
]
}

The above policy will give lambda the following permissions -

  1. SendEmail — It composes an email message and immediately queues it for sending.
  2. SendRawEmail —It also composes an email message and immediately queues it for sending. This operation is more flexible than the SendEmail API operation. When we use the SendRawEmail operation, we can specify the headers of the message as well as its content. This flexibility is useful, for example, when we want to send a multipart MIME email (such a message that contains both a text and an HTML version). We can also use this operation to send messages that include attachments.

Step 2: Now, we’ll attach this IAM policy to an IAM role, which will be assigned to our lambda function later.

In the IAM console, we’ll click on Roles and attach the policy named myPolicy. We’ll name this role as myRole.

Step 3: For sending email, we need to verify the sender’s and receiver’s email address.

Now, we’ll open the Amazon SES console and click on Go to identity management. There we’ll enter the email addresses of the sender and the receiver. A mail will be received by both the sender and the receiver, which they need to open and follow the steps for verification.

In this step, it is important to note the region in which we’re verifying the email addresses. The same region will be used in writing lambda function. Let’s say, we have verified the email addresses in ap-south-1 region. We’ll include the same region in lambda function.

Step 4: We’ll now open the Lambda console and click on Create new function. The name of our function will be myLambda and our runtime will be Node.js 12x. In the Permissions field, we’ll select use existing role and then choose myRole. In the code editor, we’ll write the following code and deploy it.

In the above code, we’ll import AWS SDK. The AWS SDK for JavaScript provides a JavaScript API for AWS services. We can use the JavaScript API to build libraries or applications for Node.js or the browser.

The rest of the codes are self-explanatory.

Step 5: Now, we’ll click on the Test button and then we’ll create new test template to pass information in the event object as an input to the function. Let’s give our Event name as testEvt. We’ll keep the default event object that is in JSON format and we’ll click on Create button. Please note that the content of JSON doesn’t have anything to do with our application, as it is only used as a payload to invoke the lambda function.

Step 6: Now, we’ll again click on the Test button to run the code. After execution, the Execution result will be displayed, where we can see the results of callback(null, {err: err, data: data}); and console.log(err); when the email is not sent and console.log(data); when the email is sent successfully.

Step 7: Now, we’ll open the inbox of the receiver (xyz.123@gmail.com). Hurray, we have an email in our inbox.

Image for post
Image for post

Please note that, the email says — “via amazonses.com”.

So, we have successfully completed our project.

Summary

In this article, we have understood the basics of Amazon SES, AWS Lambda and finally we’ve created a lambda function that can send an email using SES.

I want to thank Edureka for conducting a webinar on ‘AWS Lambda tutorial for beginners’ on 30th October 2020, which helped me to complete this project successfully.

References

https://docs.aws.amazon.com/lambda/index.html

https://docs.aws.amazon.com/ses/latest/DeveloperGuide/

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store