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.

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.

Pre requirities :

  1. Before going to this project you need to set up an amazon aws account. Amazon aws provides a free-tier for 12 months. If you don’t have click here to signup.
  2. You should already have installed Nodejs.
  3. You should have the basic understanding of the express.js framework.

Verify Email Addresses :

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 Request a Sending Limit Increase.

Go to SES Management Dashboard -> Email Addresses -> Verify a New Email Address

amazon ses - verify a new email address

 

Creating a Expressjs Project

Create a directory to hold the project

mkdir amazonSES

cd amazonSES

Now run npm init   

$ npm init

It will prompt you for several things but for now, just leave it as default.

package name: (amazonSES) 
version: 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 

Once you have done with npm init your package.json file should look like this.

{
  "name": "amazonSES",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Now install express js framework in our project.

$ npm install express --save

It will install all the dependencies and required modules of express js framework.

And finally, install the aws-sdk module. It acts as a mediator between amazon web services and your application.

$ npm install aws-sdk --save

In your project folder create a file called config.json and paste the following code

config.json

{
"accessKeyId":"", //enter your accesskeyId
"secretAccessKey":"", // enter your secretAccessKey
"region":"" // enter your region which you choose on account setup
}

To find security credentials

Go to AWS Console -> Account Name -> My Security Credentials -> Create New Access key

amazon ses - create access key

Ok, now you have set up everything let’s jump into code.

index.js

const express = require( 'express' );
const app = express();

app.get('/', (req, res) => {

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

      ses.sendEmail(
      {
        Source: from,
        Destination: { ToAddresses: to },
        Message: {
          Subject: {
          Data: 'Email from Amazon SES'
        },
        Body: {
          Html: {
                Charset: "UTF-8",
                Data: "Hey, It's a Email from amazon SES(Simple Email Service)",
          }
        }
      }
       }
        , function(err, data) {
          if(err) throw err
          console.log("Email sent:");
          console.log(data);
          return false;
        });
});

app.listen(3000, () => console.log('Example app listening on port 3000!'))

 

also read creating a basic crud application using angularjs


Leave a Reply

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