Sending Emails in Node/Express.js with Nodemailer + Gmail (Part 1)

Elijah Echekwu
4 min readAug 3, 2024

--

Nodemailer

Sending emails is one of the crucial feature of any modern web application. Most web applications send mails to their users for various functions such as verification (OTP or activation link), password reset or recovery, updates on a new product or services, changes in a user account, login authorization on a new device and the list goes on.
if you application runs on the Nodejs runtime, then nodemailer is the one tool you’ll need in your journey building applications.

Nodemailer is a popular module in Nodejs application used for sending emails. It provides a simple and powerful API to interact with email servers, allowing you to send, schedule, and manage emails within your Node.js applications.

Nodemailer supports the following key features:

  • SMTP Transport: Supports SMTP (Simple Mail Transfer Protocol), which is the most common method for sending emails.
  • OAuth2 Authentication: Allows secure authentication using OAuth2.
  • HTML Emails: Supports sending HTML-formatted emails.
  • Attachments: Allows including attachments with emails.
  • Templating: Integrates with templating engines like EJS to send dynamic content.

In this setup, we would setup a mailing service for our application using nodemailer with Gmail as the transport object.

Setting up Mailing Service

To create a mailing service using Nodemailer, you need to create a transporter object that defines how emails will be sent. This includes specifying the service provider (e.g., Gmail), authentication details, and other optional configurations. In this setup, we would use Gmail as our transport.

Gmail as a Transport

When using Gmail as the transport for sending emails with Nodemailer, you are essentially using Gmail’s SMTP server to handle the delivery of your emails. If you are using a regular Gmail account, you might need to allow “less secure apps” or generate an app-specific password for better security.

To set that up, login to the google account you want to use to send the mails
Google Account . click on “Security”

Next, setup 2- step verification on your account, if it is not active

Next, click on the search bar and search “App Password”

search app password

Create an app and store the password safe (we’ll need it later)

nodemailer app password

Installing and Setting up nodemailer

Install nodemailer, nodemailer requires a Nodejs v6.0.0 or newer.

npm install nodemailer dotenv

Store your app password and other credentials in a .env file

SMTP_PASS = "your-app-pass"
SMTP_USER = "yourmail@gmail.com"
SMTP_SERVICE = "gmail"

Creating the mailing service

Creating the mailing service using nodemailer involves three main steps

  1. Create a Transporter object
  2. Create a MailOptions Object
  3. Use the Transporter.sendMail method.

Create a Transport object

const nodemailer = require('nodemailer');
const dotenv = require('dotenv');
dotenv.config();


const transporter = nodemailer.createTransport({
service: process.env.SMTP_SERVICE,
auth: {
user: process.env.SMTP_MAIL,
pass: process.env.SMTP_PASS
}
});

Create mailing options and send email

const mailOptions = {
from: process.env.SMTP_MAIL,
to: 'recipient-email@example.com',
subject: 'Test Email',
text: 'Hello, this is a test email sent using Nodemailer and Gmail.'
};

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

Everything in a single file mail.js

const nodemailer = require('nodemailer');
const dotenv = require('dotenv');
dotenv.config();

const transporter = nodemailer.createTransport({
service: process.env.SMTP_SERVICE,
auth: {
user: process.env.SMTP_MAIL,
pass: process.env.SMTP_PASS
}
});

const mailOptions = {
from: process.env.SMTP_MAIL,
to: 'recipient-email@example.com',
subject: 'Test Email',
text: 'Hello, this is a test email sent using Nodemailer and Gmail.'
};

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

Run the script

node mail.js
// Expected response: Email sent:

Explanation:

  1. Create a Transporter Object: The createTransport method is used to create a transporter object. By specifying the service as 'Gmail', you instruct Nodemailer to use Gmail's SMTP server. The auth object contains the email address and password for authentication.
  2. Send an Email: The sendMail method is used to send an email. The mailOptions object specifies the sender's address, recipient's address, subject, and email body. The callback function block handles the response or any errors that occur during the process.

This setup is ideal for small-scale applications or development environments. In the upcoming post, “Implementing various application features with nodemailer”.

Thanks for reading.

--

--

Elijah Echekwu

ML || BackendDeveloper(Node.js) || STEM Tutor (Robotics & Electronics) at Inventib Limited. https://www.linkedin.com/in/elijah-echekwu-7b877019a