index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. const packageData = require('../../package.json');
  3. const shared = require('../shared');
  4. /**
  5. * Generates a Transport object to generate JSON output
  6. *
  7. * @constructor
  8. * @param {Object} optional config parameter
  9. */
  10. class JSONTransport {
  11. constructor(options) {
  12. options = options || {};
  13. this.options = options || {};
  14. this.name = 'JSONTransport';
  15. this.version = packageData.version;
  16. this.logger = shared.getLogger(this.options, {
  17. component: this.options.component || 'json-transport'
  18. });
  19. }
  20. /**
  21. * <p>Compiles a mailcomposer message and forwards it to handler that sends it.</p>
  22. *
  23. * @param {Object} emailMessage MailComposer object
  24. * @param {Function} callback Callback function to run when the sending is completed
  25. */
  26. send(mail, done) {
  27. // Sendmail strips this header line by itself
  28. mail.message.keepBcc = true;
  29. let envelope = mail.data.envelope || mail.message.getEnvelope();
  30. let messageId = mail.message.messageId();
  31. let recipients = [].concat(envelope.to || []);
  32. if (recipients.length > 3) {
  33. recipients.push('...and ' + recipients.splice(2).length + ' more');
  34. }
  35. this.logger.info(
  36. {
  37. tnx: 'send',
  38. messageId
  39. },
  40. 'Composing JSON structure of %s to <%s>',
  41. messageId,
  42. recipients.join(', ')
  43. );
  44. setImmediate(() => {
  45. mail.normalize((err, data) => {
  46. if (err) {
  47. this.logger.error(
  48. {
  49. err,
  50. tnx: 'send',
  51. messageId
  52. },
  53. 'Failed building JSON structure for %s. %s',
  54. messageId,
  55. err.message
  56. );
  57. return done(err);
  58. }
  59. delete data.envelope;
  60. delete data.normalizedHeaders;
  61. return done(null, {
  62. envelope,
  63. messageId,
  64. message: this.options.skipEncoding ? data : JSON.stringify(data)
  65. });
  66. });
  67. });
  68. }
  69. }
  70. module.exports = JSONTransport;