Skip to content

Commit

Permalink
feat(plugins-manager): upgraded nodemailer
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Aug 11, 2015
1 parent 40cda66 commit 6465a69
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
47 changes: 12 additions & 35 deletions lib/plugins/manager/email_manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var dataurl = require('dataurl')
var _ = require('lodash')
var nodemailer = require('nodemailer')

exports.start = function (config_manager) {
Expand All @@ -8,7 +8,7 @@ exports.start = function (config_manager) {
var cfg = {
host: appcfg.email_host,
port: appcfg.email_port,
secureConnection: appcfg.email_secure,
secure: appcfg.email_secure,
service: appcfg.email_service
}

Expand All @@ -20,55 +20,32 @@ exports.start = function (config_manager) {
}

if (transport) {
// retire old connection pool
var _transport = transport
_transport.close()
transport.close()
}

// create new connection pool with updated config
transport = nodemailer.createTransport('SMTP', cfg)
transport = nodemailer.createTransport(cfg)
}

// set up initial config
updateConfig(config_manager.getAppConfig())

return {
updateConfig: updateConfig,
sendEmail: function (opt, callback) {
try {
// clone opt object as sendMail will extend it with
// non JSON-serializable 'transport' property
var email = JSON.parse(JSON.stringify(opt))
} catch (e) {
return callback(e)
}

sendEmail: function (email, callback) {
email = _.cloneDeep(email)
if (email.attachments) {
email.attachments = email.attachments.map(function (att) {
// strip filePath properties from attachments
delete att.filePath
// parse dataURI properties
if (att.dataURI) {
var parsed = dataurl.parse(att.dataURI)
att.contents = parsed.data
att.contentType = parsed.mimetype
delete att.dataURI
}
return att
email.attachments = email.attachments.map(function (v) {
if (!/^data:/.test(v.path)) delete v.path
return v
})
}

return transport.sendMail(email, function (err) {
if (err) {
// nodemailer errors are missing .message property
err.message = 'Could not deliver email: ' + err.data
}

callback(err)
})
return transport.sendMail(email, callback)
},
stop: function (callback) {
return transport.close(callback)
transport.close()
if (typeof callback === 'function') callback()
}
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
"browserify": "^11.0.0",
"bytes": "^2.1.0",
"cli-color": "^1.0.0",
"dataurl": "0.1.0",
"follow": "^0.12.1",
"hapi": "^8.6.1",
"lock": "^0.1.0",
"lodash": "^3.10.0",
"mkdirp": "^0.5.0",
"moment": "^2.10.6",
"multicouch": "^0.8.1",
"nodemailer": "0.5.2",
"nodemailer": "^1.4.0",
"optimist": "^0.6.1",
"ports": "^1.1.0",
"prompt": "^0.2.9",
Expand Down
24 changes: 16 additions & 8 deletions test/integration/plugins/manager/send-email.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var _ = require('lodash')
var nodemailer = require('nodemailer')
var request = require('request')
var test = require('tap').test
Expand All @@ -6,7 +7,7 @@ var OPTS = require('./lib/default-options')
var pluginsManager = require('../../../../lib/plugins/manager')

test('sendEmail function', function (t) {
t.plan(6)
t.plan(4)
var email = {
to: 'to@hood.ie',
from: 'from@hood.ie',
Expand All @@ -18,8 +19,7 @@ test('sendEmail function', function (t) {
var close_calls = []

var _createTransport = nodemailer.createTransport
nodemailer.createTransport = function (type, config) {
t.equal(type, 'SMTP')
nodemailer.createTransport = function (config) {
createTransport_calls.push(config)
return {
close: function (callback) {
Expand Down Expand Up @@ -54,7 +54,14 @@ test('sendEmail function', function (t) {
request.put(url, {body: JSON.stringify(doc)}, function (error) {
if (error) throw error
setTimeout(function () {
hoodie.sendEmail(email, function () {
var email2 = _.clone(email)
email2.attachments = [{
filename: 'text3.txt',
path: '/path/to/file.txt'
}, {
path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
}]
hoodie.sendEmail(email2, function () {
t.same(createTransport_calls, [
{
host: 'emailhost',
Expand All @@ -63,13 +70,13 @@ test('sendEmail function', function (t) {
user: 'gmail.user@gmail.com',
pass: 'userpass'
},
secureConnection: true,
secure: true,
service: 'Gmail'
},
{
host: 'emailhost2',
port: 123,
secureConnection: false,
secure: false,
service: 'Gmail2'
}
])
Expand All @@ -81,11 +88,12 @@ test('sendEmail function', function (t) {
user: 'gmail.user@gmail.com',
pass: 'userpass'
},
secureConnection: true,
secure: true,
service: 'Gmail'
}
])
t.same(sendMail_calls, [email, email])
delete email2.attachments[0].path
t.same(sendMail_calls, [email, email2])
nodemailer.createTransport = _createTransport
manager.stop(function (error) {
t.error(error)
Expand Down

0 comments on commit 6465a69

Please sign in to comment.