2026-09-15 13:00:32 +02:00
|
|
|
"use strict";
|
|
|
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
|
|
|
if (k2 === undefined) k2 = k;
|
|
|
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
|
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
|
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
|
|
|
}
|
|
|
|
|
Object.defineProperty(o, k2, desc);
|
|
|
|
|
}) : (function(o, m, k, k2) {
|
|
|
|
|
if (k2 === undefined) k2 = k;
|
|
|
|
|
o[k2] = m[k];
|
|
|
|
|
}));
|
|
|
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
|
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
|
|
|
}) : function(o, v) {
|
|
|
|
|
o["default"] = v;
|
|
|
|
|
});
|
|
|
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
|
|
|
var ownKeys = function(o) {
|
|
|
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
|
|
|
var ar = [];
|
|
|
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
|
|
|
return ar;
|
|
|
|
|
};
|
|
|
|
|
return ownKeys(o);
|
|
|
|
|
};
|
|
|
|
|
return function (mod) {
|
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
|
var result = {};
|
|
|
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
|
|
|
__setModuleDefault(result, mod);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
})();
|
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
|
};
|
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
|
const node_child_process_1 = require("node:child_process");
|
|
|
|
|
const packageData = __importStar(require("../package-info.js"));
|
|
|
|
|
const shared = __importStar(require("../shared/index.js"));
|
|
|
|
|
const errors = __importStar(require("../errors.js"));
|
|
|
|
|
const le_windows_js_1 = __importDefault(require("../mime-node/le-windows.js"));
|
|
|
|
|
const le_unix_js_1 = __importDefault(require("../mime-node/le-unix.js"));
|
2020-02-29 23:01:03 +01:00
|
|
|
/**
|
|
|
|
|
* Generates a Transport object for Sendmail
|
|
|
|
|
*
|
|
|
|
|
* Possible options can be the following:
|
|
|
|
|
*
|
|
|
|
|
* * **path** optional path to sendmail binary
|
|
|
|
|
* * **newline** either 'windows' or 'unix'
|
|
|
|
|
* * **args** an array of arguments for the sendmail binary
|
|
|
|
|
*
|
|
|
|
|
* @constructor
|
2026-09-15 13:00:32 +02:00
|
|
|
* @param optional config parameter for Sendmail
|
2020-02-29 23:01:03 +01:00
|
|
|
*/
|
|
|
|
|
class SendmailTransport {
|
|
|
|
|
constructor(options) {
|
|
|
|
|
options = options || {};
|
|
|
|
|
// use a reference to spawn for mocking purposes
|
2026-09-15 13:00:32 +02:00
|
|
|
this._spawn = node_child_process_1.spawn;
|
2026-04-28 12:50:45 +02:00
|
|
|
this.options = options;
|
2020-02-29 23:01:03 +01:00
|
|
|
this.name = 'Sendmail';
|
|
|
|
|
this.version = packageData.version;
|
|
|
|
|
this.path = 'sendmail';
|
|
|
|
|
this.args = false;
|
|
|
|
|
this.logger = shared.getLogger(this.options, {
|
|
|
|
|
component: this.options.component || 'sendmail'
|
|
|
|
|
});
|
2026-04-28 12:50:45 +02:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
|
this.path = options;
|
2026-09-15 13:00:32 +02:00
|
|
|
}
|
|
|
|
|
else if (typeof options === 'object') {
|
2026-04-28 12:50:45 +02:00
|
|
|
if (options.path) {
|
|
|
|
|
this.path = options.path;
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(options.args)) {
|
|
|
|
|
this.args = options.args;
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-15 07:32:52 +02:00
|
|
|
this.winbreak = ['win', 'windows', 'dos', '\r\n'].includes((options.newline || '').toString().toLowerCase());
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* <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();
|
2020-02-29 23:01:03 +01:00
|
|
|
let returned;
|
2020-11-12 16:31:20 +01:00
|
|
|
const hasInvalidAddresses = []
|
|
|
|
|
.concat(envelope.from || [])
|
|
|
|
|
.concat(envelope.to || [])
|
2026-09-07 07:05:39 +02:00
|
|
|
// a local part is either a dot-atom or a quoted-string, so a leading dash sits at
|
|
|
|
|
// offset 0 or, behind the opening quote, at offset 1. Only the first shape is read
|
|
|
|
|
// as an option by sendmail, but both are the address this guard keeps out of argv
|
|
|
|
|
.some(addr => /^"?-/.test(addr));
|
2020-11-12 16:31:20 +01:00
|
|
|
if (hasInvalidAddresses) {
|
2026-04-28 12:50:45 +02:00
|
|
|
const err = new Error('Can not send mail. Invalid envelope addresses.');
|
2026-02-05 08:49:11 +01:00
|
|
|
err.code = errors.ESENDMAIL;
|
|
|
|
|
return done(err);
|
2020-11-12 16:31:20 +01:00
|
|
|
}
|
2026-04-28 12:50:45 +02:00
|
|
|
// force -i to keep single dots
|
|
|
|
|
const args = this.args
|
|
|
|
|
? ['-i'].concat(this.args).concat(envelope.to)
|
|
|
|
|
: ['-i'].concat(envelope.from ? ['-f', envelope.from] : []).concat(envelope.to);
|
2026-09-15 13:00:32 +02:00
|
|
|
const callback = (err) => {
|
2020-02-29 23:01:03 +01:00
|
|
|
if (returned) {
|
|
|
|
|
// ignore any additional responses, already done
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
returned = true;
|
|
|
|
|
if (typeof done === 'function') {
|
|
|
|
|
if (err) {
|
|
|
|
|
return done(err);
|
|
|
|
|
}
|
2026-04-28 12:50:45 +02:00
|
|
|
return done(null, {
|
|
|
|
|
envelope,
|
|
|
|
|
messageId,
|
|
|
|
|
response: 'Messages queued for delivery'
|
|
|
|
|
});
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
|
|
|
|
};
|
2026-04-28 12:50:45 +02:00
|
|
|
let sendmail;
|
2020-02-29 23:01:03 +01:00
|
|
|
try {
|
|
|
|
|
sendmail = this._spawn(this.path, args);
|
2026-09-15 13:00:32 +02:00
|
|
|
}
|
|
|
|
|
catch (E) {
|
|
|
|
|
this.logger.error({
|
|
|
|
|
err: E,
|
|
|
|
|
tnx: 'spawn',
|
|
|
|
|
messageId
|
|
|
|
|
}, 'Error occurred while spawning sendmail. %s', E.message);
|
2020-02-29 23:01:03 +01:00
|
|
|
return callback(E);
|
|
|
|
|
}
|
|
|
|
|
if (sendmail) {
|
|
|
|
|
sendmail.on('error', err => {
|
2026-09-15 13:00:32 +02:00
|
|
|
this.logger.error({
|
|
|
|
|
err,
|
|
|
|
|
tnx: 'spawn',
|
|
|
|
|
messageId
|
|
|
|
|
}, 'Error occurred when sending message %s. %s', messageId, err.message);
|
2020-02-29 23:01:03 +01:00
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
sendmail.once('exit', code => {
|
|
|
|
|
if (!code) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2026-09-15 13:00:32 +02:00
|
|
|
const err = new Error(code === 127 ? 'Sendmail command not found, process exited with code ' + code : 'Sendmail exited with code ' + code);
|
2026-02-05 08:49:11 +01:00
|
|
|
err.code = errors.ESENDMAIL;
|
2026-09-15 13:00:32 +02:00
|
|
|
this.logger.error({
|
|
|
|
|
err,
|
|
|
|
|
tnx: 'stdin',
|
|
|
|
|
messageId
|
|
|
|
|
}, 'Error sending message %s to sendmail. %s', messageId, err.message);
|
2020-02-29 23:01:03 +01:00
|
|
|
callback(err);
|
|
|
|
|
});
|
2026-09-15 13:00:32 +02:00
|
|
|
// the close listener is handed the exit code as its first argument, so a non-zero
|
|
|
|
|
// code reaching it before the exit listener did counts as the error value
|
2020-02-29 23:01:03 +01:00
|
|
|
sendmail.once('close', callback);
|
|
|
|
|
sendmail.stdin.on('error', err => {
|
2026-09-15 13:00:32 +02:00
|
|
|
this.logger.error({
|
|
|
|
|
err,
|
|
|
|
|
tnx: 'stdin',
|
|
|
|
|
messageId
|
|
|
|
|
}, 'Error occurred when piping message %s to sendmail. %s', messageId, err.message);
|
2020-02-29 23:01:03 +01:00
|
|
|
callback(err);
|
|
|
|
|
});
|
2026-04-28 12:50:45 +02:00
|
|
|
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
|
|
|
|
|
}, 'Sending message %s to <%s>', messageId, recipients.join(', '));
|
2026-04-28 12:50:45 +02:00
|
|
|
const sourceStream = mail.message.createReadStream();
|
2026-06-15 07:32:52 +02:00
|
|
|
let stream = sourceStream;
|
|
|
|
|
if (this.options.newline) {
|
|
|
|
|
// apply the transport-level line ending transform; the message-level
|
|
|
|
|
// `newline` option is handled by MimeNode in createReadStream()
|
2026-09-15 13:00:32 +02:00
|
|
|
stream = sourceStream.pipe(this.winbreak ? new le_windows_js_1.default() : new le_unix_js_1.default());
|
2026-06-15 07:32:52 +02:00
|
|
|
sourceStream.once('error', err => stream.emit('error', err));
|
|
|
|
|
}
|
|
|
|
|
stream.once('error', err => {
|
2026-09-15 13:00:32 +02:00
|
|
|
this.logger.error({
|
|
|
|
|
err,
|
|
|
|
|
tnx: 'stdin',
|
|
|
|
|
messageId
|
|
|
|
|
}, 'Error occurred when generating message %s. %s', messageId, err.message);
|
2020-02-29 23:01:03 +01:00
|
|
|
sendmail.kill('SIGINT'); // do not deliver the message
|
|
|
|
|
callback(err);
|
|
|
|
|
});
|
2026-06-15 07:32:52 +02:00
|
|
|
stream.pipe(sendmail.stdin);
|
2026-09-15 13:00:32 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2026-04-28 12:50:45 +02:00
|
|
|
const err = new Error('sendmail was not found');
|
2026-02-05 08:49:11 +01:00
|
|
|
err.code = errors.ESENDMAIL;
|
|
|
|
|
return callback(err);
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-15 13:00:32 +02:00
|
|
|
exports.default = SendmailTransport;
|
|
|
|
|
module.exports = exports.default;
|
|
|
|
|
Object.defineProperty(module.exports, 'default', { value: exports.default, enumerable: false, writable: true, configurable: true });
|