简体   繁体   中英

Gmail blocks Nodemailer from sending mail

I am working on deploying my Node.js app. However I am having issues with having the registration email getting sent out.

const transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: GMAIL_USER,
        pass: GMAIL_PASS,
    },
});
......
......
transporter.sendMail({
                        to: newUser.email,
                        subject: 'Confirm Email',
                        html: `Please click this email to confirm your email: <a href="${url}">${url}</a>`
                    });

This works perfectly when I try running it on local host, but as soon as I upload the files to my server and try it, google blocks the sign in attempt, and I get an email saying

Someone just used your password to try to sign in to your account. Google blocked them, but you should check what happened.

Every time, I click the button "this was me", but any future attempts still get blocked.

I have "less secure apps" enabled. Is there a way to whitelist an IP to send from my gmail? or a way to get this working in general?

You have two options.Either you set the access to less secure apps setting to Enabled or you obtain an accessToken and a refreshToken from Google OAuth2.0 and use them in your nodemailer config

if you choose option two your config for the transport will look something like this:

auth: {
    type: 'OAuth2',
    user: 'user@example.com',
    accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x'
}

Sometimes people who don't assign recovery email, phone number .In that scenario also google hinders login. In my case google first allowed me on turning less secure app but very next day it denied. So I added recovery email for that particular email and it worked. So that's how I think google security algo works.

Had to fill out some hidden captcha for gmail, then everything worked fine. Sadly dont have the link, or id post it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM