我正在尝试使用Nodemailer和Gmail为我的网站实施联系表格,但不幸的是出了点问题。我找不到问题。以下是我的发布路线:
app.post('/form', cors(), (req, res, next) => {
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: 'some.mail@gmail.com',
pass: 'password'
},
tls: {
rejectUnauthorized: false
}
});
const htmlEmail = `
<h3>Contact Details</h3>
<ul>
<li>Name: ${req.body.author}</li>
</ul>
<h3>Message:</h3>
<p>${req.body.body}</p>
`
let mailOptions = {
from: req.body.author,
to: 'some@mail.com',
replyTo: req.body.author,
subject: "Portfolio Website - Contact",
text: req.body.body,
html: htmlEmail
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
return res.json({err: error});
}
console.log('Message sent: %s', info.messageId);
res.redirect('form');
});
});
这是我的HTML表单:
<form method="POST" action="form" accept-charset=utf-8>
<div class="form-group">
<textarea name="body" placeholder="Write an e-mail here..." class="form-control"></textarea>
</div>
<div class="form-group">
<input type="text" name="author" placeholder="Author" class="form-control author" />
</div>
<div class="form-group">
<input class="btn btn-lg btn-secondary btn-block" type="submit" value="Send" />
</div>
</form>
我将不胜感激!
干杯, 席蒙(Szymon)
PS。这是GitHub上的仓库的链接: https://github.com/alojzz/alojzz.github.io/blob/master/server.js
答案 0 :(得分:0)
我遇到了同样的问题,我将Google帐户的安全性中的“缺乏安全应用访问权限”更改为“启用”。这就解决了我的问题。