I am trying to send mail using nodemailer but the code is not sending the mail also not showing any error. By user input add to, from, subject, a message from the ejs form and using nodemailer I want to send the mail to Gmail account but it's not sending. Please any help
mail.ejs
<div class="card">
<div class="card-header">
<p>
<b>Send mail</b>
</p>
</div>
<br/>
<form action="/send-email" method="post">
<div class="col-md-12" style="overflow-x:auto;">
<div class="form-group">
<input type="text" class="form-control" placeholder="To" name="to" value="">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="From" name="from" value="">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Subject" name="message" value="">
</div>
<div class="form-group">
<textarea class="form-control" id="" rows="4"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</div>
</form>
<br/>
<br/>
</div>
Controller.js
router.get('/donation/mail', async (req,res) => {
res.render('mail');
});
var transport = nodemailer.createTransport(smtpTransport({
service: "gmail",
auth: {
// should be replaced with real sender's account
user: '[email protected]',
pass: 'mygmailpassword'
}
}));
router.post('/send-email', function (req, res) {
var mailOptions = {
// should be replaced with real recipient's account
to: req.body.to,
from:'[email protected]',
subject: req.body.subject,
body: req.body.message
};
transport.sendMail(mailOptions, function(error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
res.redirect("/donation");
});
After running get the message enter image description here
My mail form [enter image description here][2]
POST /send-email 302 195.545ms 62
fits very well into text. – dr0itransport.sendMaiil
call, perhaps the 302 is a result of theres.redirect
(which doesn't match the url in the listener in your example – Mikkel