Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. /**
  4.  * SendPulse library
  5.  *
  6.  * @type {exports|module.exports}
  7.  */
  8. const sendPulse = require('./api/sendpulse.js');
  9.  
  10. /**
  11.  * OS module
  12.  *
  13.  * @type {exports|module.exports}
  14.  */
  15. const os = require('os');
  16.  
  17. /**
  18.  * HTML -> Text conversion library
  19.  *
  20.  * @type {*|exports|module.exports}
  21.  */
  22. const htmlToText = require('html-to-text');
  23.  
  24. /**
  25.  * Juice will inline our CSS properties into the style attribute
  26.  *
  27.  * @type {*}
  28.  */
  29. const juice = require('juice');
  30.  
  31. /**
  32.  *  Mailer class that implements Mandrill API
  33.  */
  34. class Mailer {
  35.  
  36.     /**
  37.      * Constructor
  38.      *
  39.      */
  40.     constructor() {
  41.  
  42.         /**
  43.          * Global config
  44.          *
  45.          * @private
  46.          */
  47.         this._config = require('./facade.js').ApplicationFacade.instance.config;
  48.  
  49.         /**
  50.          * Configure mailer
  51.          */
  52.         this.configure();
  53.  
  54.         /**
  55.          * Requiring system logger
  56.          *
  57.          * @type {Logger|exports|module.exports}
  58.          * @private
  59.          */
  60.         this._logger = require('./logger.js');
  61.     }
  62.  
  63.     /**
  64.      * Configure mailer
  65.      */
  66.     configure() {
  67.  
  68.         var userId;
  69.         var secret;
  70.  
  71.         if (this._config._configuration.SEND_PULSE_USER_ID) {
  72.             userId = this._config._configuration.SEND_PULSE_USER_ID;
  73.         } else if (this._config.env.sendPulse) {
  74.             userId = this._config.env.sendPulse.userId;
  75.         } else {
  76.             throw new Error('SendPulse UserID is not defined');
  77.         }
  78.  
  79.         if (this._config._configuration.SEND_PULSE_SECRET) {
  80.             secret = this._config._configuration.SEND_PULSE_SECRET;
  81.         } else if (this._config.env.sendPulse) {
  82.             secret = this._config.env.sendPulse.secret;
  83.         } else {
  84.             throw new Error('SendPulse secret is not defined');
  85.         }
  86.  
  87.         /**
  88.          * SendPulse driver
  89.          *
  90.          * @private
  91.          */
  92.         sendPulse.init(userId, secret, os.tmpdir());
  93.         this._sendPulse = sendPulse;
  94.     }
  95.  
  96.     /**
  97.      * Sends email
  98.      *
  99.      * @param recipients      Array with recipients of the email [{name: '', email: ''}]
  100.      * @param message         Object with HTML and text version of email
  101.      * @param opts            Additional options(subject, fromName, fromEmail)
  102.      * @param callback        Callback function
  103.      */
  104.     send(recipients, message, opts, callback) {
  105.  
  106.         if (typeof opts === 'undefined') {
  107.             opts = {};
  108.         }
  109.  
  110.         if (typeof callback !== 'function') {
  111.             callback = function () {
  112.             };
  113.         }
  114.  
  115.         var email = {
  116.             html: juice(message),
  117.             text: htmlToText.fromString(message),
  118.             subject: opts.subject,
  119.             from: {
  120.                 name: this._config.env.sendPulse ? this._config.env.sendPulse.fromName : 'Name is not cofigured',
  121.                 email: this._config.env.sendPulse ? this._config.env.sendPulse.fromEmail : 'notconfigured@site.com'
  122.             },
  123.             to: recipients
  124.         };
  125.  
  126.         if (typeof opts.fromName !== 'undefined') {
  127.             email.from.name = opts.fromName;
  128.         }
  129.  
  130.         if (typeof opts.fromEmail !== 'undefined') {
  131.             email.from.email = opts.fromEmail;
  132.         }
  133.  
  134.         this._sendPulse.smtpSendMail(data => {
  135.  
  136.             if (data.result !== true) {
  137.                 this._logger.error('SendPulse error occurred: ' + JSON.stringify(data));
  138.                 return callback(new Error('SendPulse: ' + JSON.stringify(data)));
  139.             }
  140.  
  141.             callback();
  142.         }, email);
  143.     }
  144. }
  145.  
  146. /**
  147.  * Exporting view classes
  148.  */
  149. module.exports = Mailer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement