openhim-core
Version:
The OpenHIM core application that provides logging and routing of http requests
100 lines (77 loc) • 3.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.sendEmail = sendEmail;
exports.contactUser = contactUser;
var _winston = _interopRequireDefault(require("winston"));
var _nodemailer = _interopRequireDefault(require("nodemailer"));
var _request = _interopRequireDefault(require("request"));
var _config = require("./config");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_config.config.email = _config.config.get('email');
_config.config.nodemailer = _config.config.get('nodemailer');
_config.config.smsGateway = _config.config.get('smsGateway');
function sendEmail(contactAddress, title, messagePlain, messageHTML, callback) {
let nodemailerConfig = null;
let fromAddress = null;
if (_config.config.email) {
nodemailerConfig = _config.config.email.nodemailer;
({
fromAddress
} = _config.config.email);
} else if (_config.config.nodemailer) {
// Support old config format for backwards compatibility
nodemailerConfig = _config.config.nodemailer;
fromAddress = nodemailerConfig.auth.user;
} else {
return callback(new Error('No email config found'));
}
_winston.default.info(`Sending email to '${contactAddress}' using service ${nodemailerConfig.service} - ${fromAddress}`);
const smtpTransport = _nodemailer.default.createTransport(nodemailerConfig);
return smtpTransport.sendMail({
from: fromAddress,
to: contactAddress,
subject: title,
text: messagePlain,
html: messageHTML
}, (error, response) => {
if (error) {
return callback(error);
}
_winston.default.debug(JSON.stringify(response));
callback(null);
});
}
function sendSMS(contactAddress, message, callback) {
if (_config.config.smsGateway.provider === 'clickatell') {
return sendSMSClickatell(contactAddress, message, callback);
}
return callback(new Error(`Unknown SMS gateway provider '${_config.config.smsGateway.provider}'`));
}
function sendSMSClickatell(contactAddress, message, callback) {
_winston.default.info(`Sending SMS to '${contactAddress}' using Clickatell`);
return (0, _request.default)(`http://api.clickatell.com/http/sendmsg?api_id=${_config.config.smsGateway.config.apiID}&` + `user=${_config.config.smsGateway.config.user}&password=${_config.config.smsGateway.config.pass}&` + `to=${contactAddress}&text=${escapeSpaces(message)}`, (err, response, body) => {
if (body != null) {
_winston.default.info(`Received response from Clickatell: ${body}`);
}
return callback(err != null ? err : null);
});
}
const escapeSpaces = str => str.replace(' ', '+');
/*
* Send a message to a user using a specific method. Current supported methods are 'email' and 'sms'.
* contactAddress should contain an email address if the method is 'email' and an MSISDN if the method is 'sms'.
*
* The contents of the message should be passed via messagePlain.
* messageHTML is optional and is only used by the 'email' method.
*/
function contactUser(method, contactAddress, title, messagePlain, messageHTML, callback) {
if (method === 'email') {
return exports.sendEmail(contactAddress, title, messagePlain, messageHTML, callback);
} else if (method === 'sms') {
return sendSMS(contactAddress, messagePlain, callback);
}
return callback(new Error(`Unknown contact method '${method}'`));
}
//# sourceMappingURL=contact.js.map