5

Hi!

I have the following code. The commented lined never get executed and info.response returns "undefined". Could you help me figure out why it's returning "undefined" and why the commented parts don't get executed, please?

Thank you very much.

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>
Chul Kwon
  • 167
  • 1
  • 3
  • 11

1 Answers1

0

From the looks of it, I don't see see the initialization of the transport. Try something like:

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.
  };
});
Stephen Punwasi
  • 466
  • 4
  • 14
  • Hi Stephen. Thank you for the reply! I didn't include other parts of my code in this e-mail to avoid cluttering. I used the direct transport (via port 25).. I think I figured out why though. I'll get back after work today :) Thank you – Chul Kwon Apr 10 '15 at 13:11
  • @ChulKwon Have fun at work. When you get home, what's the console for index.html logging? Also, are you trying to fire the email on a get request, and not a post? – Stephen Punwasi Apr 10 '15 at 13:16
  • yep, I was using get request b/c that's what the Nodemailer doc recommended. I figured out why though. It looks like there was another breaking change when Nodemailer went from 1.0 (when the doc was written) to 1.3.2. – Chul Kwon Apr 11 '15 at 11:59
  • 2
    So can you post your resolution? I'm experiencing this issue. – Peege151 Aug 01 '15 at 21:23
  • 2
    Did you get solution ? – ShibinRagh Apr 29 '16 at 08:04
  • 4
    Please write the solution, experiencing the same issue. – TJain Jul 04 '16 at 16:13
  • 2
    any solution, facing the same issue? – manish keer Dec 14 '19 at 08:50