fix: Refactor sendmail parameter handling and update related tests
This commit is contained in:
parent
4e8292f43c
commit
4c06f874c9
|
|
@ -220,13 +220,6 @@ class PHPMailer
|
|||
*/
|
||||
public $Sendmail = '/usr/sbin/sendmail';
|
||||
|
||||
/**
|
||||
* Parsed sendmail parameters extracted from sendmail_path.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private $SendmailParams = [];
|
||||
|
||||
/**
|
||||
* Whether mail() uses a fully sendmail-compatible MTA.
|
||||
* One which supports sendmail's "-oi -f" options.
|
||||
|
|
@ -1021,7 +1014,6 @@ class PHPMailer
|
|||
for ($i = 0; $i < count($parts); ++$i) {
|
||||
$part = $parts[$i];
|
||||
if (preg_match('/^-(i|t)$/', $part, $matches)) {
|
||||
$this->SendmailParams[$matches[0]] = '';
|
||||
continue;
|
||||
}
|
||||
if (preg_match('/^-f(.*)$/', $part, $matches)) {
|
||||
|
|
@ -1029,10 +1021,8 @@ class PHPMailer
|
|||
if ($address === '' && isset($parts[$i + 1]) && strpos($parts[$i + 1], '-') !== 0) {
|
||||
$address = $parts[++$i];
|
||||
}
|
||||
if (static::validateAddress($address)) {
|
||||
$this->SendmailParams['-f'] = $address;
|
||||
continue;
|
||||
}
|
||||
$this->Sender = $address;
|
||||
continue;
|
||||
}
|
||||
|
||||
$remainder[] = $part;
|
||||
|
|
@ -1923,22 +1913,15 @@ class PHPMailer
|
|||
// Also don't add the -f automatically unless it has been set either via Sender
|
||||
// or sendmail_path. Otherwise it can introduce new problems.
|
||||
// @see http://github.com/PHPMailer/PHPMailer/issues/2298
|
||||
if (isset($this->SendmailParams['-f']) && self::isShellSafe($this->SendmailParams['-f'])) {
|
||||
$sendmailArgs[] = '-f' . $this->SendmailParams['-f'];
|
||||
} elseif (!empty($this->Sender) && static::validateAddress($this->Sender) && self::isShellSafe($this->Sender)) {
|
||||
if (!empty($this->Sender) && static::validateAddress($this->Sender) && self::isShellSafe($this->Sender)) {
|
||||
$sendmailArgs[] = '-f' . $this->Sender;
|
||||
}
|
||||
|
||||
// Qmail doesn't accept all the sendmail parameters
|
||||
// @see https://github.com/PHPMailer/PHPMailer/issues/3189
|
||||
if ($this->Mailer !== 'qmail') {
|
||||
if (isset($this->SendmailParams['-i'])) {
|
||||
$sendmailArgs[] = '-i';
|
||||
}
|
||||
|
||||
if (isset($this->SendmailParams['-t'])) {
|
||||
$sendmailArgs[] = '-t';
|
||||
}
|
||||
$sendmailArgs[] = '-i';
|
||||
$sendmailArgs[] = '-t';
|
||||
}
|
||||
|
||||
$resultArgs = (empty($sendmailArgs) ? '' : ' ' . implode(' ', $sendmailArgs));
|
||||
|
|
@ -2127,8 +2110,8 @@ class PHPMailer
|
|||
$this->Sender = ini_get('sendmail_from');
|
||||
}
|
||||
if (!empty($this->Sender) && static::validateAddress($this->Sender)) {
|
||||
$this->parseSendmailPath(ini_get('sendmail_path'));
|
||||
if (self::isShellSafe($this->Sender) && !isset($this->SendmailParams['-f'])) {
|
||||
$phpmailer_path = ini_get('sendmail_path');
|
||||
if (self::isShellSafe($this->Sender) && strpos($phpmailer_path, '-f') === false) {
|
||||
$params = sprintf('-f%s', $this->Sender);
|
||||
}
|
||||
$old_from = ini_get('sendmail_from');
|
||||
|
|
|
|||
|
|
@ -184,9 +184,9 @@ final class MailTransportTest extends SendTestCase
|
|||
*
|
||||
* @param string $sendmailPath The sendmail path to parse.
|
||||
* @param string $expectedCommand The expected command after parsing.
|
||||
* @param array $expectedParams The expected parameters after parsing.
|
||||
* @param string $expectedSender The expected Sender (-f parameter) after parsing.
|
||||
*/
|
||||
public function testParseSendmailPath($sendmailPath, $expectedCommand, array $expectedParams)
|
||||
public function testParseSendmailPath($sendmailPath, $expectedCommand, $expectedSender)
|
||||
{
|
||||
$mailer = $this->Mail;
|
||||
|
||||
|
|
@ -199,21 +199,18 @@ final class MailTransportTest extends SendTestCase
|
|||
);
|
||||
$command = $parseSendmailPath($sendmailPath);
|
||||
|
||||
$getSendmailParams = \Closure::bind(
|
||||
function () {
|
||||
return $this->{'SendmailParams'};
|
||||
},
|
||||
$mailer,
|
||||
\PHPMailer\PHPMailer\PHPMailer::class
|
||||
);
|
||||
$params = $getSendmailParams();
|
||||
|
||||
self::assertSame($expectedCommand, $command);
|
||||
self::assertSame($expectedParams, $params);
|
||||
self::assertSame($expectedCommand, $command, 'Sendmail command not parsed correctly');
|
||||
self::assertSame($expectedSender, $mailer->Sender, 'Sender property not set correctly');
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testParseSendmailPath.
|
||||
*
|
||||
* @return array<string, array<int, string>> {
|
||||
* @type string $0 The sendmail path to parse.
|
||||
* @type string $1 The expected command after parsing.
|
||||
* @type string $2 The expected Sender (-f parameter) after parsing.
|
||||
* }
|
||||
*/
|
||||
|
||||
public function sendmailPathProvider()
|
||||
|
|
@ -222,27 +219,27 @@ final class MailTransportTest extends SendTestCase
|
|||
'path only' => [
|
||||
'/usr/sbin/sendmail',
|
||||
'/usr/sbin/sendmail',
|
||||
[],
|
||||
''
|
||||
],
|
||||
'with i and t' => [
|
||||
'/usr/sbin/sendmail -i -t',
|
||||
'/usr/sbin/sendmail',
|
||||
['-i' => '', '-t' => ''],
|
||||
''
|
||||
],
|
||||
'with f concatenated' => [
|
||||
'/usr/sbin/sendmail -frpath@example.org -i',
|
||||
'/usr/sbin/sendmail',
|
||||
['-f' => 'rpath@example.org', '-i' => ''],
|
||||
'rpath@example.org'
|
||||
],
|
||||
'with f separated' => [
|
||||
'/usr/sbin/sendmail -f rpath@example.org -t',
|
||||
'/usr/sbin/sendmail',
|
||||
['-f' => 'rpath@example.org', '-t' => ''],
|
||||
'rpath@example.org',
|
||||
],
|
||||
'with extra flags preserved' => [
|
||||
'/opt/sendmail -fuser@example.org -x -y',
|
||||
'/opt/sendmail -x -y -fuser@example.org',
|
||||
'/opt/sendmail -x -y',
|
||||
['-f' => 'user@example.org'],
|
||||
'user@example.org',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue