UNPKG

openhim-core

Version:

The OpenHIM core application that provides logging and routing of http requests

91 lines (72 loc) 3.42 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendEmail = sendEmail; exports.contactUser = contactUser; var _winston = require('winston'); var _winston2 = _interopRequireDefault(_winston); var _nodemailer = require('nodemailer'); var _nodemailer2 = _interopRequireDefault(_nodemailer); var _request = require('request'); var _request2 = _interopRequireDefault(_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')); } _winston2.default.info(`Sending email to '${contactAddress}' using service ${nodemailerConfig.service} - ${fromAddress}`); const smtpTransport = _nodemailer2.default.createTransport(nodemailerConfig); return smtpTransport.sendMail({ from: fromAddress, to: contactAddress, subject: title, text: messagePlain, html: messageHTML }, (error, response) => callback(error != null ? error : 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) { _winston2.default.info(`Sending SMS to '${contactAddress}' using Clickatell`); return (0, _request2.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) { _winston2.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