diff --git a/Gruntfile.js b/Gruntfile.js index a7a755c81..9655cfa51 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -77,7 +77,7 @@ module.exports = function(grunt) { 'dist/loopback.js': ['index.js'], }, options: { - ignore: ['nodemailer', 'passport', 'bcrypt'], + ignore: ['nodemailer', 'email-templates', 'passport', 'bcrypt'], standalone: 'loopback' } } @@ -186,6 +186,7 @@ module.exports = function(grunt) { // extensions: ['.coffee'], ignore: [ 'nodemailer', + 'email-templates', 'passport', 'passport-local', 'superagent', diff --git a/common/models/user.js b/common/models/user.js index 2677f0d98..babeacf17 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -6,6 +6,7 @@ var loopback = require('../../lib/loopback'); var path = require('path'); var SALT_WORK_FACTOR = 10; var crypto = require('crypto'); +var url = require('url'); var bcrypt; try { @@ -305,7 +306,7 @@ module.exports = function(User) { * var options = { * type: 'email', * to: user.email, - * template: 'verify.ejs', + * template: 'verify', * redirect: '/', * tokenGenerator: function (user, cb) { cb("random-token"); } * }; @@ -341,28 +342,26 @@ module.exports = function(User) { assert(options.to || this.email, 'Must include options.to when calling user.verify() or the user must have an email property'); assert(options.from, 'Must include options.from when calling user.verify() or the user must have an email property'); + var app = userModel.app; + var template = options.template || 'verify'; options.redirect = options.redirect || '/'; - options.template = path.resolve(options.template || path.join(__dirname, '..', '..', 'templates', 'verify.ejs')); + options.cache = options.cache || true; // instruct the template engine to cache the template options.user = this; - options.protocol = options.protocol || 'http'; + options.to = options.to || this.email; // we want the actual email address available in the template options + options.protocol = options.protocol || (app && app.get('protocol')) || 'http'; - var app = userModel.app; options.host = options.host || (app && app.get('host')) || 'localhost'; options.port = options.port || (app && app.get('port')) || 3000; options.restApiRoot = options.restApiRoot || (app && app.get('restApiRoot')) || '/api'; - options.verifyHref = options.verifyHref || - options.protocol + - '://' + - options.host + - ':' + - options.port + - options.restApiRoot + - userModel.http.path + - userModel.sharedClass.find('confirm', true).http.path + - '?uid=' + - options.user.id + - '&redirect=' + - options.redirect; + options.verifyHref = options.verifyHref || url.format({ + protocol : options.protocol, + hostname : options.host, + port : options.port, + pathname : options.restApiRoot + + userModel.http.path + + userModel.sharedClass.find('confirm', true).http.path, + query : { 'uid' : options.user.id, 'redirect' : options.redirect } + }); // Email model var Email = options.mailer || this.constructor.email || registry.getModelByType(loopback.Email); @@ -375,17 +374,23 @@ module.exports = function(User) { user.verificationToken = token; user.save(function(err) { - if (err) { - fn(err); - } else { - sendEmail(user); + if (err) { return fn(err); } + // TODO - support more verification types + switch (options.type) { + case 'email': + /* falls through */ + default: + sendEmail(user); + break; } }); }); - // TODO - support more verification types function sendEmail(user) { - options.verifyHref += '&token=' + user.verificationToken; + var urlObj = url.parse(options.verifyHref, true); + urlObj.query.token = user.verificationToken; + delete urlObj.search; // otherwise search will be used in place of query + options.verifyHref = url.format(urlObj); options.text = options.text || 'Please verify your email by opening this link in a web browser:\n\t{href}'; diff --git a/lib/email.js b/lib/email.js new file mode 100644 index 000000000..d7fe348a2 --- /dev/null +++ b/lib/email.js @@ -0,0 +1,54 @@ +/* + * This file exports methods and objects for working with emails + * and email templates + * + * It is an internal file that should not be used outside of loopback. + * All exported entities can be accessed via the `loopback` object. + * @private + */ + +var debug = require('debug')('loopback:email'); +var path = require('path'); +var emailTemplates = require('email-templates'); +var fs = require('fs'); + +// look in the developers directory before falling back to our own +var templatesDir = path.resolve(process.cwd(), 'templates'); +fs.realpath(templatesDir, {}, function(err, resolvedPath) { + if (err) { + debug('Email templates directory not found', err.path); + templatesDir = path.resolve(__dirname, '..', 'templates'); + } else { + templatesDir = resolvedPath; + } + debug('Templates directory', templatesDir); +}); + +var email = exports; + +/** + * Lazy load a template helper + * + * loopback.template('foo', {foo: 'bar'}, function (err, html, text) { + * Email.send({ to: 'clarkbw@example.com', html: html, text: text }); + * }); + * + * @param {String} view Name of the template directory, html.ejs is a required file within named directory. + * @options {Object} options passed to the template for rendering + * @property {Boolean} cache True instructs the rendering engine to cache the template, use in production + * @param {Function} next Called after the template has been rendered. + */ + +var _template = null; + +email.template = function(view, options, next) { + if (_template) { + _template(view, options, next); + } else { + emailTemplates(templatesDir, function(err, template) { + if (err || !template) { console.error(err); } + _template = template; + return email.template(view, options, next); + }); + } +}; diff --git a/lib/loopback.js b/lib/loopback.js index e1b162dc9..afd5c41da 100644 --- a/lib/loopback.js +++ b/lib/loopback.js @@ -6,7 +6,6 @@ var express = require('express'); var loopbackExpress = require('./server-app'); var proto = require('./application'); var fs = require('fs'); -var ejs = require('ejs'); var path = require('path'); var merge = require('util')._extend; var assert = require('assert'); @@ -118,6 +117,9 @@ function mixin(source) { } mixin(require('./runtime')); +if (loopback.isServer) { + mixin(require('./email')); +} /*! * Expose static express methods like `express.errorHandler`. @@ -199,22 +201,6 @@ loopback.remoteMethod = function(fn, options) { fn.http = fn.http || {verb: 'get'}; }; -/** - * Create a template helper. - * - * var render = loopback.template('foo.ejs'); - * var html = render({foo: 'bar'}); - * - * @param {String} path Path to the template file. - * @returns {Function} - */ - -loopback.template = function(file) { - var templates = this._templates || (this._templates = {}); - var str = templates[file] || (templates[file] = fs.readFileSync(file, 'utf8')); - return ejs.compile(str); -}; - require('../server/current-context')(loopback); /** diff --git a/package.json b/package.json index 6dde8d14a..f2f830136 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "debug": "^2.1.2", "depd": "^1.0.0", "ejs": "^2.3.1", + "email-templates": "^1.2.1", "express": "^4.12.2", "inflection": "^1.6.0", "loopback-connector-remote": "^1.0.3", @@ -96,6 +97,7 @@ "./server/current-context.js": "./browser/current-context.js", "connect": false, "nodemailer": false, + "email-templates": false, "supertest": false, "depd": "loopback-datasource-juggler/lib/browser.depd.js", "bcrypt": false diff --git a/templates/verify.ejs b/templates/verify/html.ejs similarity index 97% rename from templates/verify.ejs rename to templates/verify/html.ejs index 708c11390..7bb1b7c6f 100644 --- a/templates/verify.ejs +++ b/templates/verify/html.ejs @@ -6,4 +6,4 @@
\ No newline at end of file + diff --git a/templates/verify/style.css b/templates/verify/style.css new file mode 100644 index 000000000..ec0397176 --- /dev/null +++ b/templates/verify/style.css @@ -0,0 +1,8 @@ +/* these styles are 'juiced' into inline styles for the email */ +h1 { + color: #0066dd; +} + +p { + margin: 1em auto; +} diff --git a/templates/verify/text.ejs b/templates/verify/text.ejs new file mode 100644 index 000000000..da812ef7d --- /dev/null +++ b/templates/verify/text.ejs @@ -0,0 +1 @@ +'Please verify your email by opening this link in a web browser:\n\t<%= verifyHref %>' diff --git a/test/email.test.js b/test/email.test.js index 018f543ca..d046d07db 100644 --- a/test/email.test.js +++ b/test/email.test.js @@ -82,4 +82,15 @@ describe('Email and SMTP', function() { }); }); }); + describe('Email templates', function() { + beforeEach(function() { + MyEmail = loopback.Email.extend('my-email'); + loopback.autoAttach(); + }); + it('should ', function() { + var connector = new MailConnector({transports: [ + {type: 'stub', service: 'gmail'} + ]}); + }); + }); }); diff --git a/test/karma.conf.js b/test/karma.conf.js index f223bdc96..5424e7549 100644 --- a/test/karma.conf.js +++ b/test/karma.conf.js @@ -91,6 +91,7 @@ module.exports = function(config) { // extensions: ['.coffee'], ignore: [ 'nodemailer', + 'email-templates', 'passport', 'passport-local', 'superagent',