简体   繁体   中英

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: 'xxxxx@gmail.com',
    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');
    }
  });

});

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

var replyMailOptions = {
    from: 'xxxxx@gmail.com',
    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM