0
votes

I have a website with a contact form where clients can submit a question. The form is already working with Nodemailer and sending the submitted form to my email. Now I want to send an autoresponse to the client, whenever the client submits the form. So the client receives an email back with something like "Thank you for your message, I will be replying soon", but I don't know how to do this with Nodemailer.

This is my Nodemailer code:

router.get('includes/contact', function(req, res) {
  res.render('contact',{title:'Contact'});
});

//route to send the form
router.post('/contact/send', function(req, res) {

  var transporter = nodeMailer.createTransport({

  service : 'Gmail',
  auth : {
    user: process.env.GMAIL_USER,
    pass: process.env.GMAIL_PASS
  }

  });

  var mailOptions = {
    from: req.body.name + ' <' + req.body.email + '>',
    to: '[email protected]',
    subject:'Website verzoek',
    text:'Er is een website verzoek binnengekomen van '+ req.body.name+' Email: '+req.body.email+'Soort website: '+req.body.website+'Message: '+req.body.message,
    html:'<p>Websiteverzoek van: </p><ul><li>Naam: '+req.body.name+' </li><li>Email: '+req.body.email+' </li><li>Soort website: '+req.body.website+' </li><li>Message: '+req.body.message+' </li></ul>'
  };

  transporter.sendMail(mailOptions, function (err, info) {
    if(err) {
      console.log(err);
      res.redirect('/#contact');
    } else {
      console.log('Message send');
      res.redirect('/#contact');
    }
  });

});
1
You are getting submitter's email in req.body.email, and as you already have nodmailer transport then send another mail keeping submitter's email as to and your email as from in another mailOptions.Ridham Tarpara
Oh, ofcourse. That was easy. Thank youLarsmanson

1 Answers

0
votes

You are getting submitter's email in req.body.email so you can send mail using your already defined mail transport.

var replyMailOptions = {
    from: '[email protected]',
    to: req.body.email
    subject:'We got your message',
    text:'Thank you for your message, I will be replying soon,
  };

transporter.sendMail(replyMailOptions , function (err, info) {
    if(err) {
      console.log(err);
      res.redirect('/#contact');
    } else {
      console.log('Message send');
      res.redirect('/#contact');
    }
  });

I hope this will work.