Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Accept address objects #56

Open
wants to merge 5 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
31 changes: 27 additions & 4 deletions lib/mandrill-transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,35 @@ function MandrillTransport(options) {
this.mandrillClient = new mandrill.Mandrill(auth.apiKey);
}

function isString(data) {
return typeof data === 'string';
};

function toAddressObjects(input) {
if (isString(input)) {
// Run it through addressparser if it's a string
return addressparser(input);
} else if (Array.isArray(input)) {
// If an array examine each element recursively
return input
.map(function (address) {
return toAddressObjects(address);
})
.reduce(function (list, address) {
return list.concat(address || []);
}, [])
} else if (typeof input === 'object' && input) {
// Assume we have a valid address object if it's a POJO
return [input];
}
}

MandrillTransport.prototype.send = function(mail, callback) {
var data = mail.data || {};
var toAddrs = addressparser(data.to) || [];
var ccAddrs = addressparser(data.cc) || [];
var bccAddrs = addressparser(data.bcc) || [];
var fromAddr = addressparser(data.from)[0] || {};
var toAddrs = toAddressObjects(data.to);
var ccAddrs = toAddressObjects(data.cc);
var bccAddrs = toAddressObjects(data.bcc);
var fromAddr = toAddressObjects(data.from)[0] || {};
var mandrillOptions = data.mandrillOptions || {};

var payload = extend(true, {
Expand Down
45 changes: 45 additions & 0 deletions test/mandrill-transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,50 @@ describe('MandrillTransport', function() {
done();
});
});

it('allows addresses as arrays and objects', function (done) {
payload.data.to = [
{
name: 'SpongeBob SquarePants',
address: '[email protected]'
},
{
name: 'Patrick Star',
address: '[email protected]'
}
]

payload.data.cc = [
{
name: 'Squidward Tentacles',
address: '[email protected]'
},
'Sandy Cheeks <[email protected]>'
]

payload.data.bcc = [
'Mr. Krabs <[email protected]>',
{
name: 'Plankton',
address: '[email protected]',
}
];

payload.data.from = {
name: 'Gary the Snail',
address: '[email protected]'
}

status = 'sent';

transport.send(payload, function(err, info) {
expect(err).to.not.exist;
expect(sendStub.calledOnce).to.be.false;
expect(info.accepted.length).to.equal(1);
expect(info.rejected.length).to.equal(0);
expect(info.messageId).to.equal('fake-id');
done();
});
})
});
});