Back to blog
Mar 05, 2025
5 min read

How to Setup a Project and Send Email in Node.js Using Nodemailer & Gmail

Learn how to set up a Node.js project and send emails using Nodemailer and Gmail. Step-by-step guide for seamless email integration.

Sending emails programmatically is a common requirement for modern web applications. Whether it’s for user verification, password resets, or notifications, integrating email functionality into your Node.js application can be a game-changer. In this guide, we’ll walk you through setting up a Node.js project and sending emails using Nodemailer and Gmail.

Search Description

Learn how to set up a Node.js project and send emails using Nodemailer and Gmail. Step-by-step guide for seamless email integration.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setting Up the Node.js Project
  4. Installing Nodemailer
  5. Configuring Gmail for Nodemailer
  6. Environment Variables
  7. Sending Your First Email
  8. Common Challenges and Solutions
  9. Conclusion

Introduction

Node.js is a powerful runtime environment that allows developers to build scalable and efficient web applications. One of the many tasks you might need to accomplish is sending emails. Nodemailer is a popular Node.js module that makes sending emails a breeze. In this article, we’ll guide you through the process of setting up a Node.js project and sending emails using Nodemailer and Gmail.

Prerequisites

Before we dive into the code, make sure you have the following installed on your machine:

Session vs JWT Authentication in Express.js

Setting Up the Node.js Project

First, let’s create a new directory for our project and initialize it with npm.

# Create a new directory
mkdir nodejs-email-project
cd nodejs-email-project

# Initialize a new Node.js project
npm init -y

This will create a package.json file in your project directory, which will keep track of your project’s dependencies and scripts.

Installing Nodemailer

Next, we need to install Nodemailer. You can do this by running the following command:

# Install Nodemailer
npm install nodemailer

This will add Nodemailer to your project’s dependencies.

Configuring Gmail for Nodemailer

To send emails using Gmail, you’ll need to configure your Gmail account to allow less secure apps to access it. Follow these steps:

  1. Go to your Google Account Settings.
  2. Navigate to the “Security” tab.
  3. Scroll down to the “Less secure app access” section and turn it on.

Note: If you have 2-Step Verification enabled, you’ll need to create an App Password instead of using your regular Gmail password.

Create an App Password for Gmail

This is where many developers get stuck. You’ll need to create an App Password for Gmail:

  1. Go to your Google Account Settings.
  2. Navigate to “Security” ➟ 2-Step Verification
  3. Scroll down and select “App passwords
  4. Choose “Mail” and “Other” from the dropdowns
  5. Generate the app password and save it securely

Pro Tip: Never hardcode your email credentials directly in the code. Use environment variables for security.

Environment Variables

Install dotenv for secure credential management:

npm install dotenv

Create a .env file in your project root:

EMAIL_AUTH_USER=youremail@gmail.com
EMAIL_AUTH_PASS=your-app-password

Add .env to your .gitignore file to prevent credential exposure.

SSH for Beginners: A Step-by-Step Guide to Secure Remote Access

Sending Your First Email

Now that everything is set up, let’s write the code to send an email. Create a new file called index.js in your project directory and add the following code:

const nodemailer = require('nodemailer');

// Create a transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
	host: "smtp.gmail.com",
	port: 465,
	secure: true,
	auth: {
		user: process.env.EMAIL_AUTH_USER,
		pass: process.env.EMAIL_AUTH_PASS,
	},
});

// Email options
let mailOptions = {
    from: {
			name: "Contact Manager", // Sender name
			address: process.env.EMAIL_AUTH_USER, // Sender address
    },
    to: 'recipient-email@example.com', // List of recipients
    subject: 'Hello from Node.js', // Subject line
    text: 'This is a test email sent from a Node.js application using Nodemailer by ExoNoob.In.' // Plain text body
};

// Send email
transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    console.error('Error sending email:', error);
  } else {
    console.log('Email sent:', info.response);
  }
});

Replace 'recipient-email@example.com' with the email address of the recipient.

To run the script, use the following command:

node index.js

If everything is set up correctly, you should see a message in the console indicating that the email was sent successfully and you should receive an email in the recipient’s inbox.

Fetch Telegram Channel/Group Data via Bot API in JavaScript

Common Challenges and Solutions

1. Authentication Errors

  • Always use App Password, not your regular Gmail password
  • Ensure 2-Step Verification is enabled
  • Double-check your credentials

2. Rate Limiting

Gmail has sending limits. For high-volume emails, consider:

  • Sending services like SendGrid
  • Multiple Gmail accounts
  • Professional email services

Conclusion

Congratulations! You’ve successfully set up a Node.js project and sent an email using Nodemailer and Gmail. This is just the beginning—Nodemailer offers a wide range of features, including HTML Email Templates, attachments, and more. Explore the Nodemailer documentation to unlock its full potential.

By following this guide, you’ve taken a significant step towards enhancing your Node.js applications with email functionality. Happy coding!