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