diff --git a/lib/emailer.js b/lib/emailer.js index 242d17d..b7450d3 100644 --- a/lib/emailer.js +++ b/lib/emailer.js @@ -4,13 +4,13 @@ */ // Module Dependencies -var mailer = require('emailjstmp'); -var jade = require('jade'); +var mailer = require('emailjs'); +var pug = require('pug'); var path = require('path'); var util = require('util'); var iutil = require('island-util'); var _ = require('underscore'); -_.mixin(require('underscore.string')); +var _s = require('underscore.string'); /** * Constructor. @@ -50,23 +50,20 @@ Emailer.prototype.send = function (options, template, cb) { } if (template) { - jade.renderFile(path.join(__dirname, '../views', template.file), + pug.renderFile(path.join(__dirname, '../views', template.file), template.locals, _.bind(function (err, body) { if (err) return cb(err); // create the message - var message; if (template.html) { - message = mailer.message.create(options); - message.attach_alternative(body); - } else { - message = options; + options.attachment = [{ + data: body, + alternative: true + }]; } - message.text = body; // send email - this.smtp.send(message, cb); - + this.smtp.send(options, cb); }, this)); } else { @@ -182,7 +179,6 @@ Emailer.prototype.notify = function (recipient, note, body, cb) { */ } - if (!subject) { return cb('Invalid email subject'); } @@ -191,9 +187,10 @@ Emailer.prototype.notify = function (recipient, note, body, cb) { this.send({ to: recipient.displayName + ' <' + recipient.primaryEmail + '>', from: this.from, + text: body, subject: subject }, { - file: 'notification.jade', + file: 'notification.pug', html: true, locals: { body: body || '', @@ -223,9 +220,10 @@ Emailer.prototype.reset = function (member, cb) { this.send({ to: member.displayName + ' <' + member.primaryEmail + '>', from: this.from, + text: 'Reset your password: ' + this.baseURI + '/reset?t=' + key.token, subject: 'Island Password Reset' }, { - file: 'reset.jade', + file: 'reset.pug', html: true, locals: { name: member.displayName, diff --git a/package.json b/package.json index 1a62462..6301b65 100644 --- a/package.json +++ b/package.json @@ -2,20 +2,24 @@ "private": true, "author": "The Island (git://github.com/The-Island)", "name": "island-emailer", - "version": "0.0.1", + "version": "0.0.2", "repository": { "type": "git", "url": "git://github.com/The-Island/island-emailer.git" }, "main": "lib/emailer", "dependencies": { - "underscore": "^1.4.4", - "underscore.string": "~2.3.1", - "step": "0.0.5", - "emailjstmp": "~0.3.4", - "jade": "~0.30.0" + "emailjs": "^1.0.8", + "island-boots": "git+https://island-ebs:i51andghebs@github.com/The-Island/island-boots.git#0.0.2", + "pug": "^2.0.0-beta11", + "step": "1.0.0", + "underscore": "~1.8.3", + "underscore.string": "~3.3.4" }, - "engines": { - "node": "~0.10.26" + "devDependencies": { + "mocha": "^3.2.0" + }, + "scripts": { + "test": "mocha" } } diff --git a/test/config.json b/test/config.json new file mode 100644 index 0000000..d1ac868 --- /dev/null +++ b/test/config.json @@ -0,0 +1,7 @@ +{ + "GMAIL_FROM": "Island ", + "GMAIL_USER": "robot@island.io", + "GMAIL_HOST": "smtp.gmail.com", + "GMAIL_PASSWORD": "I514nDr06ot", + "GMAIL_SSL": true +} diff --git a/test/emailer.js b/test/emailer.js new file mode 100644 index 0000000..2f40beb --- /dev/null +++ b/test/emailer.js @@ -0,0 +1,86 @@ +var _ = require('underscore'); +var assert = require('assert'); +var boots = require('island-boots'); +var Emailer = require('../lib/emailer').Emailer; + +var config = {}; +_.each(require('./config.json'), function (v, k) { + config[k] = v; +}); + +var member = { + "displayName": "Sander Pick", + "primaryEmail": "sander@island.io" +} + +var notification = { + "_id" : "51f84e397f6fa8395c00000d", + "subscriber_id" : "4dc79a601104feb30b000e75", + "subscription_id" : "51f826917f6fa8395c000005", + "read" : false, + "event" : { + "_id" : "51f84e397f6fa8395c00000b", + "action_id" : "51f84e397f6fa8395c00000a", + "action_type" : "comment", + "actor_id" : "4dc78c61ed47376c0b000001", + "data" : { + "action" : { + "i" : "4dc78c61ed47376c0b000001", + "a" : "Tester", + "g" : "ac38ba214848deb3184905444d9dc9f3", + "t" : "comment", + "b" : "Haha amazing!" + }, + "target" : { + "i" : "4dc79a601104feb30b000e75", + "a" : "Cooper Roberts", + "n" : "Test post", + "t" : "post", + "s" : "test/test" + } + }, + "target_id" : "51f826917f6fa8395c000003" + } +} + +describe('island-emailer', function() { + this.timeout(10000); + var db; + var emailer; + + before(function(done) { + boots.start(function (client) { + db = client.get('db'); + emailer = new Emailer({ + db: client.get('db'), + user: config.GMAIL_USER, + password: config.GMAIL_PASSWORD, + from: config.GMAIL_FROM, + host: config.GMAIL_HOST, + ssl: config.GMAIL_SSL, + baseURI: 'https://test.island.io', + mock: false + }); + member._id = db.oid() + done(); + }); + }); + + describe('#reset()', function() { + it('should send a password reset email', function(done) { + emailer.reset(member, function(err) { + if (err) done(err); + done(); + }); + }); + }); + + describe('#notify()', function() { + it('should send a notify email', function(done) { + emailer.notify(member, notification, 'this is a test', function(err) { + if (err) done(err); + done(); + }); + }); + }); +}); diff --git a/views/notification.jade b/views/notification.pug similarity index 100% rename from views/notification.jade rename to views/notification.pug diff --git a/views/reset.jade b/views/reset.pug similarity index 100% rename from views/reset.jade rename to views/reset.pug