Amazon SES – How To Send EMAIL Using Amazon SES And Nodejs

Are you looking to send email using Amazon SES and Nodejs? If yes, you are in the right place. In this tutorial, we are going to demonstrate how to send email using Amazon SES and Nodejs.

This article contains tons of images and covered a lot of topics. So, here is a quick summary of what we are going to learn.

  1. Firstly, we will learn what is Amazon SES.
  2. Secondly, I will teach you how to go to the Amazon SES management console.
  3. Thirdly, we will learn how to find the security credentials of AWS.
  4. Finally, we will build a project using express.js and angular.js.

Prerequisites :

  1. Before going to this project you need to set up an Amazon AWS account.
  2. You should already have installed Nodejs.
  3. You should have a basic understanding of the Node.js and express.js framework.
  4. We are using Angular Js for front-end so, you should have familiar with Angular Js also.

What is Amazon SES?

Amazon Simple Email Service (Amazon SES) is a cloud-based email sending service designed to help digital marketers and application developers to send marketing, notification, and transactional emails.

It is reliable and cost-effective for all size of businesses which use email service to keep in contact with their customers.

It’s a pay-per-use service which means you pay only for what you use.

As I mentioned above, you must create an account in Amazon AWS. If you don’t have click here to signup. It takes only a few minutes and it provides a free-tier for 12 months.  So, you don’t have to pay anything for the basic use.

Amazon SES Management Console

Step 1: After you have created the account, go to console.amazon.com.

amazon ses management console

Step 2: Then go to the Customer Engagement Section. There click on Simple Email Service. 

amazon ses - select simple email service

Or, you can simply find the Simple Email Service link by searching.

amazon ses - search ses

By clicking on Simple Email Service, you will be redirected to the SES Management Console.

amazon ses - aws console home

Verify Email Address

If you are using Amazon free tier, you are in sandbox access. With sandbox access you can only send emails to that you have verified. To move out of the sandbox please send a Request to Amazon AWS to increase sending limit.

To send emails while you are in free tier, you need to verify each email address that you going to send to and send from.

Here is the process to verify an email address in Amazon SES.

Step 1: Go to the SES Management Console (see the above steps).

Step 2: Click on Email Addresses. By clicking on Email Addresses, you will be redirected to a new page.

amazon ses - select email addresses

Step 3: Then click on a button called Verify a New Email Address.

amazon ses - verify email address

Now you will be prompted with a model which asks Email Address.

amazon ses - verify email address model

Step 4: In the model, you have an input field. There type your email address which you want to verify. Then click on Verify This Email Address button.

amazon ses - verify email address button

Then close the model by clicking on the close button.

amazon ses - close button

Step 5: Now go to your email account and open the email sent by an AWS.

If you open that email, you will see a link. Click on the link to verify your email address with Amazon SES.

amazon ses - email account

Once you click on the link, your email address will be verified and you will be redirected to a verify success page.

amazon ses - verify success message

Ok. Now you have verified your email address.

Let’s check whether it verified or not.

To check email address is verified or not, go to  SES Management Console -> Email Addresses. 

amazon ses - verification status

If you see, the Verification Status is verified, your email address is verified.

Finding Security Credentials

Amazon AWS provides security credentials to make programming calls to AWS services. So, here is the process to find AWS security credentials.

Step 1: Go to the Amazon AWS Console.

Step 2: In the menu bar, you will see your account name. Click on your account name.

amazon ses - click on account name

 

Step 3: Now you will be displayed with a drop-down menu. In that drop-down menu options, you will see a My Security Credentials link. click on the link.

amazon ses - my security credentials

 

By clicking on My Security Credentials, you will get displayed with an alert box with a security warning message.

By default, AWS security Credentials provides full access to AWS services. If you want to secure your access, read here the IAM Best Practices guide.

amazon ses - continue to security credentials

 

Then click on Continue to Security Credentials.

Step 4: Now Click on Access keys (access key ID and secret access key).

amazon ses - access keys

 

Step 5: Now you will see a button Create New Access Key. Then click on that button.

amazon ses - create new access key

 

Step 6: Now you will be displayed with Access keysClick the link Show Access Key to see and click on the Download Key File to download Access Keys.

