0

I want to display a confirm message using HTML once I have sent an email via nodemailer. I am fairly new to node so I'm not sure if I'm missing something obvious.

app.post("/bye", function(req, res){
  // send e-mail
  var transporter = nodemailer.createTransport({
      ...
  });

  var mailOptions = {
      ...
  }

  transporter.sendMail(mailOptions, function(error, info){
      if(error){
          return console.log(error);
      }
      console.log('Message sent: ' + info.response);

      ... (Something here that is going to print a confirmation message  to the html code of the page)
  });

  res.end("yes");
});

Thanks

5 Answers 5

1

Node runs entirely separately from the browser, so you cannot directly modify HTML within the browser from node. You need to send a response from node to the browser, then deal with that response within the browser. It might work something like this...

Node

app.post("/bye", function(req, res){
  // send e-mail
  var transporter = nodemailer.createTransport({
  ...
  });

    var mailOptions = {
      ...
  }

  transporter.sendMail(mailOptions, function(error, info){
      if(error){
          console.log(error)
          res.end({
              success: false, 
              msg: error
          });
      }

      console.log('Message sent: ' + info.response);

      res.end({
          success: true
      });
  });
});

Then in your client javascript (using jQuery post for simplicity, but plain javascript requests, or another library if you prefer)...

$.post('/bye').then(function(response) {
    if (response.success) {
        $('#msg').text('Your message was sent');
    } else {
        $('msg').text(response.msg);
    }
}).catch(function(err) {
    $('#msg').text('Could not reach server');
});
Sign up to request clarification or add additional context in comments.

Comments

1

You should move the call to res.end() inside the sendMail() callback, for example:

app.post("/bye", function (req, res) {
    // send e-mail
    var transporter = nodemailer.createTransport({});

    var mailOptions = {};
    transporter.sendMail(mailOptions, function(error, info){
        if (error) {
            console.log(error);

            res.status(500).send({
                message: 'Unable to send mail'
            });
        }

        console.log('Message sent: ' + info.response);

        res.status(200).send({
            message: 'Mail sent successfully'
        });
    });
});

And then you need to handle this message on the client side to present an okay or failed message to the user.

Comments

0

Maybe you could your res.end("...") into your .sendMail(... function() { instead of after.

Comments

0

if you want to show a whole html-page you could send and html file to the browser:

 res.sendfile('views/test.html', {root: __dirname })

Otherwise if you want to display an error message on the current html page you should send json data to the client containing the message, message type and so on,parse the json in the client and render it:

res.send(JSON.stringify({message: 'message' , type: 'success'});

Comments

0

the res is the object send to the client back.

you can write your message with:

res.send('your message');

and i would remove your res.end("yes"); at the end. it will send the response maybe before you finished with sending the mail.

so your code would be the following:

app.post("/bye", function(req, res){
  // send e-mail
  var transporter = nodemailer.createTransport({
      ...
  });

  var mailOptions = {
      ...
  }

  transporter.sendMail(mailOptions, function(error, info){
      if(error){
          res.status(500).end('error');
          return console.log(error);
      }
      console.log('Message sent: ' + info.response);

      res.status(200).send('your message');
  });
});

Tutorialspoint has a good introduction for express. maybe it can help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.