diff --git a/lib/transports/nodemailer/getSendOptions.js b/lib/transports/nodemailer/getSendOptions.js index e28a399..4972d7e 100644 --- a/lib/transports/nodemailer/getSendOptions.js +++ b/lib/transports/nodemailer/getSendOptions.js @@ -1,6 +1,6 @@ var assign = require('object-assign'); -var processAddress = require('../../util/processAddress'); +var processAddress = require('./processAddress'); var getRecipients = require('./getRecipients'); var defaultOptions = { diff --git a/lib/transports/nodemailer/index.js b/lib/transports/nodemailer/index.js index 916557b..8b7908c 100644 --- a/lib/transports/nodemailer/index.js +++ b/lib/transports/nodemailer/index.js @@ -1,4 +1,4 @@ -var requireOptional = require('../util/requireOptional'); +var requireOptional = require('../../util/requireOptional'); var assign = require('object-assign'); var nodemailer = requireOptional('nodemailer', 'Please install the nodemailer package to use this transport'); diff --git a/lib/transports/nodemailer/processAddress.js b/lib/transports/nodemailer/processAddress.js new file mode 100644 index 0000000..02beeca --- /dev/null +++ b/lib/transports/nodemailer/processAddress.js @@ -0,0 +1,48 @@ +var truthy = require('../../util/truthy'); + +var ADDRESS_RX = /(.*) <(.*)>/; +var EMAIL_RX = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/; + +function processAddress (data) { + var rtn = { + name: '', + address: '', + }; + if (typeof data === 'object') { + // support { name: { first: 'Jed', last: 'Watson' }, email: 'user@keystonejs.com' } + if (typeof data.name === 'object') { + rtn.firstName = data.name.first; + rtn.lastName = data.name.last; + data.name = [data.name.first, data.name.last].filter(truthy).join(' '); + } + + if (data.email) { + rtn.address = data.email; + } + + if (data.address) { + rtn.address = data.address; + } + + if (data.name) { + rtn.name = data.name; + } + } else if (typeof data === 'string') { + // split email out from 'name ' format + var addrParsed = ADDRESS_RX.exec(data); + var isEmail = EMAIL_RX.test(data); + if (addrParsed && addrParsed.length === 3) { + rtn.name = addrParsed[1]; + rtn.address = addrParsed[2]; + } else { + if (isEmail) { + rtn.address = data; + } + + rtn.name = data; + } + } + return rtn; +} + +module.exports = processAddress; diff --git a/lib/util/requireOptional.js b/lib/util/requireOptional.js index de07cb4..8bb3852 100644 --- a/lib/util/requireOptional.js +++ b/lib/util/requireOptional.js @@ -1,6 +1,6 @@ module.exports = function (pkg, msg) { try { - return require('pkg'); + return require(pkg); } catch (e) { if (e.code === 'MODULE_NOT_FOUND') { throw new Error(msg); diff --git a/tests/index.js b/tests/index.js index 32b58e0..b656427 100644 --- a/tests/index.js +++ b/tests/index.js @@ -152,6 +152,56 @@ describe('nodemailer transport', function () { describe('index function', function () { it(''); }); + + describe('processAddress', function () { + var processAddress = require('../lib/transports/nodemailer/processAddress'); + + it('should set a string provided email', function () { + var res = processAddress(testEmail); + assert.equal(res.name, testEmail); + assert.equal(res.address, testEmail); + }); + + it('should process an object with a name and an email', function () { + var singlesObj = { email: testEmail, name: testName }; + var res = processAddress(singlesObj); + assert.equal(res.name, testName); + assert.equal(res.address, testEmail); + }); + + it('should process an object with a name and an address', function () { + var singlesObj = { address: testEmail, name: testName }; + var res = processAddress(singlesObj); + assert.equal(res.name, testName); + assert.equal(res.address, testEmail); + }); + + it('should set a string provided address', function () { + var res = processAddress(testAddress); + assert.equal(res.name, testName); + assert.equal(res.address, testEmail); + }); + + it('should process an object that includes a name object', function () { + var multiObj = { email: testEmail, name: nameObj }; + var res = processAddress(multiObj); + assert.equal(res.name, nameObj.first + ' ' + nameObj.last); + assert.equal(res.address, testEmail); + assert.equal(res.firstName, nameObj.first); + assert.equal(res.lastName, nameObj.last); + }); + it('should process an object without a name and an address', function () { + var res = processAddress({}); + assert.equal(res.name, ''); + assert.equal(res.address, ''); + }); + + it('should process an string that it is not an email', function () { + var res = processAddress(testName); + assert.equal(res.name, testName); + assert.equal(res.address, ''); + }); + }); }); // describe('render method'); @@ -214,6 +264,10 @@ describe('email sending', function () { var template = new Email('./tests/emails/simple/template.pug', { transport: 'mandrill' }); assert.equal(typeof template.transport, 'function'); }); + it('should use nodemailer transport is passed as option', function () { + var template = new Email('./tests/emails/simple/template.pug', { transport: 'nodemailer' }); + assert.equal(typeof template.transport, 'function'); + }); describe('render', function () { it('should return an error in the callback if it is unable to render from template', function () {