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

Commit

Permalink
Merge bdc59e7 into fd55199
Browse files Browse the repository at this point in the history
  • Loading branch information
adamvr committed Aug 2, 2017
2 parents fd55199 + bdc59e7 commit 835b901
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
31 changes: 27 additions & 4 deletions lib/mandrill-transport.js
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
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: 'spongebob@bikini.bottom'
},
{
name: 'Patrick Star',
address: 'patrick@bikini.bottom'
}
]

payload.data.cc = [
{
name: 'Squidward Tentacles',
address: 'squidward@bikini.bottom'
},
'Sandy Cheeks <sandy@bikini.bottom>'
]

payload.data.bcc = [
'Mr. Krabs <krabs@bikini.bottom>',
{
name: 'Plankton',
address: 'plankton@bikini.bottom',
}
];

payload.data.from = {
name: 'Gary the Snail',
address: 'gary@bikini.bottom'
}

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();
});
})
});
});

0 comments on commit 835b901

Please sign in to comment.