Nodemailer - 为什么我的联系表单会随机发送来自未定义的"undefined undefined undefined"消息?



我已经为我的投资组合网站建立了一个联系表单,它工作得很好,我有几个朋友用它给我发电子邮件。它有四个输入字段:名字、姓氏、电子邮件和消息正文。然而,每隔一段时间,我就会收到一封从我的账户发出的"未定义消息"的电子邮件。其他三个输入字段也都是未定义的。输入字段是必需的,因此提交的消息不能是空的。来自联系表单的消息。对不起,如果这个问题之前已经问过,我谷歌了一下,看了几篇文章,但找不到任何东西。我只是好奇为什么会发生这种情况。此外,我确实使用了gmail帐户和Oauth2认证。

这是联系人表单的HTML:

<form action="#" class="row contact-form">
<div class="col-lg-6 col-md-6 mb-3"><input type="text" placeholder="First Name"
id="inputFirstName" class="shadow form-control form-control-lg" required></div>
<div class="col-lg-6 col-md-6 mb-3"><input type="text" placeholder="Last Name"
id="inputLastName" class="shadow form-control form-control-lg" required></div>
<div class="col-lg-12 mb-3"><input type="text" placeholder="Email Address" id="inputEmail"
class="shadow form-control form-control-lg" required></div>
<div class="col-lg-12 mb-3">
<textarea name="message" id="message" rows="8"
class="shadow form-control form-control-lg" required></textarea>
</div>
<div class="text-center d-grid mt-1">

<button type="submit" class="btn btn-primary rounded-pill pt-3 pb-3">
Send Message
<i class="fas fa-paper-plane"></i>
</button> 
</div>
</form>

这就是我的服务器是如何设置nodemailer的

//Oauth Variables
const oauth2Client = new OAuth2(
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
"https://developers.google.com/oauthplayground"
);
oauth2Client.setCredentials({
refresh_token: process.env.REFRESH_TOKEN,
});
const accessToken = oauth2Client.getAccessToken();
//Contact Form
app.post("/", (req, res) => {
//Nodemailer Transporter
const smtpTransport = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: process.env.EMAIL,
accessToken: accessToken,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
},
});
// Nodemailer options
let mailOptions = {
from: req.body.email,
to: process.env.EMAIL,
subject: `Message from ${req.body.email}`,
text: `${req.body.nameFirst}  ${req.body.nameLast} n  ${req.body.message}`,
};
//Nodemailer Transporter/Sendmail
smtpTransport.sendMail(mailOptions, (error, info) => {
if (error) {
res.send("error");
} else {
res.send("success");
}
});
});

所以我使用了dnodemailer,你的代码与我的完全不同。虽然这可能是接口更改的情况,但这可能值得一试:

const email = new Email({
message: {
from: "some-bot@gmail.com",
},
preview: false,
transport: this._transport,
render: async () => html,
});
return new Promise((resolve, reject) => {
email.send({
text: html,
message: {
to: user.email,
subject: "Email Subject",
},
})
.then(resolve)
.catch(reject);
});

我创建了一个实际的Email对象,用于发送电子邮件。

相关内容

  • 没有找到相关文章

最新更新