我在尝试从联系表单发送电子邮件时遇到了一些问题。我正在使用NodeMailer模块。
我使用node.js并且我真的不明白它是如何工作的...但无论如何,我有这样的形式:
<form role="form" method="post" action="/" class="contact-form">
<input type="hidden" name="scrollPosition">
<input type="hidden" name="submitted" id="submitted" value="true">
<div class="col-lg-4 col-sm-4" data-scrollreveal="enter left after 0s over 1s" data-sr-init="true" data-sr-complete="true">
<input required="required" type="text" name="name" placeholder="Your Name" class="form-control input-box" value="">
</div>
<div class="col-lg-4 col-sm-4" data-scrollreveal="enter left after 0s over 1s" data-sr-init="true" data-sr-complete="true">
<input required="required" type="email" name="email" placeholder="Your Email" class="form-control input-box" value="">
</div>
<div class="col-lg-4 col-sm-4" data-scrollreveal="enter left after 0s over 1s" data-sr-init="true" data-sr-complete="true">
<input required="required" type="text" name="subject" placeholder="Subject" class="form-control input-box" value="">
</div>
<div class="col-lg-12 col-sm-12" data-scrollreveal="enter right after 0s over 1s" data-sr-init="true" data-sr-complete="true">
<textarea name="message" rows="12" class="form-control textarea-box" placeholder="Your Message"></textarea>
</div>
<button class="btn btn-primary custom-button red-btn" type="submit" data-scrollreveal="enter left after 0s over 1s" data-sr-init="true" data-sr-complete="true">Send Message</button>
</form>
这里是我在index.js中的代码:
app.post('/', function(req, res){
var mailOpts, smtpTrans;
//Setup Nodemailer transport, I chose gmail. Create an application-specific password to avoid problems.
smtpTrans = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
user: "me@gmail.ca",
pass: "application-specific-password"
}
});
//Mail options
mailOpts = {
from: req.body.name + ' <' + req.body.email + '>', //grab form data from the request body object
to: 'me@gmail.ca',
subject: req.body.subject,
text: req.body.message
};
smtpTrans.sendMail(mailOpts, function (error, response) {
//Email not sent
if (error) {
console.log("Failed");
}
//Yay!! Email sent
else {
console.log("Succes");
}
});
});
但是当我尝试按下按钮&#34;发送消息&#34;时,它只打开未找到404的页面。我不明白为什么。我之前从未使用过node.js,因为我和我一起工作的人想要使用它...所以我真的不知道。
还有&#34; app.js&#34;,我会在那里写这段代码吗?我有点迷失,我们试图合并3个wordpress主题,我们需要转换所有内容(因为wordpress是php ...)而且我一直试图实现联系表单2个星期......似乎没什么好想的。
有人有想法吗?非常感谢。
我在这里添加了app.js:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
//var knob = require('jQuery-Knob');
var index = require('./routes/index');
var prices = require('./routes/prices');
var projects_startup = require('./routes/projects_startup');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//app.use(knob);
app.use('/', index);
app.use('/prices', prices);
app.use('/projects_startup', projects_startup);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
再次感谢。
答案 0 :(得分:0)
好的,我看到了你的app.js文件,那里的一切看起来都不错,问题可能出在你发布表单时,浏览器正在等待一个POST页面,所以你应该这样做呈现成功页面,或者您只需重定向到原始GET页面。您应该能够在成功页面中添加一行。
smtpTrans.sendMail(mailOpts, function (error, response) {
//Email not sent
if (error) {
console.log("Failed");
}
//Yay!! Email sent
else {
console.log("Succes");
res.render('successPage')
//or you can do res.redirect('/')
}
});