How to Send Email Using Gmail SMTP in Node Js

Reading Time: 5 minutes
4,133 Views

Inside this article we will see the concept of sending emails. We will send email using Gmail account details. Additionally we will use nodemail npm package.

Tutorial is super easy to understand and also easy to implement in your code. If you are looking of an article to send an email using Gmail in node js this tutorial is best for you to learn and understand.

While using Gmail account as a To account to send emails first you need to make sure Less secure apps should be enabled. We will understand to enable it in this article as well.

Node JS is an environment where we can deploy and execute javascript based applications. In this article we will send email using Gmail Smtp in node js.

Learn More –

Let’s get started.


Node Js Application Setup

Please make sure you have node and npm both installed. To check available versions use these commands.

To check node version

$ node -v

To check npm version

$ npm -v

Create a folder with any name say nodemail. Open this folder into terminal or command prompt.

Next, we need package.json file. Run this given command into terminal

$ npm init -y

The given command will auto generate package.json file with default values.

Next we need to create a file say server.js into node application.


GMAIL Account Settings

Go to your Google account and click on “Account”. Once you are on the “Account” page, click on “Security“. Scroll down to the bottom and you will find “Less secure app access” settings.

Set as ON.

Have a look into these images.


Install nodemailer NPM Package

Open project in terminal and run this npm command to install nodemailer.

$ npm install nodemailer

It will install nodemailer npm package to application.

Package details you can read from here.


Send Email Using Gmail

Open server.js file and write this following code into it.

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: "your_email_address@gmail.com",
        pass: "your_password"
    }
});

let message = {
    from: "your_email_address@gmail.com",
    to: "to_emaill_address@gmail.com",
    subject: "Test Mail Using Node Js",
    html: "<h1>Hi, I am from Online Web Tutor</h1>"
}

transporter.sendMail(message, function(err, info) {
    if (err) {
        console.log(err);
    } else {
        console.log(info);
    }
})

Application Testing

Open project terminal and type this command.

$ node server.js

Back to GMAIL Account,

We hope this article helped you to learn How to Send Email Using Gmail SMTP in Node Js Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more