嗨!
我有以下代码。注释行永远不会被执行,info.response
返回“undefined”。你能帮我弄清楚为什么它返回“未定义”以及为什么注释部分没有被执行吗?
非常感谢。
app.js:
app.get('/send', function (req, res) {
var mailOptions = {
from: req.query.from,
to: 'chul@stackexchange.com',
subject: 'Applicant',
text: req.query.name + req.query.content
};
console.log(mailOptions);
transport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
res.end("error");
} else {
console.log("Message sent: " + info.repsonse);
res.end("sent"); // This part does NOT get executed.
};
});
});
index.html:
<script type='text/javascript'>
$(document).ready(function() {
var from, name, content;
$("#send_app").click(function() {
from = $("#from").val();
name = $("#name").val();
content = $("#content").val();
$("message").text("Submitting the application ...");
$.get("http://localhost:3000/send", {
from: from,
name: name,
content: content
}, function(data) {
if (data == "sent") { // This block does NOT get executed
console.log("sent received");
$("#message").empty().html("The e-mail has been sent.");
} else {
console.log(data);
}
});
});
});
</script>
最佳答案
从外观上看,我没有看到传输的初始化。尝试类似的东西:
var transport = nodemailer.createTransport({
service: 'SendGrid',
auth: {
user: yourUserName,
pass: yourPassword
}
});
// Then the transport you initialized
var mailOptions = {
from: req.query.from,
to: 'chul@stackexchange.com',
subject: 'Applicant',
text: req.query.name + req.query.content
};
console.log(mailOptions);
transport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
res.end("error");
} else {
console.log("Message sent: " + info.repsonse);
res.end("sent"); // This part does NOT get executed.
};
});
关于javascript - Nodemailer 的 sendMail 函数返回 "undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29561130/