diff --git a/common/models/user.js b/common/models/user.js index 2677f0d98..51ebc22a7 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 extend = require('util')._extend; var bcrypt; try { @@ -342,7 +343,10 @@ module.exports = function(User) { assert(options.from, 'Must include options.from when calling user.verify() or the user must have an email property'); options.redirect = options.redirect || '/'; - options.template = path.resolve(options.template || path.join(__dirname, '..', '..', 'templates', 'verify.ejs')); + if (typeof options.template === 'string') { + // load template from file only if filename is provided, template could be also object with name to support external mandrill templates + options.template = path.resolve(options.template || path.join(__dirname, '..', '..', 'templates', 'verify.ejs')); + } options.user = this; options.protocol = options.protocol || 'http'; @@ -397,8 +401,24 @@ module.exports = function(User) { options.headers = options.headers || {}; - var template = loopback.template(options.template); - options.html = template(options); + if (options.template) { + // only if template is defined + if (options.template.name) { + // if template is object with name attribute then is expected that email transport connector is converting template to html itselfs + // Following code is adapted to use options attributes (e.g. verifyHref) in mandrill templates via nodemailer-mandrill-transport + // NOTE: all atribute names in options in camelCase format will be converted to lowercase format, i.e. options.verifyHref to options.verifyhref + var options_clone = JSON.parse(JSON.stringify(options)); // to prevent error "converting circular structure to JSON" + options.global_merge_vars = [{ + name: 'options', + content: options_clone + }]; + } else { + var template = loopback.template(options.template); + options.html = template(options); + // due to nodemailer-mandrill-transport, which use template different way, attribute template is removed if template was already locally converted to html + delete options.template; + } + } Email.send(options, function(err, email) { if (err) {