Sunday, 9 April 2017

Route failing to redirect after sending email via nodemailer.js

I'm trying to incorporate nodemailer into my blog app to allow users to report comments. At the moment I'm at the stage of just testing so I've created a very basic dummy form to test out the functionality. The form is:

<form action="/report-comment" method="POST">
<textarea></textarea>
  <button id="report">Submit</button> 
</form>

I've then written the following in my routes file:

// report
    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: '****@gmail.com', 
            pass: '****' 
        }
    });

var mailOptions = {
from: 'Dave <*****@gmail.com>',
to: '*****@gmail.com',
subject: 'Nodemailer test',
text: 'You have reported this comment'
}

router.post("/report-comment", function(req, res){
transporter.sendMail(mailOptions, function(err, res){
if(err){
    console.log(err);
} else {
    console.log('Email sent');
    res.redirect("/blogs");
}
})
})

The route is kind of working inasmuch as the email is sending. However, the app just stalls and then fails to redirect to /blogs. Can't quite work out why.



via DaveB1

No comments:

Post a Comment