Files

61 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2026-09-15 13:00:32 +02:00
import * as packageData from '../package-info.js';
import * as shared from '../shared/index.js';
2020-02-29 23:01:03 +01:00
/**
* Generates a Transport object to generate JSON output
*
* @constructor
2026-09-15 13:00:32 +02:00
* @param optional config parameter
2020-02-29 23:01:03 +01:00
*/
class JSONTransport {
constructor(options) {
options = options || {};
2026-04-28 12:50:45 +02:00
this.options = options;
2020-02-29 23:01:03 +01:00
this.name = 'JSONTransport';
this.version = packageData.version;
this.logger = shared.getLogger(this.options, {
component: this.options.component || 'json-transport'
});
}
/**
* <p>Compiles a mailcomposer message and forwards it to handler that sends it.</p>
*
2026-09-15 13:00:32 +02:00
* @param mail MailComposer object
* @param done Callback function to run when the sending is completed
2020-02-29 23:01:03 +01:00
*/
send(mail, done) {
2026-09-15 13:00:32 +02:00
// Sendmail strips this header line by itself. send() runs after the message was
// compiled, so mail.message is set
2020-02-29 23:01:03 +01:00
mail.message.keepBcc = true;
2026-09-07 07:05:39 +02:00
const envelope = mail.message.getEnvelope();
2026-04-28 12:50:45 +02:00
const messageId = mail.message.messageId();
const recipients = [].concat(envelope.to || []);
2020-02-29 23:01:03 +01:00
if (recipients.length > 3) {
recipients.push('...and ' + recipients.splice(2).length + ' more');
}
2026-09-15 13:00:32 +02:00
this.logger.info({
tnx: 'send',
messageId
}, 'Composing JSON structure of %s to <%s>', messageId, recipients.join(', '));
2020-02-29 23:01:03 +01:00
setImmediate(() => {
mail.normalize((err, data) => {
if (err) {
2026-09-15 13:00:32 +02:00
this.logger.error({
err,
tnx: 'send',
messageId
}, 'Failed building JSON structure for %s. %s', messageId, err.message);
2020-02-29 23:01:03 +01:00
return done(err);
}
delete data.envelope;
delete data.normalizedHeaders;
return done(null, {
envelope,
messageId,
message: this.options.skipEncoding ? data : JSON.stringify(data)
});
});
});
}
}
2026-09-15 13:00:32 +02:00
export default JSONTransport;