amazon ses - access keys download

 

I know that I dragged a lot. But, without knowing this you can’t go with the code.

Let’s go to the fun part. I mean to start writing the code for sending an email.

Building a Project

In this project, we are going to build an application to send an email to multiple recipients using Amazon SES and Nodejs.

I will explain step by step.

So let’s get started.

Step 1: Firstly, install express-generator tool.

Express-generator is a tool which will create you an express js application with the desired folders and files.

npm install express-generator -g

Step 2: Create an express application using the below command.

express SES

The above command will create an express js application along with some folders and files in the SES folder.

Note: You don’t have to create a folder. The express command will create for you.

Step 3: Install dependencies for an express app.

The express command will only create a project folder and files. But it doesn’t install the dependencies. So we need to install them manually.

Type the below command in the terminal or command prompt to install express js dependencies.

cd SES

npm install

The above command will install all the dependencies which are required to run express js application.

Step 4: Create a config.json file.

In the root of the project folder, create a file called config.json and paste the following code.

{
"accessKeyId":"your_access_key_id", 
"secretAccessKey":"your_secret_access_key", 
"region":"us-west-2" 
}

Note: In the accessKeyId and secreatAccessKey, type your access credentials.

Once you have done with the above commands your express application should look like below.

amazon ses - project tree structure

Ok. Now we have created our application. Let’s check how it works.

Run your application

Open your command prompt or terminal. Navigate to your project folder. Then type npm start to run the express js application.

Now open your browser window and type http://localhost:3000.  This is the default port address for express js application.

If everything works fine, your application should look like below.

amazon ses - express js application

 

Step 5: Install aws-sdk module.

The aws-sdk module is the middleware between Amazon AWS and Nodejs. Using this module, you can access AWS services.

Type the below command to install aws-sdk module into your project.

npm install aws-sdk

Ok. Now we have set up everything on the backend. Let’s jump into the front-end.

Front-end

By default, express js application provides a jade template engine to display views. But, we are not going to use them.

We are going to use static HTML pages along with Angular Js framework.

Follow the below steps to set up front-end.

Step 1: Create an index.html file in the public folder.

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <h1>Hello World</h1>
</body>
</html>

Now re-run the application to see the changes.

amazon ses - hello world

 

The index.html file will be loaded by default instead of index.jade.

Step 2: Add angular.min.js framework to index.html file.

You can add angular js file in two ways. You can either download or add a CDN link.

To add CDN link paste the below link in your head section of your HTML page.

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.7/angular.min.js"></script>
<!DOCTYPE html>
<html>
<head>
  <title>Sending Email Using Amazon SES and Nodejs</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.7/angular.min.js"></script>
</head>
<body>
  <h1>Hello World</h1>
</body>
</html>

Step 3: Now you need to create two more files. One is for adding styles to the application and another one is to add functionality to the application.

Create a style.css file in the stylesheets folder in the public folder.

Create a script.js file in the javascript folder in the public folder.

Now your project folder should look like below.

amazon ses - application structure

 

Step 4: Add the script.js and style.css files to the index.html page.

<!DOCTYPE html>
<html>
<head>
  <title>Sending Email Using Amazon SES and Nodejs</title>
  <link rel="stylesheet" type="text/css" href="./stylesheets/style.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.7/angular.min.js"></script>
  <script src="./javascripts/script.js"></script>
</head>
<body>
  <h1>Hello World</h1>
</body>
</html>

Now we have set up everything. Let’s dig into the code.

index.html

<!DOCTYPE html>
<html ng-app="SesApp">
<head>
  <title></title>
  <link rel="stylesheet" type="text/css" href="./stylesheets/style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.7/angular.min.js"></script>
  <script src="./javascripts/script.js"></script>
