Nodemailer is one of the most popular way to send e-mails when using NodeJs. Most of the tutorials show how to set it up with Gmail account, however some of these tutorials might be outdated. Why do I think this ?
It was this week I looked into Nodemailer and sending e-mails with my Gmail account, but I faced with some problems from Gmail side. This article is going to focus on that problem.
But first let´s set up the Nodemailer in few steps.
Setting up Nodemailer on you NodeJs app
First, I assume you have already node (version 6 and above) installed. The steps are the following:
npm install nodemailer
import nodemailer from "nodemailer" or const nodemailer = required("nodemailer") - depend on you use modules or common Js.
then create the transporter in your app and the mail itself.
- Under the auth key, you find the user and pass keys. And now here comes the tricky part.It's time to talk about the Gmail account set up.
Setting up your Gmail account
Most of the older tutorials at this point guide you to your Account Settings and under the Security option you can find a Less secure apps, where you should turn this option Off. Then it is possible to use your username and password for the above mentioned auth .
BUT
"From May 30, 2022, Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password."
Therefore it is not possible to turn on or off the Less secure apps option.
Luckily, there is solution and it's called App passwords. To have this option on your account, you need to do the following steps:
Go to you Account Settings, then Security and under the Signing in to Google choose 2-steps verification.
You might have 2-steps verification already ON, then you should able to see the App passwords option under.
If the 2-steps verification is OFF, then click on it and follow the steps to have it enabled on your account. 2-steps verification means that every time you want to log in to your Account, you need to provide your password and an extra 6-digit password which is going to be sent to your phone.
After the 2-steps verification is enabled, the App passwords option should appear. By clicking on it, you can set up an app passwords for your mail. Save the 16-digit password, because this can be used for the nodemailer.
Using app password to nodemailer
I chose to make and save my e-mail (username) and the app-specific password in an .env file and then refer it in my application as a process.env.
This might not work at first. If this is the case, you need to install dotenv package (use the npm install dotenv) and then write dotenv.config() line of code in the beginning of your application.
Note
Gmail allows you to send max 500 mails per day. After that your account might get blocked. Moreover your e-mails might end up anyway in the Spam folder or just will not work perfectly because of security issues. Best option could be to set up an OAuth2 with your system.
Thank you for reading this and any feedback or advice will be highly appreciated as I am still just a codenewbie.
Top comments (2)
Super explanation. Much appreciated
But now I can't see an app passwords option. why?