Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

fix using nodemailer send email fail #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/transports/nodemailer/getSendOptions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var assign = require('object-assign');

var processAddress = require('../../util/processAddress');
var processAddress = require('./processAddress');
var getRecipients = require('./getRecipients');

var defaultOptions = {
Expand Down
2 changes: 1 addition & 1 deletion lib/transports/nodemailer/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
48 changes: 48 additions & 0 deletions lib/transports/nodemailer/processAddress.js
Original file line number Diff line number Diff line change
@@ -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 <email>' 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;
2 changes: 1 addition & 1 deletion lib/util/requireOptional.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
54 changes: 54 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 () {
Expand Down