Join us
@sofiatarhonska ă» Sep 16,2022 ă» 2 min read ă» 5303 views ă» Originally posted on mailtrap.io
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.
The installation and set up of both accounts are really simple.
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â;
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!
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.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.