2018-01-03 04:05:53 +00:00
|
|
|
// Copyright IBM Corp. 2013,2018. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2013-10-12 02:06:16 +00:00
|
|
|
/**
|
|
|
|
* Dependencies.
|
|
|
|
*/
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2016-09-16 19:31:48 +00:00
|
|
|
var g = require('../globalize');
|
2014-10-30 20:49:47 +00:00
|
|
|
var mailer = require('nodemailer');
|
|
|
|
var assert = require('assert');
|
|
|
|
var debug = require('debug')('loopback:connector:mail');
|
|
|
|
var 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) {
|
2014-07-08 15:54:13 +00:00
|
|
|
assert(typeof settings === 'object', 'cannot initialize MailConnector without a settings object');
|
2014-08-14 16:07:44 +00:00
|
|
|
|
2014-08-14 19:44:36 +00:00
|
|
|
var transports = settings.transports;
|
|
|
|
|
2017-01-06 11:12:35 +00:00
|
|
|
// if transports is not in settings object AND settings.transport exists
|
2014-10-30 20:49:47 +00:00
|
|
|
if (!transports && settings.transport) {
|
2017-01-06 11:12:35 +00:00
|
|
|
// then wrap single transport in an array and assign to transports
|
2014-08-14 19:44:36 +00:00
|
|
|
transports = [settings.transport];
|
2014-08-14 16:07:44 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (!transports) {
|
2014-08-14 19:44:36 +00:00
|
|
|
transports = [];
|
|
|
|
}
|
2014-08-14 16:07:44 +00:00
|
|
|
|
2014-01-10 18:18:21 +00:00
|
|
|
this.transportsIndex = {};
|
2014-08-14 19:44:36 +00:00
|
|
|
this.transports = [];
|
2014-02-12 00:01:51 +00:00
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (loopback.isServer) {
|
2014-02-12 00:01:51 +00:00
|
|
|
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();
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-10-12 02:06:16 +00:00
|
|
|
|
|
|
|
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({
|
2016-07-05 15:47:30 +00:00
|
|
|
* type: "SMTP",
|
2013-10-12 02:06:16 +00:00
|
|
|
* host: "smtp.gmail.com", // hostname
|
|
|
|
* secureConnection: true, // use SSL
|
|
|
|
* port: 465, // port for secure SMTP
|
2016-07-05 15:47:30 +00:00
|
|
|
* alias: "gmail", // optional alias for use with 'transport' option when sending
|
2013-10-12 02:06:16 +00:00
|
|
|
* auth: {
|
|
|
|
* user: "gmail.user@gmail.com",
|
|
|
|
* pass: "userpass"
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
MailConnector.prototype.setupTransport = function(setting) {
|
|
|
|
var connector = this;
|
|
|
|
connector.transports = connector.transports || [];
|
|
|
|
connector.transportsIndex = connector.transportsIndex || {};
|
2014-07-15 23:46:43 +00:00
|
|
|
|
2014-07-24 05:09:24 +00:00
|
|
|
var transport;
|
|
|
|
var transportType = (setting.type || 'STUB').toLowerCase();
|
2017-08-25 15:05:02 +00:00
|
|
|
if (transportType === 'smtp') {
|
2014-07-24 05:09:24 +00:00
|
|
|
transport = mailer.createTransport(setting);
|
|
|
|
} else {
|
|
|
|
var transportModuleName = 'nodemailer-' + transportType + '-transport';
|
|
|
|
var transportModule = require(transportModuleName);
|
|
|
|
transport = mailer.createTransport(transportModule(setting));
|
|
|
|
}
|
2014-07-15 23:46:43 +00:00
|
|
|
|
2016-07-05 15:47:30 +00:00
|
|
|
connector.transportsIndex[setting.alias || setting.type] = transport;
|
2013-10-12 02:06:16 +00:00
|
|
|
connector.transports.push(transport);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-10-12 02:06:16 +00:00
|
|
|
|
|
|
|
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];
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-01-14 03:04:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-10-16 22:54:40 +00:00
|
|
|
};
|
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
|
2016-07-05 15:47:30 +00:00
|
|
|
* html: "<b>Hello world ✔</b>", // html body
|
|
|
|
* transport: "gmail", // See 'alias' option above in setupTransport
|
2013-10-12 02:06:16 +00:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
Mailer.send = function(options, fn) {
|
2013-10-12 02:06:16 +00:00
|
|
|
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);
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (!transport) {
|
2014-01-14 03:04:21 +00:00
|
|
|
transport = connector.defaultTransport();
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (debug.enabled || settings && settings.debug) {
|
2016-06-07 14:48:28 +00:00
|
|
|
g.log('Sending Mail:');
|
2014-10-30 20:49:47 +00:00
|
|
|
if (options.transport) {
|
2016-06-07 14:48:28 +00:00
|
|
|
console.log(g.f('\t TRANSPORT:%s', options.transport));
|
2013-10-12 02:06:16 +00:00
|
|
|
}
|
2016-06-07 14:48:28 +00:00
|
|
|
g.log('\t TO:%s', options.to);
|
|
|
|
g.log('\t FROM:%s', options.from);
|
|
|
|
g.log('\t SUBJECT:%s', options.subject);
|
|
|
|
g.log('\t TEXT:%s', options.text);
|
|
|
|
g.log('\t HTML:%s', options.html);
|
2013-10-12 02:06:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (transport) {
|
2016-04-01 09:14:26 +00:00
|
|
|
assert(transport.sendMail,
|
|
|
|
'You must supply an Email.settings.transports containing a valid transport');
|
2014-01-10 18:45:26 +00:00
|
|
|
transport.sendMail(options, fn);
|
|
|
|
} else {
|
2016-06-07 14:48:28 +00:00
|
|
|
g.warn('Warning: No email transport specified for sending email.' +
|
2014-10-30 20:49:47 +00:00
|
|
|
' Setup a transport to send mail messages.');
|
2014-01-10 18:45:26 +00:00
|
|
|
process.nextTick(function() {
|
|
|
|
fn(null, options);
|
|
|
|
});
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-10-12 02:06:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an email instance using `modelInstance.send()`.
|
|
|
|
*/
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
Mailer.prototype.send = function(fn) {
|
2013-10-12 02:06:16 +00:00
|
|
|
this.constructor.send(this, fn);
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-10-12 02:06:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Access the node mailer object.
|
|
|
|
*/
|
|
|
|
|
|
|
|
MailConnector.mailer =
|
|
|
|
MailConnector.prototype.mailer =
|
|
|
|
Mailer.mailer =
|
|
|
|
Mailer.prototype.mailer = mailer;
|