Nodemailer is arguably the most popular module for sending emails from Node.js apps. Gmail, on the other hand, is one of the worldâs two most popular email clients. With so many faithful followers, it would seem natural that you can combine both tools and send emails via Google servers. And thatâs the case indeed! Join us as weâre exploring the secrets behind Nodemailer & Gmail integration.
How to use Nodemailer with Gmail?
The installation and set up of both accounts are really simple.
Installing Nodemailer
First things first, we need to install Nodemailer. Youâll be good to go if youâre running Node.js 6.0.0+ so any version released since May 2018 will work.
You likely want to use npm:
npm install nodemailer
But Yarn package manager will also work:
yarn add nodemailer
Once Nodemailer is installed, you can use the following code to add it to your app:
const nodemailer = require('nodemailer');
or the following for ES Modules:
import nodemailer from ânodemailerâ;
Configuring a Gmail account
Weâre not going to explain how to set up a Gmail account, you likely already have more than one. Whether you use a free Gmail account or a paid Google account, you need to first configure it for use with Nodemailer.
Launch your client, then click on your profile in the top-right corner -> Google Account -> Security. Youâll see the following setting: