index.js 2.5 KB

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