Send Email by using nodejs / nestjs API, nodemailer & AWS SES

Kingyinma
3 min readJan 11, 2021

First Part is about AWS SES setup guileline

Go to AWS Console, enter SES Home

https://ap-southeast-1.console.aws.amazon.com/ses/home?region=ap-southeast-1#

Verify domain

Go to Domains > Verify a New Domain button, enter your domain, it shows picture below and you can use these information by input to your DNS management of this domain, e.g. cloudflare

Normally, within 24 hours or less, AWS SES will finish verify your domain and go back to SES console page, status update to verified

Next, verify an email, input an email address you need to work with AWS SES

Check your mailbox of this address, just click the link to verify this email address

then go back to SES console, this email address status becomes verified

But it is not finish configuration yet, as your SES account is still in sandbox environment, you can check by navigate to > Sending Statistics

Moving out of Amazon SES sandbox

Navigate to SMTP Setting, click Create My SMTP Credentials button

These section is going to introduce how to implement simple nodejs api with nodemailer and ses

first we create a iam database in mongodb, which store the credential of ses (and other credential in the future)

then you need to store your sample data to database.

If you want to make your data more secure, you can encrypt the credential and save to database and decrypt it before use

public async sendMail(sendMailModel: SendMailModel) {    // get IAM    const iam = await this.iamService.findOne(
{
subType: 'ses', }, { credential: {
// true / false depends you want to decrypt
decrypt: true, fields: ['accessKeyId', 'secretAccessKey'], }, },
...
);

and also we create a Mailer module to handle send email feature

and also create a simple restful API for sending email

Run the API locally and use postman to test it

Here is the response

The sample code is here, hope this help you understand how AWS SES works and help you add sending email feature by node js. Thanks

and check your email, you can also well receive an email

--

--