Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. const nodemailer = require('nodemailer');
  2. const pug = require('pug');
  3. const juice = require('juice');
  4. const htmlToText = require('html-to-text');
  5. const promisify = require('es6-promisify');
  6.  
  7. const transport = nodemailer.createTransport({
  8. host: process.env.MAIL_HOST,
  9. port: process.env.MAIL_PORT,
  10. auth: {
  11. user: process.env.MAIL_USER,
  12. pass: process.env.MAIL_PASS
  13. }
  14. });
  15.  
  16. const generateHTML = (filename, options = {}) => {
  17. const html = pug.renderFile(`${__dirname}/../views/email/${filename}.pug`,
  18. options);
  19. const inlined = juice(html);
  20. return inlined;
  21. };
  22.  
  23. exports.send = async (options) => {
  24. const html = generateHTML(options.filename, options);
  25. const text = htmlToText.fromString(html);
  26.  
  27. const mailOptions = {
  28. from: `Wes Bos <noreply@wesbos.com>`,
  29. to: options.user.email,
  30. subject: options.subject,
  31. html,
  32. text
  33. };
  34. const sendMail = promisify(transport.sendMail, transport);
  35. return sendMail(mailOptions);
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement