Join us

How to Set up Nodemailer with Gmail

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:

Enable access. While it might not seem like the most secure thing to do, it’s required to let Nodemailer use your Gmail account for mailing purposes.

Also, make sure you complete the Captcha Enable challenge, as following only the step above might not give Nodemailer the sufficient permissions. 

Finally, if your account has two-factor authentication set up and you don’t want to disable it, you’ll need to create an “application-specific password”.

Security first!

Setting up a transporter object and a message to be sent

Since we have both tools set up, we can now add our Gmail credentials to a sendmail transporter object of this node and configure a message. See this example of Nodemailer & Gmail integration:

const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'chiefspammer@yourgreatdomain.com',
pass: 'SuperSecretPassword' // naturally, replace both with your real credentials or an application-specific password
}
});
const mailOptions = {
from: 'vindication@enron.com',
to: 'friendsofenron@gmail.com, enemiesofenron@gmail.com,
subject: 'Invoices due',
text: 'Dudes, we really need your money.'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});

Note that you can configure different types of callbacks in the last sections of the code. Instead of just getting notified when a message fails to deliver, you could, for example, receive an array including the recipient’s address along with the server response. See all the available options here.

That’s all!

To learn about limitations, Nodemailer alternatives and testing head to Mailtrap’s Nodemailer with Gmail SMTP Settings tutorial.


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

Mailtrap

Email Sandbox Service

User Popularity
807

Influence

81k

Total Hits

49

Posts