</head>
<body>
  <div ng-controller="SendEmailController" class="container">

     <form class="email_box" >

     	<div class="form_field">
     		<h1>Sending Email Using SES</h1>
     	</div>

     	<div class="form_field">
     		<label>To Email Address:</label> <br>
     		<input type="email" ng-model="email" placeholder="Type your Email Address" />
     	</div>
     	
     	<div class="form_field">
     		<label>Compose Message: </label> <br>
     		<textarea  ng-model="message"></textarea>	 		
     	</div>
     	
     	<div class="form_field">
     		<button type="button" ng-click="post();">SEND MESSAGE</button>
     	</div>

     	<div class="info_field" >
     		<span class="success_message" ng-if="successed">
     			Message sent successfully!
     		</span>
     	</div>
     </form>

    
    
  </div>
</body>
</html>

style.css

body {
  padding: 50px;
  font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
  color: #00B7FF;
}
.container{
  width: 500px;
  margin-left: auto;
  margin-right: auto;
}
input{
  width: 100%;
  height: 30px;
  font-size: 16px;
  padding: 10px;
  outline: none;
  border: 1px solid #2f5bb7;
}

textarea{
  width: 100%;
  height: 250px;
  padding: 10px;
  outline:none;
  resize: none;
  border: 1px solid #2f5bb7;
}

.form_field{
  margin-top: 20px;
}
.form_field label{
  font-size: 18px;
}
.form_field button{
  display: block;
    float: right;
    height: 40px;
    width: 200px;
    outline: none;
    border: none;
    background-color: #2f5bb7;
    color: white;
    border-radius: 5px;
    margin-left: 10px;
}
.form_field h1{
  text-align: center;
  text-decoration: underline;
}

.email_box{
  width: 500px;
  margin-left: auto;
  margin-right: auto;
}
.info_field .success_message{
  color: #107110;
    font-size: 18px;
    font-weight: bold;
}

script.js

var app = angular.module('SesApp',[]);

app.controller('SendEmailController',function($scope,$http){

  $scope.post = function(){
    console.log("posting data");

    $http.post('/sendEmail',{

      ToEmail: $scope.email,
      Message : $scope.message

    }).then(function(resp){
      if(resp){
        console.log(resp);
        $scope.successed = true;
      }
    });	
      
  }

  

});

If you run the application, the result may look like below.

amazon ses - html view

 

Ok. Now we have written code for everything on the front-end. Let’s go to the backend.

Back-end

In the root folder of your project, under the routes folder, we have two route files(index.js, user.js). But, we are not going to touch the second one.

In the index.js file, type the following code.

routes/index.js

var express = require('express');
var bodyParser = require('body-parser');
var router = express.Router();

router.use(bodyParser.json());

router.post('/sendEmail',function(req,res,next){

  var aws = require('aws-sdk');
    aws.config.loadFromPath('config.json');
    var ses = new aws.SES({apiVersion: '2010-12-01'});

    console.log(req.body.ToEmail);

    var to = req.body.ToEmail;
    var from = '[email protected]';

      ses.sendEmail(
      {
        Source: from,
        Destination: { ToAddresses: to },
        Message: {
          Subject: {
          Data: 'Email from Amazon SES'
        },
        Body: {
          Html: {
                Charset: "UTF-8",
                Data: ""+ req.body.Message +"",
          }
        }
      }
       }
        , function(err, data) {
          if(err) throw err
          return res.json(data);
        });
        
});

module.exports = router;

Now run your application.

amazon ses - email screenshot

If your email is sent successfully, you will get a Message sent successfully! message

You can add multiple recipients using comma(,) separator.

Note: The above application also tested with multiple recipients.

Below is the screenshot of sample message which is sent using Amazon SES and Nodejs.

amazon ses - email message

 

Conclusion:

Today we learned what is Amazon SES and how to go to the Amazon SES management console and how to verify emails in Amazon SES. Till now the process was same whatever the technology you use for coding. But, the approach for coding is different.

If you use different server technology here is the article which will take you in the right path.

Send an Email through Amazon SES Programmatically using an AWS SDK

Finally, we build a project using Nodejs with the Express js framework and angular js as a front-end framework.

Hope you like this article, If you get anything wrong let me know in the comments.

Also, try How To Send SMS Using Amazon SNS And Nodejs

Thanks for reading!

Happy [email protected]

Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
Yes4
No1
Powered by Pixelbart

2 thoughts on “Amazon SES – How To Send EMAIL Using Amazon SES And Nodejs

Leave a Reply

Your email address will not be published. Required fields are marked *