Reputation: 73
I tried to connect nodemailer to gmail. More specifically I want to connect the contact form with the backend. Despite numerous attempts to connect both services I keep getting ERROR. A message appears that the login and password do not match or are not accepted. I logged in to the browser and everything works. I followed carefully the directions for connecting nodemailer to gmail. It's about allowing the gmail to connect to less secure applications. I have turned off two-factor authentication in gmail. Unfortunately it still didn't help. My code below.
ContactForm.js
import React, { useState } from "react";
const ContactForm = () => {
const [status, setStatus] = useState("Submit");
const handleSubmit = async (e) => {
e.preventDefault();
setStatus("Sending...");
const { name, email, message } = e.target.elements;
let details = {
name: name.value,
email: email.value,
message: message.value,
};
let response = await fetch("http://localhost:5000/contact", {
method: "POST",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify(details),
});
setStatus("Submit");
let result = await response.json();
alert(result.status);
};
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="name">Name:</label>
<input type="text" id="name" required />
</div>
<div>
<label htmlFor="email">Email:</label>
<input type="email" id="email" required />
</div>
<div>
<label htmlFor="message">Message:</label>
<textarea id="message" required />
</div>
<button type="submit">{status}</button>
</form>
);
};
export default ContactForm;
Index.js
const express = require("express");
const router = express.Router();
const cors = require("cors");
const nodemailer = require("nodemailer");
const app = express();
app.use(cors());
app.use(express.json());
app.use("/", router);
app.listen(5000, () => console.log("Server Running"));
const contactEmail = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: "*********@gmail.com",
pass: "**********",
}
});
contactEmail.verify((error) => {
if (error) {
console.log(error);
} else {
console.log("Ready to Send");
}
});
router.post("/contact", (req, res) => {
const name = req.body.name;
const email = req.body.email;
const message = req.body.message;
const mail = {
from: name,
to: "",
subject: "Contact Form Submission",
html: `<p>Name: ${name}</p>
<p>Email: ${email}</p>
<p>Message: ${message}</p>`,
};
contactEmail.sendMail(mail, (error) => {
if (error) {
res.json({ status: "ERROR" });
} else {
res.json({ status: "Message Sent" });
}
});
});
After running server with Node.js
Server Running
Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials w4sm11053143qkd.94 - gsmtp
at SMTPConnection._formatError (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:774:19)
at SMTPConnection._actionAUTHComplete (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:1513:34)
at SMTPConnection.<anonymous> (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:540:26)
at SMTPConnection._processResponse (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:932:20)
at SMTPConnection._onData (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:739:14)
at TLSSocket.SMTPConnection._onSocketData (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:189:44)
at TLSSocket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at TLSSocket.Readable.push (internal/streams/readable.js:223:10) {
code: 'EAUTH',
response: '535-5.7.8 Username and Password not accepted. Learn more at\n' +
'535 5.7.8 https://support.google.com/mail/?p=BadCredentials w4sm11053143qkd.94 - gsmtp',
responseCode: 535,
command: 'AUTH PLAIN'
}
Upvotes: 1
Views: 2304
Reputation: 71
the google less-secure is no longer available. but u can use two-Step Verification ! after Verifying Your Account u can add nodemailer to the App-passwords and set the 16-digit code to the createTransport auth
Upvotes: 2
Reputation: 61
After you turn of two-factor authorization, you also need to turn of the settings in your google account to allow less-secure apps. You can do it from https://myaccount.google.com/lesssecureapps
Then, it will give you a randomly generated password to use with Nodemailer. Your actual password doesn't work.
Upvotes: 2