Amazon SES (Simple Email Service) is a popular cloud-based service for sending transactional and marketing emails. It integrates well with all popular programming languages. In this tutorial, I'll explain how to use this email service for sending transactional (or even marketing) email in an Express.js/Node.js project.

Want to integrate AWS SES in Spring Boot project? Follow this tutorial for step-by-step instructions.

AWS SES Setup

To use AWS SES in your project, you must have:

  • An AWS account with full access to SES service (you need to create a support ticket to remove sandbox restrictions)
  • A verified email address or domain name you want to use for sending emails

If you need help, follow these instructions to complete the setup.

Create Node.js project

Let's create a new Node.js project and install all the required dependencies. We will be using official AWS SDK for JavaScript to call SES email service API.

$ npm init -y
$ npm install express body-parser aws-sdk --save

The -y flag will skip the questionnaire and create a default package.json file project root directory.

Sending Emails using AWS SES API

Create ses-client.js file in your project root directory with the following content:

ses-client.js

const AWS = require('aws-sdk');

const config = require('./config'); // load configurations file

AWS.config.update({
    accessKeyId: config.aws.key,
    secretAccessKey: config.aws.secret,
    region: config.aws.ses.region
});

const ses = new AWS.SES({apiVersion: '2010-12-01'});

const sendEmail = (to, subject, message, from) => {
    const params = {
        Destination: {
            ToAddresses: [to]
        },
        Message: {
            Body: {
                Html: {
                    Charset: 'UTF-8',
                    Data: message
                },
                /* replace Html attribute with the following if you want to send plain text emails. 
                Text: {
                    Charset: "UTF-8",
                    Data: message
                }
             */
            },
            Subject: {
                Charset: 'UTF-8',
                Data: subject
            }
        },
        ReturnPath: from ? from : config.aws.ses.from.default,
        Source: from ? from : config.aws.ses.from.default,
    };

    ses.sendEmail(params, (err, data) => {
        if (err) {
            return console.log(err, err.stack);
        } else {
            console.log("Email sent.", data);
        }
    });
};

module.exports = {sendEmail};

In above file, we have created a function called sendEmail which can be called from anywhere in Node.js application to send an email via SES API.

Now create config.js file in root directory and specify AWS access key, access secret and other required information.

config.js

module.exports = {
    'aws': {
        'key': 'YOUR_ACCESS_KEY',
        'secret': 'YOUR_ACCESS_SECRET',
        'ses': {
            'from': {
                // replace with actual email address
                'default': '"Example.com" <noreply@example.com>', 
            },
            // e.g. us-west-2
            'region': 'YOUR_ACCESS_REGION' 
        }
    }
};

Create server.js file

Let's create server.js file in our project root directory that will be used to run our Node.js application.

server.js

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

const sesClient = require('./ses-client');

app.use(bodyParser.urlencoded({ extended: false }));  
app.use(bodyParser.json());

app.get('/', (req, res) => {
    // call sesClient to send an email
    sesClient.sendEmail('user@example.com', "Hey! Welcome", "This is the body of email");
    
    res.send('Email is sent!');
});

app.listen(3000, () => {
    console.log('App is listening on port 3000');
});

Source code: Download the complete source code from GitHub available under MIT license.

Run the application

To execute the project, run the following command in the terminal:

$ npm run start

It will start the application server listening on port 3000 for connections. To test the solution, open the following URL in your browser window: http://localhost:3000. It is only a basic example to test the solution. If you have any question related to this tutorial, tweet me anytime.

🙌 Join the weekly newsletter and get the latest tutorials from my blog and around the internet.
No spam, ever. You can unsubscribe anytime.

Follow @attacomsian on Twitter to stay up-to-date with posts on this blog. You can also follow me on LinkedIn and DEV.


Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.