gpt4 book ai didi

javascript - Nodemailer - 重定向到成功页面

转载 作者:太空宇宙 更新时间:2023-11-04 01:52:35 26 4
gpt4 key购买 nike

发送电子邮件后尝试让我的页面从node.js 和express.js 页面重定向。消息已发送,我在终端中收到 console.log() (使用 morgan 进行日志记录),但它没有将我重定向到成功页面,并且我在浏览器中没有收到 console.log 错误。它只是停止,然后我得到一个本地主机没有发送任何数据错误。以前从未使用过nodemailer,但我设法发送消息,只是在重定向到新页面时遇到问题。

谢谢!!

//成功html代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>

<h1>Success</h1>

</body>
</html>

//表单框

  <form action="/contact" id="contact-form" method="post" role="form">
<fieldset>
<label for="name">Name &#42;</label>
<input id="name" name="name" type="text" placeholder="Your name" required="required">
<label for="email">Email &#42;</label>
<input id="email" name="email" type="text" placeholder="Your email" required="required">
<label for="message">Message &#42;</label>
<textarea id="message" name="message" placeholder="Enter your message here" rows="3" required="required"></textarea>
<button type="submit">Submit</button>
</fieldset>
</form>

//成功的 HTML 路由

 app.get('/success', function(req,res){
res.sendFile(__dirname + '/views' + '/success.html');
});

//Node 邮件程序//从联系表单发布路由

app.post('/contact', function (req, res) {
let mailOpts, smtpTrans;
smtpTrans = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_PASS
}
});
mailOpts = {
from: req.body.name + ' &lt;' + req.body.email + '&gt;',
to: process.env.GMAIL_USER,
subject: 'New message from Portfolio site',
text: `${req.body.name} (${req.body.email}) says: ${req.body.message}`
};
smtpTrans.sendMail(mailOpts, function (error, res) {
if (error) {
return console.log(error);
}
else {
console.log('success');
res.redirect('/success');
}
});
});

最佳答案

想知道您是否仍然坚持这个问题,但以防万一,下面对我有用。

app.post('/contact', function (req, res) {
let mailOpts, smtpTrans;
smtpTrans = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_PASS
}
});
mailOpts = {
from: req.body.name + ' &lt;' + req.body.email + '&gt;',
to: process.env.GMAIL_USER,
subject: 'New message from Portfolio site',
text: `${req.body.name} (${req.body.email}) says: ${req.body.message}`
};
smtpTrans.sendMail(mailOpts, function (error, res) {
if (error) {
return console.log(error);
}
else {
console.log('success');
// NOT HERE res.redirect('/success');
}
});
res.redirect('/success'); // I MOVED THE REDIRECTING CODE HERE AND WORKED
});

关于javascript - Nodemailer - 重定向到成功页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49102087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com