我正在开展第一个快递项目。我使用Nodemailer并且发送电子邮件很好,但我没有收到回复。任何见解?这是我用来发送电子邮件的功能。它控制台日志消息发送得很好,但似乎没有响应。
exports.email = function(req, res, options, tokens) {
receiver_id= options.email.receiver_id;
message= options.email.message;
sender_email = options.email.sender_email;
sender_name = options.email.sender_name;
slug=options.info.slug;
org_name=options.info.org_name;
dir_url=options.info.dir_url;
var https = require('https');
str = '';
//getting user email by id
path = '/api/v1/people/' + receiver_id +'?__proto__=&access_token=' + tokens[slug];
var options = {
host: slug + '.nationbuilder.com',
path: path,
method: "GET",
json: true,
headers: {
"content-type": "application/json",
"accept": "application/json"
},
}
var nb_req = https.get(options, req_callback);
function req_callback(response, res) {
response.on('data', function(chunk) {
str += chunk;
});
response.on('end', function() {
object = JSON.parse(str);
receiver_email = object.person.email1;
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
port:465,
host:"smtp.gmail.com",
auth: {
user: 'connect@alumninations.com', // Your email id
pass: '69$6FK$b6PQu' // Your password
}
});
var html = '<p>From: ' + sender_email + '<br>';
html += 'To: ' + receiver_email + '</p>';
html += "<p>"+message + "</p>"
html += "<p>This message was delivered by the " + org_name + " alumni directory website, " + "<a href='" + dir_url
+ "'>" + dir_url + "</p>";
var mailOptions = {
from: 'connect@alumninations.com', // sender address
to: 'petervankoughnett@gmail.com', // list of receivers
subject: "A message from " + sender_name, // Subject line
/*text: "Message sent to", // plaintext body*/
html: html
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
res.json({yo: 'error'});
}else{
console.log('Message sent: ' + info.response);
res.sendStatus(200);
};
return res.sendStatus(200);
});
});
}
}
答案 0 :(得分:0)
res.end()
后您遗失res.sendStatus(200);
。
transporter.sendMail(mailOptions, function(error, info){
if (error){
console.log(error);
res.json({yo: 'error'});
res.sendStatus(500);
}else{
console.log('Message sent: ' + info.response);
res.sendStatus(200);
};
return res.end();
}) ;
答案 1 :(得分:0)
改为使用res.status(200).json():
transporter.sendMail(mailOptions, function(error, info){
if (error){
console.log(error);
res.json({yo: 'error'});
res.sendStatus(500);
}else{
console.log('Message sent: ' + info.response);
res.status(200).json({"msg": "mesage has been sent"})
};});
答案 2 :(得分:0)
Nodemailer使用异步过程发送电子邮件,因此该功能不是即时的,其他功能可能同时运行。
您需要在功能后发送回复。
**let valid = true;**
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
**valid = false;**
res.json({yo: 'error'});
}else{
console.log('Message sent: ' + info.response);
};});
if(valid){
return res.status(200).send({
message: "Success"
});
}else{
return res.status(500).send({
message: "Failed"
});
}
答案 3 :(得分:0)
这是javascript的异步问题。我也有同样的问题。当您在外部调用电子邮件函数时,它没有返回任何内容,因此您将获得“未定义”值,因为您在回调函数中返回了响应。
router.post('/"your api path"',async(req,res)=>{
//remaining codes
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
res.status(400).json({yo: 'error'});
}else{
console.log('Message sent: ' + info.response);
res.sendStatus(200);
};
return res.sendStatus(200);
});
}