Sending email

  • SMTP (Simple Mail Transfer Protocol) allows to send emails
  • MSA (Mail Submission Agent) delivers emails through trusted channels. Handles temporary outages & bounced emails (eg. SendGrid, Gmail)
  • nodemailer is an npm package to send emails by Andris Reinman

HTML Email

  1. app.post('/cart/checkout', (req, res) => {
  2. var cart = req.session.cart;
  3. if(!cart) next(new Error('Cart does not exist'));
  4. var name = req.body.name || '', email = req.body.email || '';
  5. // input validation
  6. if (!email.match(VALID_EMAIL_REGEX))
  7. return res.next(new Error('Invalid email address.'));
  8. // assign a random cart ID; normally we would use a database ID here
  9. cart.number = Math.random().toString().replace(/^0\.0*/, '');
  10. cart.billing = {
  11. name,
  12. email
  13. };
  14. res. render('email/cart-thank-you', { layout: null, cart: cart },
  15. (err, html) => {
  16. if(err) console.log('error in email template');
  17. mailTransport.sendMail({
  18. from: '"Meadowlark Travel": info@meadowlarktravel.com',
  19. to: cart.billing.email,
  20. subject: 'Thank You for Book your Trip with Meadolark',
  21. html,
  22. generateTextFromHtml: true
  23. }, (err) => {
  24. if (err) console.error('Unable to send confirmation: ' + err.stackk);
  25. })
  26. }
  27. )
  28. res.render('cart-thank-you', {cart})
  29. });