How to Send Email using Gmail Account in Node.js?

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother,

This post will give you lesson of node js send email lesson. I’m going to show you about node js send email through gmail. this lesson will help you node.js send email with nodemailer. you can understand a concept of how to send a mail using node js.

In this lesson we will use nodemailer npm package for sending email. here we will use sender as google gmail account and you have to add your email and password in sender details.

so let's follow simple step to send mail with node js.

Lesson 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Lesson 2: Install nodemailer

run bellow command and install nodemailer npm package.

npm install nodemailer -S

Lesson 3: Create server.js file

server.js

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({

service: 'gmail',

auth: {

user: "your_gmail_account@gmail.com",

pass: "PASSWORD"

}

});

let message = {

from: "from@email.com",

to: "receiver_email@gmail.com",

subject: "Subject",

html: "<h1>Hello SMTP Email</h1>"

}

transporter.sendMail(message, function(err, info) {

if (err) {

console.log(err);

} else {

console.log(info);

}

})

now you can simply run by following command:

node server.js

Result:-

thank you very much for following up with me.

Tags

Recent Posts

Related Posts