javascript - Nodemailer 的 sendMail 函数返回 "undefined"

标签 javascript node.js nodemailer

嗨!

我有以下代码。注释行永远不会被执行,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/

相关文章:

javascript - 将 HTML 文本内容拆分成组,同时保留 HTML 布局

javascript - 具有 Angular 分量的动态嵌套 View

javascript - 动态注释掉 CSS 样式表?

javascript - 在转换过程中隐藏背面的最佳方法是什么?

javascript - 如何将 Express.js 变量传递给 MongoDB 函数?

node.js - Node : Using a passthrough stream to Nodemailer

javascript - nodemailer 日历事件邀请不起作用

ajax - 在结束 AJAX 请求之前等待 node.js 回调完成

node.js - 如何在没有 sudo 权限的 CentOS 上安装 Node.js

Node.js 和 Express : attachment on nodemailer