尽管console.log显示了我输入的邮件地址,但没有收件人定义的错误



我遵循了dev的教程https://dev.to/jlong4223/how-to-implement-email-functionality-with-node-js-react-js-nodemailer-and-oauth2-2h7m

我的问题是,即使我输入了用户电子邮件,console.log也会在服务器代码和电子邮件输入代码中显示正确的电子邮件。我仍然收到错误:没有定义收件人。我已经改变了我的";至";mailOptions中的参数设置为";mymail@gmail.com"这非常有效。

这是我的密码;

服务器代码

transporter.verify((err, success) => {
err
? console.log(err)
: console.log(`=== Server is ready to take messages: ${success} ===`);
});


app.post("/send", function (req, res) {
console.log(req.body.email); //shows entered mail
let mailOptions = {
from: "some@mail.com",
to: '${req.body.email}',
subject: "Message from: ",
text: "Reset password",
};

transporter.sendMail(mailOptions, function (err, data) {
if (err) {
console.log(err);
res.json({
status: "fail",
});
} else {
console.log("== Message Sent ==");
res.json({
status: "success",
});
}
});
});

提交电子邮件的代码

const [email, setEmail] = useState("");
async function handleReset(e) {
e.preventDefault();
console.log(email);
const response = await fetch("http://localhost:2525/send", {
mode: "cors",
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({ email }),
})
.then((res) => res.json())
.then(async (res) => {
const resData = await res;
if (resData.status === "success") {
alert("Message Sent");

} else if (resData.status === "fail") {
alert("Message failed to send");

}
});
}
Error: No recipients defined
at SMTPConnection._formatError
at SMTPConnection._setEnvelope 
at SMTPConnection.send 
at sendMessage 
SMTPConnection._actionAUTHComplete 
at SMTPConnection.<anonymous> 
at SMTPConnection._processResponse 
at SMTPConnection._onData 
at TLSSocket.SMTPConnection._onSocketData
code: 'EENVELOPE',
command: 'API'

非常感谢您的意见!

您应该在app.post("/send")回调函数中使用带有to参数的backticks:

to: `${req.body.email}`

最新更新