Fold the To list passed to mail()

mailSend() joined the To addresses for mail() with its own implode, so
a long list still went out as one line, while the To header used for
getSentMIMEMessage() and DKIM signing is folded by addrAppend(). mail()
keeps CRLF + space folds in its to argument, so build it with
addrAppend() too.
This commit is contained in:
dualfroz committed 2026-10-04 12:54:41 +02:00
1 parent 95d2378b6a
commit 1ac61eecb6
2 files changed
+39 -1

No files matched your search

+2 -1
View File
@@ -2090,7 +2090,8 @@ class PHPMailer
foreach ($this->to as $toaddr) { foreach ($this->to as $toaddr) {
$toArr[] = $this->addrFormat($toaddr); $toArr[] = $this->addrFormat($toaddr);
} }
$to = trim(implode(', ', $toArr)); //Fold long lists the same way as the To header, mail() keeps CRLF + WSP folds intact
$to = trim(substr($this->addrAppend('To', $this->to), strlen('To:')));
//If there are no To-addresses (e.g. when sending only to BCC-addresses) //If there are no To-addresses (e.g. when sending only to BCC-addresses)
//the following should be added to get a correct DKIM-signature. //the following should be added to get a correct DKIM-signature.
+37
View File
@@ -13,6 +13,7 @@
namespace PHPMailer\Test\PHPMailer; namespace PHPMailer\Test\PHPMailer;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\Test\SendTestCase; use PHPMailer\Test\SendTestCase;
/** /**
@@ -117,6 +118,42 @@ final class MailTransportTest extends SendTestCase
self::assertStringNotContainsString("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers'); self::assertStringNotContainsString("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
} }
/**
* Test that a long To list is folded before it is passed to mail().
*
* @covers \PHPMailer\PHPMailer\PHPMailer::mailSend
*/
public function testMailSendFoldsLongToList()
{
$this->Mail->Body = 'Sending via mail()';
$this->buildBody();
$this->Mail->Subject = $this->Mail->Subject . ': mail()';
$this->Mail->clearAddresses();
for ($i = 1; $i <= 100; ++$i) {
$this->Mail->addAddress('recipient' . $i . '@example.com', 'Recipient ' . $i);
}
$to = '';
$this->Mail->Debugoutput = static function ($str) use (&$to) {
if (strpos($str, 'To: ') === 0) {
$to = substr($str, 4);
}
};
$this->Mail->isMail();
self::assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
$lines = explode(PHPMailer::getLE(), 'To: ' . $to);
self::assertGreaterThan(1, count($lines), 'The To list was not folded');
foreach ($lines as $index => $line) {
self::assertLessThanOrEqual(PHPMailer::MAX_LINE_LENGTH, strlen($line), "Line $index is too long");
}
self::assertStringContainsString(
'To: ' . $to . PHPMailer::getLE(),
$this->Mail->getSentMIMEMessage(),
'mail() did not get the same To list as the message header'
);
}
/** /**
* Test sending using PHP mail() function with Sender address * Test sending using PHP mail() function with Sender address
* and explicit sendmail_from ini set. * and explicit sendmail_from ini set.