Sending Email in Node.JS with Amazon SES

Amazone Simple Email Service (SES) is a cheap and easy way to send transactional emails to your users. I use this on many projects to do “lifecycle marketing” (activity updates, reminders) and sending items like password reset. In this tutorial, we will set up Amazon SES and send a quick email.

Prereqs

Start by setting up your aws keys.
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html?shortFooter=true

Getting Setup

First let’s create a new directiory and set up a npm project.

mkdir amazon-ses
cd amazon-ses/
npm init -y

 Then, install the aws-sdk library and create a new file for our email program.

npm install aws-sdk --save
touch send-email.js

Sending the Email

We start off by configuring our AWS library and creating a new instance of AWS.SES which allows us to send the emails.

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

AWS.config.update({ region: 'us-west-2' });

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

Now, to send the email we simply use the sendEmail and pass it a list of params. We also call .promise() so we can have a promise, of course 😀

const fromEmailAddress = 'myadmin@admin.com';
const testEmailAddress = 'test@test.com';

const params = {
  Destination: {
    CcAddresses: [
      testEmailAddress,
    ],
    ToAddresses: [
      testEmailAddress,
    ],
  },
  Message: {
    Body: {
      Html: {
       Charset: 'UTF-8',
       Data: '<div>Html!</div>',
      },
      Text: {
       Charset: 'UTF-8',
       Data: 'test email',
      }
     },
     Subject: {
      Charset: 'UTF-8',
      Data: 'Test email'
     }
    },
  Source: fromEmailAddress,
  ReplyToAddresses: [
    testEmailAddress,
  ],
};

const sendPromise = SES.sendEmail(params).promise();
sendPromise.then((data) =&gt; {
    console.log(data.MessageId);
  }).catch((err) =&gt; {
    console.error(err, err.stack);
  });

The params are rather large, but it will get the job done!