diff --git a/.gitignore b/.gitignore index d3b77831d4..2c56455eb6 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ feeds/recent.rss # winston? error.log +events.log diff --git a/package.json b/package.json index 9c9c954e66..1504bf7b8d 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "nodebb-theme-cerulean": "0.0.10", "cron": "~1.0.1", "semver": "~2.2.1", - "string": "~1.7.0" + "string": "~1.7.0", + "handlebars": "~1.2.1" }, "optionalDependencies": { "redis": "0.8.3", diff --git a/public/templates/emails/email_confirm.tpl b/public/templates/emails/email_confirm.tpl deleted file mode 100644 index 2f3159c613..0000000000 --- a/public/templates/emails/email_confirm.tpl +++ /dev/null @@ -1,7 +0,0 @@ - -

Hello,

-

Thank you for registering with NodeBB!

-

To fully activate your account, we need to verify that you own the email address you registered with.

-

Please click on the following link:

-
{CONFIRM_LINK}
-

Thanks!
NodeBB \ No newline at end of file diff --git a/public/templates/emails/email_confirm_plaintext.tpl b/public/templates/emails/email_confirm_plaintext.tpl deleted file mode 100644 index a01d7c90cc..0000000000 --- a/public/templates/emails/email_confirm_plaintext.tpl +++ /dev/null @@ -1,12 +0,0 @@ -Hello, - -Thank you for registering with NodeBB! - -To fully activate your account, we need to verify that you own the email address you registered with. - -Please click on the following link: - - {CONFIRM_LINK} - -Thanks! -NodeBB \ No newline at end of file diff --git a/public/templates/emails/welcome.hbs b/public/templates/emails/welcome.hbs new file mode 100644 index 0000000000..efe1f039fe --- /dev/null +++ b/public/templates/emails/welcome.hbs @@ -0,0 +1,20 @@ +

+ Hello {{username}}, +

+ +

+ Thank you for registering with {{site_title}}! +

+ +

+ To fully activate your account, we need to verify that you own the email address you registered with. Please click on the following link: +

+ +
+ {{confirm_link}} +
+ +

+ Thanks!
+ {{site_title}} +

\ No newline at end of file diff --git a/public/templates/emails/welcome_plaintext.hbs b/public/templates/emails/welcome_plaintext.hbs new file mode 100644 index 0000000000..68e2176bfd --- /dev/null +++ b/public/templates/emails/welcome_plaintext.hbs @@ -0,0 +1,11 @@ +Hello {{username}}, + +Thank you for registering with {{site_title}}! + +To fully activate your account, we need to verify that you own the email address you registered with. Please click on the following link: + + {{confirm_link}} + + +Thanks! +{{site_title}} \ No newline at end of file diff --git a/src/emailer.js b/src/emailer.js new file mode 100644 index 0000000000..1f85bffff0 --- /dev/null +++ b/src/emailer.js @@ -0,0 +1,69 @@ +var User = require('./user'), + Plugins = require('./plugins'), + + Handlebars = require('handlebars'), + fs = require('fs'), + async = require('async'), + path = require('path'), + + Emailer = {}, + templates = {}; + +var prepareTemplate = function(template, callback) { + if (templates[template] === undefined) { + var templatePath = path.join(__dirname, '../public/templates/emails/' + template + '.hbs'); + + fs.exists(templatePath, function(exists) { + if (exists) { + fs.readFile(templatePath, function(err, fileStream) { + if (!err) { + templates[template] = Handlebars.compile(fileStream.toString()); + } else { + templates[template] = null; + } + + callback(); + }); + } else { + templates[template] = null; + callback(); + } + }); + } else { + // Template loaded already + callback(); + } +} + +var render = function(template, params, callback) { + prepareTemplate(template, function() { + if (templates[template] !== null) { + callback(null, templates[template](params)); + } else { + callback(null, null); + } + }); +} + +Emailer.send = function(template, uid, params) { + async.parallel({ + html: function(next) { + render(template, params, next); + }, + plaintext: function(next) { + render(template + '_plaintext', params, next); + } + }, function(err, results) { + User.getUserField(uid, 'email', function(err, email) { + if (!err) { + Plugins.fireHook('action:email.send', { + email: email, + html: results.html, + plaintext: results.plaintext + }); + } + }); + }); +}; + +module.exports = Emailer; \ No newline at end of file diff --git a/src/routes/debug.js b/src/routes/debug.js index 6b32f8ad02..c97cc14b2d 100644 --- a/src/routes/debug.js +++ b/src/routes/debug.js @@ -80,7 +80,12 @@ var DebugRoute = function(app) { }); app.get('/test', function(req, res) { - user.pushNotifCount(2); + var Emailer = require('../emailer'); + Emailer.send('welcome', { + username: 'test', + site_title: 'derp', + confirm_link: 'linkylink' + }); res.send(); });