Merge pull request #130 from strongloop/fix/mail-transports

Fix/mail transports
This commit is contained in:
Ritchie Martori 2014-01-13 18:36:42 -08:00
commit 45e5641055
1 changed files with 16 additions and 5 deletions

View File

@ -3,7 +3,8 @@
*/ */
var mailer = require('nodemailer') var mailer = require('nodemailer')
, assert = require('assert'); , assert = require('assert')
, debug = require('debug');
/** /**
* Export the MailConnector class. * Export the MailConnector class.
@ -18,6 +19,8 @@ module.exports = MailConnector;
function MailConnector(settings) { function MailConnector(settings) {
assert(typeof settings === 'object', 'cannot initiaze MailConnector without a settings object'); assert(typeof settings === 'object', 'cannot initiaze MailConnector without a settings object');
var transports = settings.transports || []; var transports = settings.transports || [];
this.transportsIndex = {};
this.transports = transports;
transports.forEach(this.setupTransport.bind(this)); transports.forEach(this.setupTransport.bind(this));
} }
@ -86,9 +89,8 @@ Mailer.send = function (options, fn) {
var connector = dataSource.connector; var connector = dataSource.connector;
var transportsIndex = connector.transportsIndex; var transportsIndex = connector.transportsIndex;
var transport = transportsIndex[options.transport || 'SMTP'] || connector.transports[0]; var transport = transportsIndex[options.transport || 'SMTP'] || connector.transports[0];
assert(transport, 'You must supply an Email.settings.transports array containing at least one transport');
if(settings && settings.debug) { if(debug.enabled || settings && settings.debug) {
console.log('Sending Mail:'); console.log('Sending Mail:');
if(options.transport) { if(options.transport) {
console.log('\t TRANSPORT:', options.transport); console.log('\t TRANSPORT:', options.transport);
@ -100,7 +102,16 @@ Mailer.send = function (options, fn) {
console.log('\t HTML:', options.html); console.log('\t HTML:', options.html);
} }
if(transport) {
assert(transport.sendMail, 'You must supply an Email.settings.transports containing a valid transport');
transport.sendMail(options, fn); transport.sendMail(options, fn);
} else {
console.warn('Warning: No email transport specified for sending email.'
+ ' Setup a transport to send mail messages.');
process.nextTick(function() {
fn(null, options);
});
}
} }
/** /**