How to send email with Nodemailer using Gmail account in Node.js ?

Nodemailer is the Node.js npm module that allows to send email easily. In this article, we will cover each steps to send email using Gmail account with the help of nodemailer.

Installations: Go to the project folder and use the following command.

  • Create a package.json file.
    npm init -y
  • Install nodemailer
    npm install nodemailer -S
  • Create server.js file directly or use command
    touch server.js


  • Include the nodemailer module in the code using require('nodemailer').
  • Use nodemailer.createTransport() function to create a transporter who will send mail. It contains the service name and authentication details (user ans password).
  • Declare a variable mailDetails that contains the sender and receiver email id, subject and content of the mail.
  • Use mailTransporter.sendMail() function to send email from sender to receiver. If message sending failed or contains error then it will display error message otherwise message send successfully.






const nodemailer = require('nodemailer');
let mailTransporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: '',
        pass: '*************'
let mailDetails = {
    from: '',
    to: '',
    subject: 'Test mail',
    text: 'Node.js testing mail for GeeksforGeeks'
mailTransporter.sendMail(mailDetails, function(err, data) {
    if(err) {
        console.log('Error Occurs');
    } else {
        console.log('Email sent successfully');


Now open the link to Allow less secure apps: ON. Then use node index.js command to run the above code. It will send the email using gmail account.

  • Terminal to run code:
  • Sent mail:

Note 1: To use this code in any file we just have to import this file and call send() function.

var mail = require('./config/mailer')();

Note 2: To send HTML formatted text in your email, use the “html” property instead of the “text” property in sendMail function.

{ from:'"admin" ',
  to: "",
  subject:'GeeksforGeeks Promotion',
  html:' <p> html code </p>'

Note 3: To send an email to more than one receiver, add them to the “to” property in sendMail function, separated by commas.

{ from:'”admin” ‘,
to: ”,, “,
subject:’GeeksforGeeks Promotion’,
text:’Check out GeeksforGeeks’+’best site to prepare for interviews and competitive exams.’

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.