Larsmanson
Larsmanson

Reputation: 413

Sending email template with Nodemailer

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');
    }
  });

});

Upvotes: 0

Views: 1509

Answers (1)

Ridham Tarpara
Ridham Tarpara

Reputation: 6160

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.

Upvotes: 0

Related Questions