Sending Emails with AWS SES and Nodemailer in Node.js

Sending emails is a common task in web applications, and various services are available to handle this.

You probably heard of SES (Simple Email Service) if you are an AWS user. If not, SES is a popular and scalable email-sending service by Amazon.

This tutorial will demonstrate how to set up and use AWS SES with Nodemailer in a Node.js project to send emails.

A letter form AWS

Prerequisites

  • An AWS account
  • Node.js and npm installed on your machine

Step 1: Set up AWS SES

  • Sign in to your AWS account.
  • Go to the SES Console: https://console.aws.amazon.com/ses/.
  • Choose the region you want to use for SES (I'm using eu-west-1 in this example so make sure you swap your region in).
  • Verify the email address or domain you'll be sending emails from and the domain or emails you will be sent to for testing.
  • Optional step (not required for the demo). Request production access to increase your sending limits, if needed (this lifts the restriction on the number of emails you can send and the emails you can send to).

Step 2: Install the required packages

Run the following command in your project folder:

npm install nodemailer @aws-sdk/client-ses dotenv

Step 3: Configure AWS credentials

Create a new IAM user with AmazonSESFullAccess permission, and obtain its AccessKeyId and SecretAccessKey.

Set the AWS credentials using environment variables or an AWS credentials file. For environment variables, set ACCESS_KEY and SECRET_ACCESS_KEY in your .env file or your server environment.

For this example I'll use a .env file.

Example of a .env file:

ACCESS_KEY=YOUR_ACCESS_KEY_ID
SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY

Step 4: Set up Nodemailer with AWS SES

Create a new file, e.g., sendMail.mjs, and import the required packages and configure Nodemailer with AWS SES. I'm using a .mjs file extension so that I can use the import syntax with Node.js for testing it.

// sendMail.mjs
import nodemailer from "nodemailer";
import * as aws from "@aws-sdk/client-ses";
// So we can use .env variables locally import dotenv
import * as dotenv from 'dotenv' 

dotenv.config()

const ses = new aws.SES({
  apiVersion: "2010-12-01",
  region: "eu-west-1", // Your region will need to be updated
  credentials: {
    accessKeyId: process.env.ACCESS_KEY,
    secretAccessKey: process.env.SECRET_KEY,
  },
});

// create Nodemailer SES transporter
const transporter = nodemailer.createTransport({
  SES: { ses, aws },
});

const sendMail = () => {
  // send mail
  transporter.sendMail(
      // mail options
      {
        from: "test@codu.co", // replace with your own address
        to: "receiver@codu.co", // replace with your own address
        subject: "Testing my Nodemailer/SES setup",
        text: "This is a message to say you did it!",
      },
      // callback
      (error, info) => {
        if (error) {
          console.error('Error sending email:', error);
        } else {
          console.log('Email sent:', info.messageId);
        }
      }
    );
};

export default sendMail;

Replace the from/to email addresses, region, and email content with your own. Run the sendMail function directly from the CLI with node.js to test it:

node -e "import('./sendMail.mjs').then(module => module.default());"

When you run this command you should see something like:

Email sent: <010201873dc22b3e-2la50757-b945-4be4-82ae-4593b772da0e-000000@eu-west-1.amazonses.com>

Note: The -e flag lets us write JavaScript in the command line. This just lets me write the file in a way that's easy for you to import into any other file in your application and start sending mail.


This will send an email using AWS SES and Nodemailer. You can adjust the code according to your specific requirements.

Check out the nodemailer docs here for more information on creating more sophisticated emails.


Let me know below if this helped or if you run into any issues. 👇

NodejsSesAwsTutorialNodemailer
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses; Lead Developer, Software Architect, Product Manager, CTO and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.