我有一个 Angular 应用程序,其形式可以发出 ajax 请求。电子邮件工作正常,但无论我将回复设置为什么;我收到路径错误。我假设 Node 期望路径“/send”呈现模板或数据,但我只想要电子邮件的路径!~我为表单使用模式弹出窗口,为路径使用 Angular ui 路由。
app.get('/send', function (req, res, err) {
var cartContent = req.body.slice(1,20);
var content = tableify(cartContent);
var sendit = {
to: '*****@gmail.com',
from: '****@bluenightphoto.com',
replyTo: req.body[0].email,
subject: "CP Carpet Quote Request: " + req.body[0].fname + " " + req.body[0].lname , // REQUIRED.
html: '<b>Customer Quote</b>' + '<br>' + 'Customer Name: ' + req.body[0].fname + " " +
'<br>' + 'Message: ' + req.body[0].message + '<br>' + <b>Table</b>'+ '<br><br>' + content,
};
// Transporter refers to nodemailer and sendit is the login details(mail works fine!)
transporter.sendMail(sendit, function (req, res, err) {
if (err) {
console.log(err + "something strange...");
res.status(401).send("that's all folk's");
} else {
res.status(200).send("nuthing here");
console.log("Message sent! " );
}
transporter.close();
});
});
但我的错误处理程序总是收到 404 响应,即使电子邮件发送成功也是如此。
编辑:尝试了下面的两个解决方案,没有用。
这是angular ajax代码
var makeForm = function () {
if (mailJson[1].total !== 0) {
deferred.resolve(mailJson);
console.log('values loaded successfully again');
$http({
method : 'GET',
url : '/send',
data : mailJson,
headers : {
'Content-type' : 'application/json'
}
}).success(function () {
console.log('success!');
$scope.alertEmailOk = true;
}).error(function (err) {
console.log('there was an error indeed ' + err);
$scope.alertEmailOk = false;
});
} else {
console.log('no values!');
deferred.reject(mailJson);
}
return deferred.promise;
};
console.log('确实有错误' + err);总是触发..也许我需要发回数据?
这是我的 express 代码中的错误处理程序:
if (app.get('env') === 'production') {
// changes it to use the optimized version for production
app.use('/', express.static(path.join(__dirname, '/dist')));
// 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: err
});
});
}
最佳答案
// Transporter refers to nodemailer and sendit is the login details(mail works fine!)
我已经阅读了您的评论,但首先要确定下一部分: 来自 nodemailer
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'sender@gmail.com',
pass: 'password'
}
});
var sendIt = { //<------ i've just renamed this compare to nodemailer.com sample
from: 'sender@address',
to: 'receiver@address',
subject: 'hello',
text: 'hello world!'
};
// this part is very important !
// note the signature.
transporter.sendMail(sendIt, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
所以现在尝试将其插入您的代码中:
//app.get('/send', function (req, res, err) {
app.get('/send', function (req, res, next) {
var cartContent = req.body.slice(1,20);
var content = tableify(cartContent);
var sendit = {
to: '*****@gmail.com',
from: '****@bluenightphoto.com',
replyTo: req.body[0].email,
subject: "CP Carpet Quote Request: " + req.body[0].fname + " " + req.body[0].lname , // REQUIRED.
html: '<b>Customer Quote</b>' + '<br>' + 'Customer Name: ' + req.body[0].fname + " " +
'<br>' + 'Message: ' + req.body[0].message + '<br>' + <b>Table</b>'+ '<br><br>' + content,
};
// Transporter refers to nodemailer and sendit is the login details(mail works fine!)
// |
// |
// v
// Transporter refers to the login details and sendit to the mail options
transporter.sendMail(sendit, function (error, info) {// instead of (req, res, err) {
if (error) { // instead of err just to avoid colision name
console.log(error + "something strange...");
res.status(401).send("that's all folk's");
} else {
res.status(200).send("nuthing here");
console.log("Message sent! " );
}
transporter.close();
});
return next();
});
在你的代码中,你的问题在这里:
transporter.sendMail( sendit, function ( req, res, err) {
//transporter.sendMail(sendit, function (error, info) {
if (err) {
console.log(err + "something strange...");
res.status(401).send("that's all folk's");
// you are acting here on the 'info' coming from transporter.sendMail
// not on 'res' from app.get('/send',
} else {
res.status(200).send("nuthing here");
console.log("Message sent! " );
// you are acting here on the 'info' coming from transporter.sendMail
// not on 'res' from app.get('/send',
}
尝试在您的问题说明上方进行此更改,我希望它能解决您的问题。 :-)
关于javascript - 发送电子邮件时出现 Node.js 404 错误,忽略了我的 res.status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32942167/