Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
How to send Attachments and Email using nodemailer in Node.js ?
Next article icon

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

Last Updated : 08 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Nodemailer is an npm module that allows you to send emails easily from the backend. In this article, we will cover the steps to send email using a Gmail account with the help of nodemailer. 

Prerequisites:

  • NPM and NodeJS
  • ExpressJS

Approach

To send email with Nodemailer using gmail

  • Import the nodemailer module.
  • 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.

Steps to Create the Node Application

Step 1: Initialise the nodejs with the following command to create a package.json file:

npm init -y

Step 2: Installing required modules

npm install express nodemailer -S

Project Structure:

NodeProj

The updated dependencies in package.json file will look like:

"dependencies": {
"express": "^4.18.2",
"nodemailer": "^6.9.8",
} 

Example: This example uses nodemainler in node js to sent emails uing the gmail account.

JavaScript
// Filename - index.js  const nodemailer = require('nodemailer');  let mailTransporter =     nodemailer.createTransport(         {             service: 'gmail',             auth: {                 user: '[email protected]',                 pass: '*************'             }         }     );  let mailDetails = {     from: '[email protected]',     to: '[email protected]',     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 https://myaccount.google.com/lesssecureapps to Allow less secure apps: ON. Then use node server.js command to run the above code. It will send the email using gmail account. 

Type the following command in 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.

const mail = require('./config/mailer')();
mail.send();

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: "[email protected]",
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: " [email protected], [email protected], [email protected] ",
subject: 'GeeksforGeeks Promotion',
text: 'Check out GeeksforGeeks' + 'best site to prepare for interviews and competitive exams.'
}

Google now have come up with a concept of “Less Secure Apps” which is any application which accepts password as plain text. So to use this the application must have OAuth2 authentication.



Next Article
How to send Attachments and Email using nodemailer in Node.js ?

P

pratyushranjan14
Improve
Article Tags :
  • Node.js
  • Web Technologies
  • Node.js-Basics
  • Node.js-Misc

Similar Reads

  • How to send Attachments and Email using nodemailer in Node.js ?
    For this purpose, we will use a package called nodemailer. It is a module that makes email sending pretty easy. For using it, you will need to install by using the following command:  $ npm install nodemailer Features of nodemailer module:  It has zero dependencies and heavy security.You can use it
    2 min read
  • How to write e-mails in HTML and send it using Gmail ?
    In this article, we are going to learn that how users can write e-mails in HTML format and send them using Gmail. However, Gmail doesn't offer an HTML editor still we can send HTML templates in an e-mail using some tools and methods. Many people need to send an e-mail with HTML templates to others.
    3 min read
  • How to Send Email using 'git send-email' via Gmail?
    Email communication is a crucial aspect of collaborative software development. Whether you're sharing patches, discussing issues, or coordinating with your team, knowing how to send emails directly from your Git repository can be highly efficient. In this article, we will guide you through the proce
    3 min read
  • How to Send Email using Mailgun API in Node.js ?
    Sending an email is an essential part of any project and it can be achieved by using Mailgun API. It is very popular for sending emails. Features of Mailgun: .  It is easy to get started and easy to use.It is a widely used and popular module for sending emails.Mails can also be scheduled. Installati
    2 min read
  • How to Send Email using NodeJS?
    Sending emails programmatically is a common requirement in many applications, especially for user notifications, order confirmations, password resets, and newsletters. In this article, we will learn how to build a simple email-sending system using NodeJS. We will use Nodemailer, a popular module for
    5 min read
  • How to Authenticate with Google using Firebase in React Native Application ?
    In this article, we will discuss how can we authenticate with Google using Firebase in React Native. Creating React Application And Installing ModuleCreate a React app using the following command: npx react-native@latest init AwesomeProjectProject StructureNow install required npm packages: npm i @r
    2 min read
  • How to authenticate with google using firebase in React ?
    The following approach covers how to authenticate with Google using firebase in react. We have used firebase module to achieve so. Creating React Application And Installing Module: Step 1: Create a React myapp using the following command: npx create-react-app myappStep 2: After creating your project
    3 min read
  • How to Implement ACL with Passport using Node.js ?
    Nodejs is an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications. Passportjs: Passport is authentication middleware for Node.js. Extremely flexible and modular, Passport can be unobtrusively dropped in to any Express-based web application. A compr
    8 min read
  • How to send email verification link with firebase using ReactJS?
    Email verification is a crucial step in user authentication and account security. It ensures that users provide a valid email address and allows you to verify their identity before granting them access to certain features or functionalities. Firebase, a powerful platform for building web and mobile
    3 min read
  • How to get emails using PHP and IMAP ?
    Reading emails from the Gmail account using PHP will be an enriching task for web developers for its simplicity of code through IMAP (Internet Message Access Protocol). Sometimes there can be a requirement in web projects or from a client that needs the complete management of inbox emails or access
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences