Nodemailer works on local, but not works without displayunlockcaptcha on Netlify

I have an email verification feature through nodemailer in node server. It works on localhost, but not works on Netlify once it’s deployed. Here are my codes.

const transporter = nodemailer.createTransport({
       host: "smtp.gmail.com",
       port: 465,
       secure: true,
       auth: {
           user: "mygmail@gmail.com",
           pass: "mypassword",
       },
});

On live server, it occurs an error that - Error: Invalid login: 534-5.7.14 534-5.7.14 Please log in via your web browser and then try again. 534-5.7.14 Learn more at 534 5.7.14 Check Gmail through other email platforms - Gmail Help 195sm513587qkd.6 - gsmtp

I enabled “Less secure apps” in my google account.

And allowed https://accounts.google.com/b/0/displayunlockcaptcha as well

it worked a while, but since I cleared browser histories, it didn’t work again.

So I allowed displayunlockcaptch again, it worked.

It means I should allow displayunlockcaptcha every times.

Is there any way to keep allowing it? Or any other way?

Hey @hyla526, before moving forward, I want to confirm that this code is in a Netlify function? If it’s not, then it’s not getting deployed since, with Netlify and the Jamstack in general, there’s no server at runtime. If it is, please go ahead and send your Netlify URL and an x-nf-request-id so we can dig in further!

In the meantime, I wanted to share another post on this topic that may be helpful:

A small tip for gmail. You can make an app specific password for nodeMailer.

If you have enabled 2-factor authentication on your Google account you can’t use your regular password to access Gmail programmatically. You need to generate an app-specific password and use that in place of your actual password.

Steps:

Log in to your Google account
Go to My Account > Sign-in & Security > App Passwords
(Sign in again to confirm it’s you)
Scroll down to Select App (in the Password & sign-in method box) and choose Other (custom name)
Give this app password a name, e.g. “nodemailer”
Choose Generate
Copy the long generated password and paste it into your Node.js script instead of your actual Gmail password. (You don’t need the spaces.)

1 Like