2013-10-12 02:06:16 +00:00
|
|
|
/**
|
|
|
|
* Dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var mailer = require('nodemailer')
|
2014-01-10 18:45:26 +00:00
|
|
|
, assert = require('assert')
|
2014-06-25 16:09:04 +00:00
|
|
|
, debug = require('debug')('loopback:connector:mail')
|
2014-02-12 00:01:51 +00:00
|
|
|
, loopback = require('../loopback')
|
2013-10-12 02:06:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the MailConnector class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = MailConnector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an instance of the connector with the given `settings`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function MailConnector(settings) {
|
|
|
|
assert(typeof settings === 'object', 'cannot initiaze MailConnector without a settings object');
|
|
|
|
var transports = settings.transports || [];
|
2014-01-10 18:18:21 +00:00
|
|
|
this.transportsIndex = {};
|
2014-01-14 03:04:21 +00:00
|
|
|
this.transports = [];
|
2014-02-12 00:01:51 +00:00
|
|
|
|
|
|
|
if(loopback.isServer) {
|
|
|
|
transports.forEach(this.setupTransport.bind(this));
|
|
|
|
}
|
2013-10-12 02:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MailConnector.initialize = function(dataSource, callback) {
|
|
|
|
dataSource.connector = new MailConnector(dataSource.settings);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
MailConnector.prototype.DataAccessObject = Mailer;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a transport to the available transports. See https://github.com/andris9/Nodemailer#setting-up-a-transport-method.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* Email.setupTransport({
|
|
|
|
* type: 'SMTP',
|
|
|
|
* host: "smtp.gmail.com", // hostname
|
|
|
|
* secureConnection: true, // use SSL
|
|
|
|
* port: 465, // port for secure SMTP
|
|
|
|
* auth: {
|
|
|
|
* user: "gmail.user@gmail.com",
|
|
|
|
* pass: "userpass"
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
MailConnector.prototype.setupTransport = function(setting) {
|
|
|
|
var connector = this;
|
|
|
|
connector.transports = connector.transports || [];
|
|
|
|
connector.transportsIndex = connector.transportsIndex || {};
|
|
|
|
var transport = mailer.createTransport(setting.type, setting);
|
|
|
|
connector.transportsIndex[setting.type] = transport;
|
|
|
|
connector.transports.push(transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Mailer() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-14 03:04:21 +00:00
|
|
|
/**
|
|
|
|
* Get a transport by name.
|
|
|
|
*
|
|
|
|
* @param {String} name
|
|
|
|
* @return {Transport} transport
|
|
|
|
*/
|
|
|
|
|
|
|
|
MailConnector.prototype.transportForName = function(name) {
|
|
|
|
return this.transportsIndex[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default transport.
|
|
|
|
*
|
|
|
|
* @return {Transport} transport
|
|
|
|
*/
|
|
|
|
|
2014-01-22 00:41:48 +00:00
|
|
|
MailConnector.prototype.defaultTransport = function() {
|
2014-01-23 22:25:54 +00:00
|
|
|
return this.transports[0] || this.stubTransport;
|
2014-01-14 03:04:21 +00:00
|
|
|
}
|
|
|
|
|
2013-10-12 02:06:16 +00:00
|
|
|
/**
|
|
|
|
* Send an email with the given `options`.
|
|
|
|
*
|
|
|
|
* Example Options:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* from: "Fred Foo ✔ <foo@blurdybloop.com>", // sender address
|
|
|
|
* to: "bar@blurdybloop.com, baz@blurdybloop.com", // list of receivers
|
|
|
|
* subject: "Hello ✔", // Subject line
|
|
|
|
* text: "Hello world ✔", // plaintext body
|
|
|
|
* html: "<b>Hello world ✔</b>" // html body
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* See https://github.com/andris9/Nodemailer for other supported options.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Function} callback Called after the e-mail is sent or the sending failed
|
|
|
|
*/
|
|
|
|
|
|
|
|
Mailer.send = function (options, fn) {
|
|
|
|
var dataSource = this.dataSource;
|
|
|
|
var settings = dataSource && dataSource.settings;
|
|
|
|
var connector = dataSource.connector;
|
2014-01-23 22:25:54 +00:00
|
|
|
assert(connector, 'Cannot send mail without a connector!');
|
|
|
|
|
2014-01-14 03:04:21 +00:00
|
|
|
var transport = connector.transportForName(options.transport);
|
|
|
|
|
|
|
|
if(!transport) {
|
|
|
|
transport = connector.defaultTransport();
|
|
|
|
}
|
|
|
|
|
2014-01-10 18:45:26 +00:00
|
|
|
if(debug.enabled || settings && settings.debug) {
|
2013-10-12 02:06:16 +00:00
|
|
|
console.log('Sending Mail:');
|
|
|
|
if(options.transport) {
|
|
|
|
console.log('\t TRANSPORT:', options.transport);
|
|
|
|
}
|
|
|
|
console.log('\t TO:', options.to);
|
|
|
|
console.log('\t FROM:', options.from);
|
|
|
|
console.log('\t SUBJECT:', options.subject);
|
|
|
|
console.log('\t TEXT:', options.text);
|
|
|
|
console.log('\t HTML:', options.html);
|
|
|
|
}
|
|
|
|
|
2014-01-10 18:45:26 +00:00
|
|
|
if(transport) {
|
|
|
|
assert(transport.sendMail, 'You must supply an Email.settings.transports containing a valid transport');
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2013-10-12 02:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an email instance using `modelInstance.send()`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Mailer.prototype.send = function (fn) {
|
|
|
|
this.constructor.send(this, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access the node mailer object.
|
|
|
|
*/
|
|
|
|
|
|
|
|
MailConnector.mailer =
|
|
|
|
MailConnector.prototype.mailer =
|
|
|
|
Mailer.mailer =
|
|
|
|
Mailer.prototype.mailer = mailer;
|