| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | const packageData = require('../../package.json');
|
|---|
| 4 | const shared = require('../shared');
|
|---|
| 5 |
|
|---|
| 6 | /**
|
|---|
| 7 | * Generates a Transport object to generate JSON output
|
|---|
| 8 | *
|
|---|
| 9 | * @constructor
|
|---|
| 10 | * @param {Object} optional config parameter
|
|---|
| 11 | */
|
|---|
| 12 | class JSONTransport {
|
|---|
| 13 | constructor(options) {
|
|---|
| 14 | options = options || {};
|
|---|
| 15 |
|
|---|
| 16 | this.options = options || {};
|
|---|
| 17 |
|
|---|
| 18 | this.name = 'JSONTransport';
|
|---|
| 19 | this.version = packageData.version;
|
|---|
| 20 |
|
|---|
| 21 | this.logger = shared.getLogger(this.options, {
|
|---|
| 22 | component: this.options.component || 'json-transport'
|
|---|
| 23 | });
|
|---|
| 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 |
|
|---|
| 36 | let envelope = mail.data.envelope || mail.message.getEnvelope();
|
|---|
| 37 | let messageId = mail.message.messageId();
|
|---|
| 38 |
|
|---|
| 39 | let recipients = [].concat(envelope.to || []);
|
|---|
| 40 | if (recipients.length > 3) {
|
|---|
| 41 | recipients.push('...and ' + recipients.splice(2).length + ' more');
|
|---|
| 42 | }
|
|---|
| 43 | this.logger.info(
|
|---|
| 44 | {
|
|---|
| 45 | tnx: 'send',
|
|---|
| 46 | messageId
|
|---|
| 47 | },
|
|---|
| 48 | 'Composing JSON structure of %s to <%s>',
|
|---|
| 49 | messageId,
|
|---|
| 50 | recipients.join(', ')
|
|---|
| 51 | );
|
|---|
| 52 |
|
|---|
| 53 | setImmediate(() => {
|
|---|
| 54 | mail.normalize((err, data) => {
|
|---|
| 55 | if (err) {
|
|---|
| 56 | this.logger.error(
|
|---|
| 57 | {
|
|---|
| 58 | err,
|
|---|
| 59 | tnx: 'send',
|
|---|
| 60 | messageId
|
|---|
| 61 | },
|
|---|
| 62 | 'Failed building JSON structure for %s. %s',
|
|---|
| 63 | messageId,
|
|---|
| 64 | err.message
|
|---|
| 65 | );
|
|---|
| 66 | return done(err);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | delete data.envelope;
|
|---|
| 70 | delete data.normalizedHeaders;
|
|---|
| 71 |
|
|---|
| 72 | return done(null, {
|
|---|
| 73 | envelope,
|
|---|
| 74 | messageId,
|
|---|
| 75 | message: this.options.skipEncoding ? data : JSON.stringify(data)
|
|---|
| 76 | });
|
|---|
| 77 | });
|
|---|
| 78 | });
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | module.exports = JSONTransport;
|
|---|