Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

node.js - I can't send emai to my gmail in PRODUCTION

I am using nodemailer. I have some forms on my front-end and the filled data is send to my GMAIL. It is working in development. But i have problems in production.

My code looks like this:

the api route

router.post("/sendemail", async (req, res) => {
    let contactUsForm = req.body;
    if (!req.body.captcha) {
        console.log("err");
        return res.json({ "success": false, "msg": "Capctha is not checked" });
    }
    const verifyUrl = `https://www.google.com/recaptcha/api/siteverify?secret=${process.env.RECAPTCHA_SECRET_KEY}&response=${req.body.captcha}`;
    request(verifyUrl, (err, response, body) => {
        if (err) { console.log(err); }
        body = JSON.parse(body);
        if (!body.success && body.success === undefined) {
            return res.json({ success: false, message: "Recaptcha verification failed" });
        }
        else if (body.score < 0.5) {
            return res.json({ success: false, message: "You might be a bot, sorry!", score: body.score });
        } else {
            sendEmailDomain.sendMail(contactUsForm, info => {
                res.send({ success: true, message: 'Your request has been send!' })
            }).catch(err => res.send({ success: false, message: 'Something went wrong' }))
        }
    })
})

the sendMail function which should send mail if recaptcha is passed

async function sendMail(contactUsForm, callback) {
    try {
        let transporter = nodemailer.createTransport({
            host: "smtp.gmail.com",
            port: 587,
            secure: false,
            auth: {
                user: process.env.EMAIL_ACCOUNT,
                pass: process.env.EMAIL_PASSWORD
            }
        });
        let mailOptions = {
            from: contactUsForm.email,
            to: '[email protected]',
            subject: `Client Contact from - ${contactUsForm.email}`,
            html: `
                <div>Hello</div>
                
          `
        };
        let info = await transporter.sendMail(mailOptions);
        callback(info);
    } catch (err) {
        console.log('err *', err);
    }
}

when i try to send mail i get error

 Error: Invalid login: 534-5.7.14

what i tried

i googled a lot for this problem

i saw somewhere that i should go to this url:

https://accounts.google.com/b/0/displayunlockcaptcha

I get this message there:

Allow access to your Google account
As a security precaution, Google may require you to complete this additional step when signing into a new device or application.

To allow access, click the Continue button below.

and i click Continue

The problem is resolved now - can send mails in production but not for long.

I have two problems:

  • The first problem is that if i go on the same url maybe after 10 minutes i get again the same text that i should accept Continue, if i don't - again i can't send mails
  • The second problem i tried from other pc where i visited my web site - mydomain.com and there i can't send mails if i am not signed in with the gmail account that actually needs to accept the form data.

How can i solve this problems ?

A lot of users should fill the data, of course that they can't be signed in with that gmail account so they can send mail to that account :(

question from:https://stackoverflow.com/questions/65876364/i-cant-send-emai-to-my-gmail-in-production

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...