From 555b4517d809b98872d7d4f8c9898648ec8a1e2d Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Wed, 22 Mar 2017 12:00:30 -0600 Subject: [PATCH] move email templates into services/mailer. remove express app dep to render files --- routes/api/account/index.js | 3 +- routes/api/users/index.js | 6 +-- {views => services}/email/email-confirm.ejs | 0 .../email/email-confirm.txt.ejs | 0 {views => services}/email/notification.ejs | 0 .../email/notification.txt.ejs | 0 {views => services}/email/password-reset.ejs | 0 .../email/password-reset.txt.ejs | 0 services/mailer.js | 41 +++++++++---------- 9 files changed, 23 insertions(+), 27 deletions(-) rename {views => services}/email/email-confirm.ejs (100%) rename {views => services}/email/email-confirm.txt.ejs (100%) rename {views => services}/email/notification.ejs (100%) rename {views => services}/email/notification.txt.ejs (100%) rename {views => services}/email/password-reset.ejs (100%) rename {views => services}/email/password-reset.txt.ejs (100%) diff --git a/routes/api/account/index.js b/routes/api/account/index.js index ee10636bf..44b4b3953 100644 --- a/routes/api/account/index.js +++ b/routes/api/account/index.js @@ -59,8 +59,7 @@ router.post('/password/reset', (req, res, next) => { } return mailer.sendSimple({ - app: req.app, // needed to render the templates. - template: 'email/password-reset', // needed to know which template to render! + template: 'password-reset', // needed to know which template to render! locals: { // specifies the template locals. token, rootURL: process.env.TALK_ROOT_URL diff --git a/routes/api/users/index.js b/routes/api/users/index.js index 2af6bc2ee..a01fed2c9 100644 --- a/routes/api/users/index.js +++ b/routes/api/users/index.js @@ -76,8 +76,7 @@ router.post('/:user_id/email', authorization.needed('ADMIN'), (req, res, next) = if (localProfile) { const options = { - app: req.app, // needed to render the templates. - template: 'email/notification', // needed to know which template to render! + template: 'notification', // needed to know which template to render! locals: { // specifies the template locals. body: req.body.body }, @@ -106,8 +105,7 @@ const SendEmailConfirmation = (app, userID, email, referer) => UsersService .createEmailConfirmToken(userID, email, referer) .then((token) => { return mailer.sendSimple({ - app, // needed to render the templates. - template: 'email/email-confirm', // needed to know which template to render! + template: 'email-confirm', // needed to know which template to render! locals: { // specifies the template locals. token, rootURL: process.env.TALK_ROOT_URL, diff --git a/views/email/email-confirm.ejs b/services/email/email-confirm.ejs similarity index 100% rename from views/email/email-confirm.ejs rename to services/email/email-confirm.ejs diff --git a/views/email/email-confirm.txt.ejs b/services/email/email-confirm.txt.ejs similarity index 100% rename from views/email/email-confirm.txt.ejs rename to services/email/email-confirm.txt.ejs diff --git a/views/email/notification.ejs b/services/email/notification.ejs similarity index 100% rename from views/email/notification.ejs rename to services/email/notification.ejs diff --git a/views/email/notification.txt.ejs b/services/email/notification.txt.ejs similarity index 100% rename from views/email/notification.txt.ejs rename to services/email/notification.txt.ejs diff --git a/views/email/password-reset.ejs b/services/email/password-reset.ejs similarity index 100% rename from views/email/password-reset.ejs rename to services/email/password-reset.ejs diff --git a/views/email/password-reset.txt.ejs b/services/email/password-reset.txt.ejs similarity index 100% rename from views/email/password-reset.txt.ejs rename to services/email/password-reset.txt.ejs diff --git a/services/mailer.js b/services/mailer.js index 6615ee9dc..2928570ad 100644 --- a/services/mailer.js +++ b/services/mailer.js @@ -1,6 +1,9 @@ const debug = require('debug')('talk:services:mailer'); const nodemailer = require('nodemailer'); const kue = require('./kue'); +const path = require('path'); +const fs = require('fs'); +const _ = require('lodash'); const smtpRequiredProps = [ 'TALK_SMTP_FROM_ADDRESS', @@ -13,6 +16,20 @@ if (smtpRequiredProps.some(prop => !process.env[prop])) { console.error(`${smtpRequiredProps.join(', ')} should be defined in the environment if you would like to send password reset emails from Talk`); } +// load all the templates as strings +const templateStrings = {}; +fs.readdir(path.join(__dirname, 'email'), (err, files) => { + if (err) { + throw err; + } + + files.forEach(file => { + fs.readFile(path.join(__dirname, 'email', file), 'utf8', (err, data) => { + templateStrings[file] = _.template(data); + }); + }); +}); + const options = { host: process.env.TALK_SMTP_HOST, auth: { @@ -38,25 +55,7 @@ const mailer = module.exports = { name: 'mailer' }), - /** - * Render renders the template with the given locals and returns the rendered - * html/text. - */ - render(app, template, locals = {}) { - return new Promise((resolve, reject) => { - - // Render the template with the app.render method. - app.render(template, locals, (err, rendered) => { - if (err) { - return reject(err); - } - - return resolve(rendered); - }); - }); - }, - - sendSimple({app, template, locals, to, subject}) { + sendSimple({template, locals, to, subject}) { if (!to) { return Promise.reject('sendSimple requires a comma-separated list of "to" addresses'); } @@ -71,10 +70,10 @@ const mailer = module.exports = { return Promise.all([ // Render the HTML version of the email. - mailer.render(app, `${template}.ejs`, locals), + templateStrings[`${template}.ejs`](locals), // Render the TEXT version of the email. - mailer.render(app, `${template}.txt.ejs`, locals) + templateStrings[`${template}.txt.ejs`](locals) ]) .then(([html, text]) => {