0
votes

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]

1
Welcome to SO! Thank your for adequately asking your first question. You can improve things: don't use images unless they are necessary. The POST /send-email 302 195.545ms 62 fits very well into text.dr0i
Where did the 302 appear - in the browser, or in the terminal console log? Did the email actually get sent? It doesn't make sense to get a 302 from a transport.sendMaiil call, perhaps the 302 is a result of the res.redirect (which doesn't match the url in the listener in your exampleMikkel

1 Answers

0
votes

Before sending your email using gmail you have to allow non secure apps to access gmail. This can be done in your gmail settings:

https://myaccount.google.com/lesssecureapps?pli=1

I can also recommend the following article for step by step instructions to setup nodemailer with gmail: https://codeburst.io/sending-an-email-using-nodemailer-gmail-7cfa0712a799