Step-by-Step Tutorial: Complete Guide on Sending Emails in Node.js with Amazon SES and Gmail

Posted by


Node.js is a popular runtime environment for servers, and it provides a powerful framework for building web applications. One common use case for Node.js applications is sending emails. In this tutorial, we will show you how to send emails in Node.js using Amazon SES and Gmail.

Amazon SES is a cloud-based email sending service that allows you to send and receive emails. It is a reliable and scalable service that provides high deliverability rates for your emails. Gmail, on the other hand, is a popular email service provided by Google.

In this tutorial, we will use the Nodemailer library to send emails in Node.js. Nodemailer is a powerful and easy-to-use library for sending emails in Node.js.

Step 1: Set up your Amazon SES account

The first step is to set up your Amazon SES account. You will need to create an AWS account if you don’t already have one. Once you have created an AWS account, you can set up your Amazon SES account by following the instructions on the Amazon SES website.

Step 2: Set up your Gmail account

If you want to send emails using Gmail, you will need to set up a Gmail account. Once you have created a Gmail account, you will need to enable "less secure apps" in your Google account settings. This will allow you to send emails using your Gmail account from external applications.

Step 3: Install Nodemailer

To send emails in Node.js, you will need to install the Nodemailer library. You can install Nodemailer using npm by running the following command in your terminal:

npm install nodemailer

Step 4: Create a new Node.js file

Create a new file called sendEmail.js in your Node.js project directory. This file will contain the code for sending emails using Nodemailer.

Step 5: Write the code to send emails

Now, we will write the code to send emails in Node.js using Amazon SES and Gmail. First, require the Nodemailer library and create a transporter object. Then, use the transporter object to send an email.

Here is an example code snippet that sends an email using Amazon SES:

const nodemailer = require('nodemailer');

// create transporter object using Amazon SES
const transporter = nodemailer.createTransport({
    host: 'email-smtp.us-west-2.amazonaws.com',
    port: 465,
    secure: true,
    auth: {
        user: 'your-amazon-ses-smtp-username',
        pass: 'your-amazon-ses-smtp-password'
    }
});

// send mail with defined transport object
const mailOptions = {
    from: 'your-email@example.com',
    to: 'recipient-email@example.com',
    subject: 'Test email',
    text: 'This is a test email sent using Amazon SES'
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        console.log(error);
    } else {
        console.log('Email sent: ' + info.response);
    }
});

Here is an example code snippet that sends an email using Gmail:

const nodemailer = require('nodemailer');

// create transporter object using Gmail
const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'your-gmail-username',
        pass: 'your-gmail-password'
    }
});

// send mail using defined transport object
const mailOptions = {
    from: 'your-email@gmail.com',
    to: 'recipient-email@example.com',
    subject: 'Test email',
    text: 'This is a test email sent using Gmail'
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        console.log(error);
    } else {
        console.log('Email sent: ' + info.response);
    }
});

Step 6: Run the code

Save the sendEmail.js file and run it using Node.js by running the following command in your terminal:

node sendEmail.js

Your email should be sent successfully using either Amazon SES or Gmail, depending on which code snippet you used.

In this tutorial, we have shown you how to send emails in Node.js using Amazon SES and Gmail. By following these steps, you can easily integrate email sending functionality into your Node.js applications. Good luck!

0 0 votes
Article Rating

Leave a Reply

24 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@devhere-y3h
4 days ago

How to get email status like opened, clicked from AWS and save them in database against each sent email

@brooklynStevee
4 days ago

Excellent presentation… you covered all the bases in 24 minutes… Thank you

@progressnwimuelekara2167
4 days ago

Good stuff, man. Needed an overview and you really provided that and more

@ashutoshsinghe
4 days ago

facing an error ses_Client.send is not a constructor

@glenbeumer8607
4 days ago

Good tutorial – made it simple to get started! Thanks.

@digenmore83
4 days ago

Thanks a lot 🙌

@appstuff6565
4 days ago

Hey thanks for this. Im using Nextjs and supabase and im now requiring to use a smtp, for sending sign up and reset emails only, so what do you think is the best way to do about this? Nodemailer + Amazon SES? or can I just use nodemailer or aws ses alone? I'm confused.

@naveenbisht4627
4 days ago

Thanks man, This works for me.

@moona1993august
4 days ago

You brightened my day with innocent narration :)Great tutorial, I am a lone freelancer with no team and no experience with aws much. so this video helps alot, thanks alot !!!!

@jacklo56
4 days ago

Hi, quick note, if you are facing the problem (MESSAGEREJECTED) verification i solved this by verifing
my Receiving email and sending email, and after that i was able to send an email

@Rahulkumarsharma-h6f
4 days ago

Thanks buddy it was Really helpful

@sombraerato5421
4 days ago

Email address is not verified. The following identities failed the check in region AP-SOUTH-1

@shivShaktitech
4 days ago

this wont work now aws made email and domain both manditory

@nickthiru
4 days ago

@16:42 It happens, even to the best and most experienced of us 😉 . Thanks for a great tutorial!

@yabsdubai
4 days ago

how to send bulk email?

@fremeto
4 days ago

Promise { <pending> } What that meaning?

@abhiseksahu4156
4 days ago

how to send direct email.i dont want to end verification email so what can i do

@roshanican504
4 days ago

well explained thanks alot man! do upload one where we can send OTP using aws-ses would be grateful if you do it using express or fastify, Thanks again

@PankajKumar-gz1mt
4 days ago

Hi bro if i send mail id wrong then what response will i get from it.

@naturevideos6464
4 days ago

Thanks .it was very helpful

24
0
Would love your thoughts, please comment.x
()
x