【发布时间】:2016-04-17 16:30:24
【问题描述】:
让我们先说我是 node 和 javascript 的初学者。我有一个使用 HTML、CSS 和 ASP.NET 构建的网站。我的网站上有一个电子邮件表单设置,我想将其从 ASP.NET 转换为 Node.js,只是为了获得学习体验。我已经按照几个不同的教程来尝试让这件作品发挥作用。我一直在做空!
我已经成功地让我的 html 电子邮件表单接受数据并且节点将其拾取。但是,尝试获取节点并使用 nodemailer 以电子邮件的形式发送它,而不是太多。我注意到有几个教程使用“路线”我想避免这个选项,因为它只是让我头疼。如果有人可以解释如何做到这一点或为什么不这样做或如何不能做到?我会很感激的。这是我到目前为止的代码:
Javascript/节点:
var nodemailer = require('nodemailer');
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/contact', function(req, res) {
var mailOpts, smtpConfig;
//Setup Nodemailer transport, I chose gmail. Create an application-specific password to avoid problems.
smtpConfig = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
user: "<myUser>",
pass: "<myPassword>"
}
});
//Mail options
mailOpts = {
from: req.query.name + ' <' + req.query.email + '>',
//grab form data from the request body object
to: '<other user>',
subject: 'Website contact form',
text: req.query.message
};
smtpConfig.sendMail(mailOpts, function(error, response) {
//Email not sent
if (error) {
res.end("Email send failed");
//res.render('contact', { title: 'Raging Flame Laboratory - Contact', msg: 'Error occured, message not sent.', err: true, page: 'contact' })
//console.log("error");
}//Yay!! Email sent
else {
res.end("Email send successfully");
//res.render('contact', { title: 'Raging Flame Laboratory - Contact', msg: 'Message sent! Thank you.', err: false, page: 'contact' })
//console.log("success");
}
});
});
app.listen(8081, function() {
console.log('Server running at http://127.0.0.1:8081/');
});
HTML:
<form action="http://127.0.0.1:8087/contact" method="post">
<b>send us a quote</b>
</br>
<input type="text" name="name" id="name" value="Name">
</br>
<!--input type="text" name="bname" id="bname" value="Business Name"></br>-->
<input type="text" name="email" id="email" value="Email Address">
</br>
<textarea name="message" id="message" cols="30" rows="10">Enter detailed information here</textarea>
</br>
<input type="submit" name="Submit" id="Submit" value="send message">
</form>
【问题讨论】:
-
有人得到了什么吗?
-
这是一个完整且有效的代码:noodl.io/market/product/P201601221424994/…
标签: javascript node.js forms email nodemailer