From 98e93afb32547355b0b46233d1fdbd0af7c84248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammet=20=C5=9Eafak?= Date: Wed, 25 Nov 2026 08:00:03 +0300 Subject: [PATCH 1/2] fix: give a custom To header its 3.0.0 behaviour back The previous change made setHeader('To') case-insensitive, kept it when the library wrote no To, and so turned it into a new recipient on sendmail. To is no longer special-cased on any transport: the library's own To line replaces a custom header named exactly To and sits beside other spellings, as in 3.0.0, including in BCC batches. Cc and Bcc keep losing to the library's own header. A matrix test compares To/Cc/Bcc x six name spellings x mail()/sendmail/SMTP x single message/BCC batches against output recorded from the 3.0.0 tag. Header names may again have spaces around them, as in 3.0.0. The new public helpers on EmailValidator and HeaderRenderer are marked @internal. --- CHANGELOG.md | 18 +- docs/API-Reference.md | 13 +- docs/Encoding-and-Headers.md | 11 +- docs/Sending-Mail.md | 32 +- docs/Transports.md | 6 + src/Mailer.php | 23 +- src/Message/HeaderRenderer.php | 14 +- src/Transport/MailTransport.php | 8 +- src/Transport/SendmailTransport.php | 11 +- src/Transport/SmtpTransport.php | 10 +- src/Validation/EmailValidator.php | 6 + tests/CustomRecipientHeadersTest.php | 61 +--- tests/HeaderInjectionTest.php | 2 +- tests/RecipientHeaderMatrixTest.php | 355 +++++++++++++++++------ tests/Support/RespondingSocketClient.php | 79 +++++ 15 files changed, 442 insertions(+), 207 deletions(-) create mode 100644 tests/Support/RespondingSocketClient.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e780e08..15c38a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,15 +42,15 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). discarded. - A `<` or `>` inside an address is rejected (even with `validate` off): it could close the SMTP path and add parameters (`RCPT TO: NOTIFY=NEVER>`). -- `setHeader()`: a header the library writes itself for the message wins over a - custom one of the same name whatever the case (a custom `from`, or `bcc` next - to a real BCC list, is dropped instead of added as a second header). A header - the library does not write is kept as before: `setHeader('Cc'|'Bcc'|'To', …)` - without `setCC()`/`setBCC()`/`setTo()` still adds that header. (3.0.0 lost a - custom `To` on sendmail and SMTP when the message had no To recipient; it is now - kept there too. `sendmail -t` reads recipients from the headers, so such a - hand-set `To` is now also a recipient on sendmail.) A name with a space, a - colon or any non-printable or non-ASCII character throws a `MailerException`. +- `setHeader()`: a header the library writes itself for the message (`Cc` with CC + recipients, `Bcc` with BCC recipients, `Subject`, `From`, …) wins over a custom + one of the same name whatever the case or the spacing (a custom `from`, or `bcc` + next to a real BCC list, is dropped instead of added as a second header). A + header the library does not write is kept as before. `To` is not special-cased: + a custom `To` behaves exactly as in 3.0.0 on `mail()`, sendmail and SMTP, + including in BCC batches, so it adds no recipient that 3.0.0 did not. A name + with a tab, an inner space, a colon, or any non-printable or non-ASCII + character throws a `MailerException`; spaces around a name are still accepted. - `setAltMessage()` strips carriage returns like `setMessage()`. The SMTP dot-stuffing now also handles a bare `"\r"` or `"\n\r"` newline. - A `"` or `\` in an attachment name is escaped inside the quoted `name` diff --git a/docs/API-Reference.md b/docs/API-Reference.md index 17b26e9..69cb98f 100644 --- a/docs/API-Reference.md +++ b/docs/API-Reference.md @@ -85,13 +85,14 @@ Promotes a previously added attachment (matched by its path/name) to an inline ### `setHeader(string $header, string $value): self` Adds a custom header. CR, LF and NUL are stripped from the value. A header the -library writes for the message itself (From, Subject, To with To recipients, -Cc with CC recipients, -Bcc with BCC recipients on sendmail and `mail()`, Date, Message-ID, …) wins over -a custom one of the same name, whatever the case; one it does not write is kept -as you set it. Use `setCC()`/`setBCC()` for recipients, not headers. +library writes for the message itself (From, Subject, Cc with CC recipients, Bcc +with BCC recipients on sendmail and `mail()`, Date, Message-ID, …) wins over a +custom one of the same name, whatever the case or spacing; one it does not write +is kept as you set it. `To` is not special-cased and behaves as it always did +(see [Custom headers](Sending-Mail.md#custom-headers)). Use +`setTo()`/`setCC()`/`setBCC()` for recipients, not headers. **Throws** `MailerException` when the header name is not printable ASCII -without `:` or spaces. +without `:`, a tab or an inner space. ### `setPriority(int $n = 3): self` `X-Priority`, 1 (highest)–5 (lowest); out-of-range → 3. diff --git a/docs/Encoding-and-Headers.md b/docs/Encoding-and-Headers.md index 9973444..b8d0b91 100644 --- a/docs/Encoding-and-Headers.md +++ b/docs/Encoding-and-Headers.md @@ -89,11 +89,12 @@ Out-of-range values fall back to 3. ## Custom headers Add any header with `setHeader()`. CR, LF and NUL are stripped from the value to -prevent header injection, a header name that is not printable ASCII without `:` -or spaces throws a `MailerException`. The headers the library writes itself -(From, To, Subject, Date, Message-ID, MIME-Version, X-Mailer, …, and Cc/Bcc when -there are such recipients) win over a custom header of the same name, whatever -the case; a header the library does not write is kept. See +prevent header injection, and a header name that is not printable ASCII without +`:`, a tab or an inner space throws a `MailerException`. The headers the library +writes itself (From, Subject, Date, Message-ID, MIME-Version, X-Mailer, …, and +Cc/Bcc when there are such recipients) win over a custom header of the same name, +whatever the case or spacing; a header the library does not write is kept. `To` +is not special-cased and behaves as it always did. See [Sending Mail](Sending-Mail.md#custom-headers). ```php diff --git a/docs/Sending-Mail.md b/docs/Sending-Mail.md index 541c0e3..1979815 100644 --- a/docs/Sending-Mail.md +++ b/docs/Sending-Mail.md @@ -121,24 +121,32 @@ repeatedly. If any batch fails, the failure is thrown. Add any header with `setHeader()`. CR, LF and NUL are stripped from the value to prevent header injection (a trailing newline in a value is harmless; it is just -removed). A header *name* must be printable ASCII without `:` or spaces, or -`setHeader()` throws a `MailerException`. +removed). A header *name* must be printable ASCII without `:`, a tab or an inner +space (spaces around the name are allowed), or `setHeader()` throws a +`MailerException`. If the library writes a header itself for this message, its value wins over a -custom header of the same name, whatever the case of the name: a custom `from` -is dropped, not added next to the real `From`. That covers From, Reply-To, -Return-Path, Date, Message-ID, MIME-Version, User-Agent, X-Mailer, X-Sender, -X-Priority, Content-Type and Content-Transfer-Encoding; `Subject`; `To` when you -called `setTo()`; `Cc` when you called `setCC()`; and `Bcc` when you called -`setBCC()` (sendmail and `mail()` write it; SMTP never does). A header the -library does **not** write is passed through as you set it: `setHeader('Cc', …)` -with no `setCC()`, or `setHeader('To', 'undisclosed-recipients:;')` on a message -that only has BCC recipients, adds that header. +custom header of the same name, whatever the case or the spacing of the name: a +custom `from` is dropped, not added next to the real `From`. That covers From, +Reply-To, Return-Path, Date, Message-ID, MIME-Version, User-Agent, X-Mailer, +X-Sender, X-Priority, Content-Type, Content-Transfer-Encoding and `Subject`; +`Cc` when you called `setCC()`; and `Bcc` when you called `setBCC()` (sendmail +and `mail()` write it; SMTP never does). A header the library does **not** write +is passed through as you set it: `setHeader('Cc', …)` with no `setCC()` adds that +header. + +**`To` is the exception: it is not treated specially.** A custom `To` behaves as +it always did. With `sendmail` and `smtp` the library's own `To` line replaces a +custom header named exactly `To` (so a message with no To recipient loses it), +and a custom header spelled differently (`to`, `TO`, `To `) is written next to +it. With `mail()` the recipients go in the function's own argument and a custom +`To` is written as an extra header. Do not use `setHeader('To', …)` to add +recipients; use `setTo()`. Use `setTo()`, `setCC()` and `setBCC()` for recipients, and know what a header set by hand does on each transport. With `mail()` and `sendmail` the MTA reads the recipients from the headers, so a `To`, `Cc` or `Bcc` header set by hand -**does** add a recipient (and a `Bcc` header set by hand on `sendmail` is removed +**can** add a recipient (and a `Bcc` header set by hand on `sendmail` is removed from the delivered copy like any other `Bcc`). With BCC batching the header is repeated in every batch, so that address gets one copy per batch. A group such as `undisclosed-recipients:;` adds no recipient. With `smtp` only the `RCPT TO` list counts: diff --git a/docs/Transports.md b/docs/Transports.md index 84c7297..e8f5f1f 100644 --- a/docs/Transports.md +++ b/docs/Transports.md @@ -92,6 +92,12 @@ each transport does the right thing: never appear in the headers** (a `Bcc` header you add by hand with `setHeader()` does; see [Sending Mail](Sending-Mail.md#custom-headers)). +A `To`, `Cc` or `Bcc` header you add by hand with `setHeader()` is not a recipient +on `smtp`, but `mail()` and `sendmail` read recipients from the headers, so there +it can be one. A custom `To` is never replaced case-insensitively: it behaves as it +always did on every transport. See +[Custom headers](Sending-Mail.md#custom-headers) before passing untrusted input. + ## Supplying your own transport Any object implementing `TransportInterface` can be injected with diff --git a/src/Mailer.php b/src/Mailer.php index a499ef0..b59deb0 100644 --- a/src/Mailer.php +++ b/src/Mailer.php @@ -87,7 +87,7 @@ class Mailer * builder; a custom header with one of these names is always dropped. The * other headers the library writes are known only while the message is built * (see {@see buildGeneralHeaders()}): `Cc` is written only when there are CC - * recipients, and `To`, `Subject` and `Bcc` are left to the transport. + * recipients, and `Subject` and `Bcc` are left to the transport. */ private const CONTENT_HEADERS = ['content-type', 'content-transfer-encoding']; @@ -217,13 +217,14 @@ public function clear(bool $clearAttachments = false): self /** * Adds an arbitrary header (for example `List-Unsubscribe`). CR, LF and NUL * are stripped from the value to prevent header injection; a header name - * that is not made of printable ASCII characters other than `:` is - * rejected. A header the library writes itself for the message (From, - * Reply-To, Date, Message-ID, Cc when there are CC recipients, To, Subject, - * Bcc when there are BCC recipients, …) takes precedence over a custom header - * of the same name, whatever the case; use setCC() and setBCC() for - * recipients. A custom `Cc`, `Bcc` or `To` the library does not write (no CC, - * BCC or To recipients) is passed through unchanged. + * that is not made of printable ASCII characters other than `:` (spaces + * around it are allowed) is rejected. A header the library writes itself for + * the message (From, Reply-To, Date, Message-ID, Subject, Cc when there are + * CC recipients, Bcc when there are BCC recipients, …) takes precedence over + * a custom header of the same name, whatever the case or the spacing; use + * setCC() and setBCC() for recipients. A custom `Cc` or `Bcc` the library + * does not write (no CC or BCC recipients) is passed through unchanged. `To` + * is not special-cased: a custom `To` behaves as it did in 3.0.0. * * @throws MailerException When the header name is not a valid field name. */ @@ -232,7 +233,7 @@ public function setHeader(string $header, string $value): self if (!HeaderRenderer::isValidName($header)) { throw new MailerException( 'A header name must be made of printable ASCII characters other than ":" ' - . '(no spaces, line breaks or NUL).', + . '(spaces are allowed only around it; no line breaks or NUL).', ); } @@ -691,10 +692,6 @@ private function buildGeneralHeaders(Address $from, Address $replyTo): array // A custom header the library also writes loses, whatever the case of // its name. One the library does not write for this message is kept. $managed = HeaderRenderer::names($headers); - if ($this->to !== []) { - // The transport writes To (or passes it to mail()) for the whole message. - $managed['to'] = true; - } $custom = []; foreach ($this->customHeaders as $name => $value) { $key = strtolower(trim((string) $name)); diff --git a/src/Message/HeaderRenderer.php b/src/Message/HeaderRenderer.php index dd09e40..d7b9612 100644 --- a/src/Message/HeaderRenderer.php +++ b/src/Message/HeaderRenderer.php @@ -58,6 +58,8 @@ public static function render(array $headers, string $newline): string * The set of header names in `$headers`, lower-cased and trimmed, as the keys * of an array (`['from' => true, …]`). * + * @internal + * * @param array $headers * * @return array @@ -77,6 +79,8 @@ public static function names(array $headers): array * to case or surrounding whitespace. Transports use it to drop a custom * header they are about to write themselves. * + * @internal + * * @param array $headers * @param list $names * @@ -101,14 +105,20 @@ public static function without(array $headers, array $names): array /** * Whether `$name` is usable as a header field name: printable ASCII other - * than `:`, so no space, line break, NUL or colon. + * than `:`, optionally with spaces around it (`To ` was accepted before, and + * RFC 5322 allows whitespace before the colon). A line break, a NUL, a tab, + * a colon or an inner space is not. + * + * @internal */ public static function isValidName(string $name): bool { - return preg_match('/\A[\x21-\x39\x3B-\x7E]+\z/', $name) === 1; + return preg_match('/\A {0,}[\x21-\x39\x3B-\x7E]+ {0,}\z/', $name) === 1; } /** + * @internal + * * @throws MailerException */ public static function assertSafe(string $name, string $value): void diff --git a/src/Transport/MailTransport.php b/src/Transport/MailTransport.php index ac3e684..a2119b2 100644 --- a/src/Transport/MailTransport.php +++ b/src/Transport/MailTransport.php @@ -59,12 +59,10 @@ public function send(PreparedEmail $email): void EmailValidator::assertSafeAddresses([...$email->to, ...$email->bcc]); HeaderRenderer::assertSafe('Subject', $email->subject); - // To (when there are recipients) and Subject travel as mail() arguments, - // so a custom header of that name would only duplicate them. + // Subject travels as a mail() argument, so a custom header of that name + // would only duplicate it; a custom Bcc gives way to the real one. To is + // left alone, as it always was. $drop = ['Subject']; - if ($email->to !== []) { - $drop[] = 'To'; - } if ($email->bcc !== []) { $drop[] = 'Bcc'; } diff --git a/src/Transport/SendmailTransport.php b/src/Transport/SendmailTransport.php index cdaad3e..3d86e86 100644 --- a/src/Transport/SendmailTransport.php +++ b/src/Transport/SendmailTransport.php @@ -55,18 +55,15 @@ public function send(PreparedEmail $email): void $from = $email->from->getEmail(); $fromArg = $this->validator->isShellSafe($from) ? '-f ' . $from : ''; - // A custom To or Bcc only gives way when the library writes one. + // A custom Subject, and a custom Bcc when there are BCC recipients, give + // way to the real header. To is written as it always was: it replaces a + // custom header named exactly "To" and sits beside any other spelling. $drop = ['Subject']; - if ($email->to !== []) { - $drop[] = 'To'; - } if ($email->bcc !== []) { $drop[] = 'Bcc'; } $headers = HeaderRenderer::without($email->headers, $drop); - if ($email->to !== []) { - $headers['To'] = implode(', ', $email->to); - } + $headers['To'] = implode(', ', $email->to); $headers['Subject'] = $email->subject; if ($email->bcc !== []) { $headers['Bcc'] = implode(', ', $email->bcc); diff --git a/src/Transport/SmtpTransport.php b/src/Transport/SmtpTransport.php index 7d2f5f9..2dc12aa 100644 --- a/src/Transport/SmtpTransport.php +++ b/src/Transport/SmtpTransport.php @@ -75,11 +75,11 @@ public function send(PreparedEmail $email): void throw new TransportException('The SMTP client hostname must not contain a line break or a NUL character.'); } - // A custom To only gives way when the library writes one. - $headers = HeaderRenderer::without($email->headers, $email->to === [] ? ['Subject'] : ['To', 'Subject']); - if ($email->to !== []) { - $headers['To'] = implode(', ', $email->to); - } + // A custom Subject gives way to the real one. To is written as it always + // was: it replaces a custom header named exactly "To" and sits beside any + // other spelling. + $headers = HeaderRenderer::without($email->headers, ['Subject']); + $headers['To'] = implode(', ', $email->to); $headers['Subject'] = $email->subject; $message = HeaderRenderer::render($headers, $this->newline) . $email->contentHeaders . $this->newline . $this->newline . $email->body; diff --git a/src/Validation/EmailValidator.php b/src/Validation/EmailValidator.php index ca89089..8f47bd2 100644 --- a/src/Validation/EmailValidator.php +++ b/src/Validation/EmailValidator.php @@ -66,6 +66,8 @@ public function assertAllValid(iterable $emails): void * Whether the value contains a CR, LF or NUL character. Such a character * ends a header line or an SMTP command, so it must never reach either; the * check does not depend on the `validate` option. + * + * @internal Shared by the library's own classes; not a public extension point. */ public static function hasLineBreakOrNul(string $value): bool { @@ -76,6 +78,8 @@ public static function hasLineBreakOrNul(string $value): bool * Whether the address contains a character that would end or extend an SMTP * path or a header line: CR, LF, NUL, `<` or `>`. An address that has one * could add parameters (` SIZE=1`) to `MAIL FROM` / `RCPT TO`. + * + * @internal Shared by the library's own classes; not a public extension point. */ public static function hasUnsafeAddressChar(string $value): bool { @@ -86,6 +90,8 @@ public static function hasUnsafeAddressChar(string $value): bool * Throws when any address contains a character that {@see hasUnsafeAddressChar()} * reports. Checked whether or not address validation is on. * + * @internal Shared by the library's own classes; not a public extension point. + * * @param iterable $emails * * @throws InvalidAddressException diff --git a/tests/CustomRecipientHeadersTest.php b/tests/CustomRecipientHeadersTest.php index 85cf719..7dc99c4 100644 --- a/tests/CustomRecipientHeadersTest.php +++ b/tests/CustomRecipientHeadersTest.php @@ -18,9 +18,10 @@ use function preg_match; /** - * `setHeader('Cc'|'Bcc'|'To'|'Subject')` keeps working when the library does - * not write that header itself, and loses to the library's own header when it - * does, whatever the case of the name. The "kept" cases below produce the same + * `setHeader('Cc'|'Bcc')` keeps working when the library does not write that + * header itself, and loses to the library's own header (and `Subject` always + * does) whatever the case of the name. `To` is covered by + * {@see RecipientHeaderMatrixTest}. The "kept" cases below produce the same * lines as 3.0.0; the "wins" cases are the intended difference (one header, not * two). */ @@ -147,17 +148,15 @@ public function testSendmailRealBccWinsWhateverTheCaseOfTheCustomName(): void self::assertSame(['Bcc: real@example.com'], $this->lines($message, 'Bcc')); } - public function testSendmailRealCcToAndSubjectWinWhateverTheCase(): void + public function testSendmailRealCcAndSubjectWinWhateverTheCase(): void { $message = $this->viaSendmail( static fn(Mailer $m) => $m->setTo('t@example.com')->setCC('real@example.com') ->setHeader('CC', 'archive@co.example') - ->setHeader('to', 'other@co.example') ->setHeader('SUBJECT', 'evil'), ); self::assertSame(['Cc: real@example.com'], $this->lines($message, 'Cc')); - self::assertSame(['To: t@example.com'], $this->lines($message, 'To')); self::assertCount(1, $this->lines($message, 'Subject')); self::assertStringNotContainsString('evil', $message); } @@ -174,18 +173,16 @@ public function testSmtpKeepsACustomCcAndBccTheTransportDoesNotWrite(): void self::assertSame(['Cc: c@co.example'], $this->lines($data, 'Cc')); } - public function testSmtpRealCcToAndSubjectWinWhateverTheCase(): void + public function testSmtpRealCcAndSubjectWinWhateverTheCase(): void { $data = $this->viaSmtp( static fn(Mailer $m) => $m->setTo('t@example.com')->setCC('real@example.com') ->setHeader('cC', 'archive@co.example') - ->setHeader('tO', 'other@co.example') ->setHeader('subject', 'evil'), 2, ); self::assertSame(['Cc: real@example.com'], $this->lines($data, 'Cc')); - self::assertSame(['To: t@example.com'], $this->lines($data, 'To')); self::assertCount(1, $this->lines($data, 'Subject')); self::assertStringNotContainsString('evil', $data); } @@ -201,50 +198,4 @@ public function testMailKeepsACustomCcAndRealBccWins(): void self::assertSame(['Bcc: real@example.com'], $this->lines($headers, 'Bcc')); self::assertSame(['Cc: c@co.example'], $this->lines($headers, 'Cc')); } - - public function testBccBatchesAfterTheFirstDoNotCarryACustomToWhenTheMessageHasATo(): void - { - $messages = []; - $mailer = Mailer::newInstance(['newline' => "\n"])->setTransport(new SendmailTransport( - '/usr/sbin/sendmail', - new EmailValidator(), - static function (string $command, string $raw) use (&$messages): int { - $messages[] = $raw; - - return 0; - }, - )); - $mailer->setFrom('s@example.com')->setSubject('Subject')->setMessage('Body') - ->setTo('t@example.com') - ->setBCC(['b1@example.com', 'b2@example.com', 'b3@example.com'], 2) - ->setHeader('To', 'other@co.example') - ->send(); - - self::assertCount(2, $messages); - self::assertSame(['To: t@example.com'], $this->lines($messages[0], 'To')); - self::assertSame([], $this->lines($messages[1], 'To')); - } - - public function testBccOnlyBatchesKeepACustomTo(): void - { - $messages = []; - $mailer = Mailer::newInstance(['newline' => "\n"])->setTransport(new SendmailTransport( - '/usr/sbin/sendmail', - new EmailValidator(), - static function (string $command, string $raw) use (&$messages): int { - $messages[] = $raw; - - return 0; - }, - )); - $mailer->setFrom('s@example.com')->setSubject('Subject')->setMessage('Body') - ->setBCC(['b1@example.com', 'b2@example.com', 'b3@example.com'], 2) - ->setHeader('To', 'undisclosed-recipients:;') - ->send(); - - self::assertCount(2, $messages); - foreach ($messages as $message) { - self::assertSame(['To: undisclosed-recipients:;'], $this->lines($message, 'To')); - } - } } diff --git a/tests/HeaderInjectionTest.php b/tests/HeaderInjectionTest.php index 1211ee8..7e57745 100644 --- a/tests/HeaderInjectionTest.php +++ b/tests/HeaderInjectionTest.php @@ -346,7 +346,7 @@ public function testReservedHeadersWinWhateverTheCaseOfTheName(string $name): vo public static function invalidHeaderNames(): array { return [ - 'trailing space' => ['From '], + 'only spaces' => [' '], 'inner space' => ['X Y'], 'colon' => ['X: y'], 'trailing colon' => ['X-A:'], diff --git a/tests/RecipientHeaderMatrixTest.php b/tests/RecipientHeaderMatrixTest.php index a6dbb6f..b1062a0 100644 --- a/tests/RecipientHeaderMatrixTest.php +++ b/tests/RecipientHeaderMatrixTest.php @@ -5,7 +5,7 @@ namespace InitPHP\Mailer\Tests; use InitPHP\Mailer\Mailer; -use InitPHP\Mailer\Tests\Support\FakeSocketClient; +use InitPHP\Mailer\Tests\Support\RespondingSocketClient; use InitPHP\Mailer\Transport\MailTransport; use InitPHP\Mailer\Transport\SendmailTransport; use InitPHP\Mailer\Transport\SmtpTransport; @@ -16,91 +16,256 @@ use function array_filter; use function array_map; use function array_values; +use function count; use function explode; use function preg_match; use function sort; /** * A custom To, Cc or Bcc header set with setHeader(), across the three - * transports: the header the library writes itself wins whatever the case of the - * custom name, and one it does not write is passed through. + * transports, with and without BCC batching, and for six spellings of the name + * (exact, lower, upper, mixed case, and a space before or after it). * - * Each row holds the To/Cc/Bcc header lines of the message as they are now and as - * 3.0.0 produced them for the same calls (recorded by running that release's - * source). Where the two differ the difference is intended: a custom header the - * library also writes is no longer emitted as a second line, and a custom To is - * kept when the message has no To recipient (3.0.0 lost it on sendmail and SMTP). + * Each row holds the To/Cc/Bcc header lines of every message as they are now and + * as 3.0.0 produced them for the same calls; the 3.0.0 column was recorded by + * running the 3.0.0 tag's source. `To` is not special-cased: its output equals + * 3.0.0's in every row. The only difference is on `Cc` and `Bcc`: when the + * library writes that header itself, a custom one of the same name (any case or + * spacing) is no longer emitted as a second line. */ final class RecipientHeaderMatrixTest extends TestCase { /** - * @return array, list}> + * @return array>, list>}> */ public static function matrix(): array { return [ - 'mail, custom To, library writes it' => ['mail', 'To', true, 'To', [], ['To: custom@co.example']], - 'mail, custom to, library writes it' => ['mail', 'To', true, 'to', [], ['to: custom@co.example']], - 'mail, custom To, library does not' => ['mail', 'To', false, 'To', ['Bcc: b@x.com', 'To: custom@co.example'], ['Bcc: b@x.com', 'To: custom@co.example']], - 'mail, custom to, library does not' => ['mail', 'To', false, 'to', ['Bcc: b@x.com', 'to: custom@co.example'], ['Bcc: b@x.com', 'to: custom@co.example']], - 'mail, custom Cc, library writes it' => ['mail', 'Cc', true, 'Cc', ['Cc: c@x.com'], ['Cc: c@x.com']], - 'mail, custom cc, library writes it' => ['mail', 'Cc', true, 'cc', ['Cc: c@x.com'], ['Cc: c@x.com', 'cc: custom@co.example']], - 'mail, custom Cc, library does not' => ['mail', 'Cc', false, 'Cc', ['Cc: custom@co.example'], ['Cc: custom@co.example']], - 'mail, custom cc, library does not' => ['mail', 'Cc', false, 'cc', ['cc: custom@co.example'], ['cc: custom@co.example']], - 'mail, custom Bcc, library writes it' => ['mail', 'Bcc', true, 'Bcc', ['Bcc: b@x.com'], ['Bcc: b@x.com']], - 'mail, custom bcc, library writes it' => ['mail', 'Bcc', true, 'bcc', ['Bcc: b@x.com'], ['Bcc: b@x.com', 'bcc: custom@co.example']], - 'mail, custom Bcc, library does not' => ['mail', 'Bcc', false, 'Bcc', ['Bcc: custom@co.example'], ['Bcc: custom@co.example']], - 'mail, custom bcc, library does not' => ['mail', 'Bcc', false, 'bcc', ['bcc: custom@co.example'], ['bcc: custom@co.example']], - 'sendmail, custom To, library writes it' => ['sendmail', 'To', true, 'To', ['To: t@x.com'], ['To: t@x.com']], - 'sendmail, custom to, library writes it' => ['sendmail', 'To', true, 'to', ['To: t@x.com'], ['To: t@x.com', 'to: custom@co.example']], - 'sendmail, custom To, library does not' => ['sendmail', 'To', false, 'To', ['Bcc: b@x.com', 'To: custom@co.example'], ['Bcc: b@x.com']], - 'sendmail, custom to, library does not' => ['sendmail', 'To', false, 'to', ['Bcc: b@x.com', 'to: custom@co.example'], ['Bcc: b@x.com', 'to: custom@co.example']], - 'sendmail, custom Cc, library writes it' => ['sendmail', 'Cc', true, 'Cc', ['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com', 'To: t@x.com']], - 'sendmail, custom cc, library writes it' => ['sendmail', 'Cc', true, 'cc', ['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com', 'To: t@x.com', 'cc: custom@co.example']], - 'sendmail, custom Cc, library does not' => ['sendmail', 'Cc', false, 'Cc', ['Cc: custom@co.example', 'To: t@x.com'], ['Cc: custom@co.example', 'To: t@x.com']], - 'sendmail, custom cc, library does not' => ['sendmail', 'Cc', false, 'cc', ['To: t@x.com', 'cc: custom@co.example'], ['To: t@x.com', 'cc: custom@co.example']], - 'sendmail, custom Bcc, library writes it' => ['sendmail', 'Bcc', true, 'Bcc', ['Bcc: b@x.com', 'To: t@x.com'], ['Bcc: b@x.com', 'To: t@x.com']], - 'sendmail, custom bcc, library writes it' => ['sendmail', 'Bcc', true, 'bcc', ['Bcc: b@x.com', 'To: t@x.com'], ['Bcc: b@x.com', 'To: t@x.com', 'bcc: custom@co.example']], - 'sendmail, custom Bcc, library does not' => ['sendmail', 'Bcc', false, 'Bcc', ['Bcc: custom@co.example', 'To: t@x.com'], ['Bcc: custom@co.example', 'To: t@x.com']], - 'sendmail, custom bcc, library does not' => ['sendmail', 'Bcc', false, 'bcc', ['To: t@x.com', 'bcc: custom@co.example'], ['To: t@x.com', 'bcc: custom@co.example']], - 'smtp, custom To, library writes it' => ['smtp', 'To', true, 'To', ['To: t@x.com'], ['To: t@x.com']], - 'smtp, custom to, library writes it' => ['smtp', 'To', true, 'to', ['To: t@x.com'], ['To: t@x.com', 'to: custom@co.example']], - 'smtp, custom To, library does not' => ['smtp', 'To', false, 'To', ['To: custom@co.example'], []], - 'smtp, custom to, library does not' => ['smtp', 'To', false, 'to', ['to: custom@co.example'], ['to: custom@co.example']], - 'smtp, custom Cc, library writes it' => ['smtp', 'Cc', true, 'Cc', ['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com', 'To: t@x.com']], - 'smtp, custom cc, library writes it' => ['smtp', 'Cc', true, 'cc', ['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com', 'To: t@x.com', 'cc: custom@co.example']], - 'smtp, custom Cc, library does not' => ['smtp', 'Cc', false, 'Cc', ['Cc: custom@co.example', 'To: t@x.com'], ['Cc: custom@co.example', 'To: t@x.com']], - 'smtp, custom cc, library does not' => ['smtp', 'Cc', false, 'cc', ['To: t@x.com', 'cc: custom@co.example'], ['To: t@x.com', 'cc: custom@co.example']], - 'smtp, custom Bcc, library writes it' => ['smtp', 'Bcc', true, 'Bcc', ['Bcc: custom@co.example', 'To: t@x.com'], ['Bcc: custom@co.example', 'To: t@x.com']], - 'smtp, custom bcc, library writes it' => ['smtp', 'Bcc', true, 'bcc', ['To: t@x.com', 'bcc: custom@co.example'], ['To: t@x.com', 'bcc: custom@co.example']], - 'smtp, custom Bcc, library does not' => ['smtp', 'Bcc', false, 'Bcc', ['Bcc: custom@co.example', 'To: t@x.com'], ['Bcc: custom@co.example', 'To: t@x.com']], - 'smtp, custom bcc, library does not' => ['smtp', 'Bcc', false, 'bcc', ['To: t@x.com', 'bcc: custom@co.example'], ['To: t@x.com', 'bcc: custom@co.example']], + 'mail, To, library writes it, one message, custom \'To\'' => ['mail', 'To', true, false, 'To', [['To: custom@co.example']], [['To: custom@co.example']]], + 'mail, To, library writes it, one message, custom \'to\'' => ['mail', 'To', true, false, 'to', [['to: custom@co.example']], [['to: custom@co.example']]], + 'mail, To, library writes it, one message, custom \'TO\'' => ['mail', 'To', true, false, 'TO', [['TO: custom@co.example']], [['TO: custom@co.example']]], + 'mail, To, library writes it, one message, custom \'tO\'' => ['mail', 'To', true, false, 'tO', [['tO: custom@co.example']], [['tO: custom@co.example']]], + 'mail, To, library writes it, one message, custom \'To \'' => ['mail', 'To', true, false, 'To ', [['To : custom@co.example']], [['To : custom@co.example']]], + 'mail, To, library writes it, one message, custom \' To\'' => ['mail', 'To', true, false, ' To', [[' To: custom@co.example']], [[' To: custom@co.example']]], + 'mail, To, library writes it, BCC batches, custom \'To\'' => ['mail', 'To', true, true, 'To', [['Bcc: b1@x.com, b2@x.com', 'To: custom@co.example'], ['Bcc: b3@x.com', 'To: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'To: custom@co.example'], ['Bcc: b3@x.com', 'To: custom@co.example']]], + 'mail, To, library writes it, BCC batches, custom \'to\'' => ['mail', 'To', true, true, 'to', [['Bcc: b1@x.com, b2@x.com', 'to: custom@co.example'], ['Bcc: b3@x.com', 'to: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'to: custom@co.example'], ['Bcc: b3@x.com', 'to: custom@co.example']]], + 'mail, To, library writes it, BCC batches, custom \'TO\'' => ['mail', 'To', true, true, 'TO', [['Bcc: b1@x.com, b2@x.com', 'TO: custom@co.example'], ['Bcc: b3@x.com', 'TO: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'TO: custom@co.example'], ['Bcc: b3@x.com', 'TO: custom@co.example']]], + 'mail, To, library writes it, BCC batches, custom \'tO\'' => ['mail', 'To', true, true, 'tO', [['Bcc: b1@x.com, b2@x.com', 'tO: custom@co.example'], ['Bcc: b3@x.com', 'tO: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'tO: custom@co.example'], ['Bcc: b3@x.com', 'tO: custom@co.example']]], + 'mail, To, library writes it, BCC batches, custom \'To \'' => ['mail', 'To', true, true, 'To ', [['Bcc: b1@x.com, b2@x.com', 'To : custom@co.example'], ['Bcc: b3@x.com', 'To : custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'To : custom@co.example'], ['Bcc: b3@x.com', 'To : custom@co.example']]], + 'mail, To, library writes it, BCC batches, custom \' To\'' => ['mail', 'To', true, true, ' To', [[' To: custom@co.example', 'Bcc: b1@x.com, b2@x.com'], [' To: custom@co.example', 'Bcc: b3@x.com']], [[' To: custom@co.example', 'Bcc: b1@x.com, b2@x.com'], [' To: custom@co.example', 'Bcc: b3@x.com']]], + 'mail, To, library does not, one message, custom \'To\'' => ['mail', 'To', false, false, 'To', [['Bcc: b@x.com', 'To: custom@co.example']], [['Bcc: b@x.com', 'To: custom@co.example']]], + 'mail, To, library does not, one message, custom \'to\'' => ['mail', 'To', false, false, 'to', [['Bcc: b@x.com', 'to: custom@co.example']], [['Bcc: b@x.com', 'to: custom@co.example']]], + 'mail, To, library does not, one message, custom \'TO\'' => ['mail', 'To', false, false, 'TO', [['Bcc: b@x.com', 'TO: custom@co.example']], [['Bcc: b@x.com', 'TO: custom@co.example']]], + 'mail, To, library does not, one message, custom \'tO\'' => ['mail', 'To', false, false, 'tO', [['Bcc: b@x.com', 'tO: custom@co.example']], [['Bcc: b@x.com', 'tO: custom@co.example']]], + 'mail, To, library does not, one message, custom \'To \'' => ['mail', 'To', false, false, 'To ', [['Bcc: b@x.com', 'To : custom@co.example']], [['Bcc: b@x.com', 'To : custom@co.example']]], + 'mail, To, library does not, one message, custom \' To\'' => ['mail', 'To', false, false, ' To', [[' To: custom@co.example', 'Bcc: b@x.com']], [[' To: custom@co.example', 'Bcc: b@x.com']]], + 'mail, To, library does not, BCC batches, custom \'To\'' => ['mail', 'To', false, true, 'To', [['Bcc: b1@x.com, b2@x.com', 'To: custom@co.example'], ['Bcc: b3@x.com', 'To: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'To: custom@co.example'], ['Bcc: b3@x.com', 'To: custom@co.example']]], + 'mail, To, library does not, BCC batches, custom \'to\'' => ['mail', 'To', false, true, 'to', [['Bcc: b1@x.com, b2@x.com', 'to: custom@co.example'], ['Bcc: b3@x.com', 'to: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'to: custom@co.example'], ['Bcc: b3@x.com', 'to: custom@co.example']]], + 'mail, To, library does not, BCC batches, custom \'TO\'' => ['mail', 'To', false, true, 'TO', [['Bcc: b1@x.com, b2@x.com', 'TO: custom@co.example'], ['Bcc: b3@x.com', 'TO: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'TO: custom@co.example'], ['Bcc: b3@x.com', 'TO: custom@co.example']]], + 'mail, To, library does not, BCC batches, custom \'tO\'' => ['mail', 'To', false, true, 'tO', [['Bcc: b1@x.com, b2@x.com', 'tO: custom@co.example'], ['Bcc: b3@x.com', 'tO: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'tO: custom@co.example'], ['Bcc: b3@x.com', 'tO: custom@co.example']]], + 'mail, To, library does not, BCC batches, custom \'To \'' => ['mail', 'To', false, true, 'To ', [['Bcc: b1@x.com, b2@x.com', 'To : custom@co.example'], ['Bcc: b3@x.com', 'To : custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'To : custom@co.example'], ['Bcc: b3@x.com', 'To : custom@co.example']]], + 'mail, To, library does not, BCC batches, custom \' To\'' => ['mail', 'To', false, true, ' To', [[' To: custom@co.example', 'Bcc: b1@x.com, b2@x.com'], [' To: custom@co.example', 'Bcc: b3@x.com']], [[' To: custom@co.example', 'Bcc: b1@x.com, b2@x.com'], [' To: custom@co.example', 'Bcc: b3@x.com']]], + 'mail, Cc, library writes it, one message, custom \'Cc\'' => ['mail', 'Cc', true, false, 'Cc', [['Cc: c@x.com']], [['Cc: c@x.com']]], + 'mail, Cc, library writes it, one message, custom \'cc\'' => ['mail', 'Cc', true, false, 'cc', [['Cc: c@x.com']], [['Cc: c@x.com', 'cc: custom@co.example']]], + 'mail, Cc, library writes it, one message, custom \'CC\'' => ['mail', 'Cc', true, false, 'CC', [['Cc: c@x.com']], [['CC: custom@co.example', 'Cc: c@x.com']]], + 'mail, Cc, library writes it, one message, custom \'cC\'' => ['mail', 'Cc', true, false, 'cC', [['Cc: c@x.com']], [['Cc: c@x.com', 'cC: custom@co.example']]], + 'mail, Cc, library writes it, one message, custom \'Cc \'' => ['mail', 'Cc', true, false, 'Cc ', [['Cc: c@x.com']], [['Cc : custom@co.example', 'Cc: c@x.com']]], + 'mail, Cc, library writes it, one message, custom \' Cc\'' => ['mail', 'Cc', true, false, ' Cc', [['Cc: c@x.com']], [[' Cc: custom@co.example', 'Cc: c@x.com']]], + 'mail, Cc, library writes it, BCC batches, custom \'Cc\'' => ['mail', 'Cc', true, true, 'Cc', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']]], + 'mail, Cc, library writes it, BCC batches, custom \'cc\'' => ['mail', 'Cc', true, true, 'cc', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'cc: custom@co.example'], ['Bcc: b3@x.com', 'Cc: c@x.com', 'cc: custom@co.example']]], + 'mail, Cc, library writes it, BCC batches, custom \'CC\'' => ['mail', 'Cc', true, true, 'CC', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [['Bcc: b1@x.com, b2@x.com', 'CC: custom@co.example', 'Cc: c@x.com'], ['Bcc: b3@x.com', 'CC: custom@co.example', 'Cc: c@x.com']]], + 'mail, Cc, library writes it, BCC batches, custom \'cC\'' => ['mail', 'Cc', true, true, 'cC', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'cC: custom@co.example'], ['Bcc: b3@x.com', 'Cc: c@x.com', 'cC: custom@co.example']]], + 'mail, Cc, library writes it, BCC batches, custom \'Cc \'' => ['mail', 'Cc', true, true, 'Cc ', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [['Bcc: b1@x.com, b2@x.com', 'Cc : custom@co.example', 'Cc: c@x.com'], ['Bcc: b3@x.com', 'Cc : custom@co.example', 'Cc: c@x.com']]], + 'mail, Cc, library writes it, BCC batches, custom \' Cc\'' => ['mail', 'Cc', true, true, ' Cc', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [[' Cc: custom@co.example', 'Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com'], [' Cc: custom@co.example', 'Bcc: b3@x.com', 'Cc: c@x.com']]], + 'mail, Cc, library does not, one message, custom \'Cc\'' => ['mail', 'Cc', false, false, 'Cc', [['Cc: custom@co.example']], [['Cc: custom@co.example']]], + 'mail, Cc, library does not, one message, custom \'cc\'' => ['mail', 'Cc', false, false, 'cc', [['cc: custom@co.example']], [['cc: custom@co.example']]], + 'mail, Cc, library does not, one message, custom \'CC\'' => ['mail', 'Cc', false, false, 'CC', [['CC: custom@co.example']], [['CC: custom@co.example']]], + 'mail, Cc, library does not, one message, custom \'cC\'' => ['mail', 'Cc', false, false, 'cC', [['cC: custom@co.example']], [['cC: custom@co.example']]], + 'mail, Cc, library does not, one message, custom \'Cc \'' => ['mail', 'Cc', false, false, 'Cc ', [['Cc : custom@co.example']], [['Cc : custom@co.example']]], + 'mail, Cc, library does not, one message, custom \' Cc\'' => ['mail', 'Cc', false, false, ' Cc', [[' Cc: custom@co.example']], [[' Cc: custom@co.example']]], + 'mail, Cc, library does not, BCC batches, custom \'Cc\'' => ['mail', 'Cc', false, true, 'Cc', [['Bcc: b1@x.com, b2@x.com', 'Cc: custom@co.example'], ['Bcc: b3@x.com', 'Cc: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'Cc: custom@co.example'], ['Bcc: b3@x.com', 'Cc: custom@co.example']]], + 'mail, Cc, library does not, BCC batches, custom \'cc\'' => ['mail', 'Cc', false, true, 'cc', [['Bcc: b1@x.com, b2@x.com', 'cc: custom@co.example'], ['Bcc: b3@x.com', 'cc: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'cc: custom@co.example'], ['Bcc: b3@x.com', 'cc: custom@co.example']]], + 'mail, Cc, library does not, BCC batches, custom \'CC\'' => ['mail', 'Cc', false, true, 'CC', [['Bcc: b1@x.com, b2@x.com', 'CC: custom@co.example'], ['Bcc: b3@x.com', 'CC: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'CC: custom@co.example'], ['Bcc: b3@x.com', 'CC: custom@co.example']]], + 'mail, Cc, library does not, BCC batches, custom \'cC\'' => ['mail', 'Cc', false, true, 'cC', [['Bcc: b1@x.com, b2@x.com', 'cC: custom@co.example'], ['Bcc: b3@x.com', 'cC: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'cC: custom@co.example'], ['Bcc: b3@x.com', 'cC: custom@co.example']]], + 'mail, Cc, library does not, BCC batches, custom \'Cc \'' => ['mail', 'Cc', false, true, 'Cc ', [['Bcc: b1@x.com, b2@x.com', 'Cc : custom@co.example'], ['Bcc: b3@x.com', 'Cc : custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'Cc : custom@co.example'], ['Bcc: b3@x.com', 'Cc : custom@co.example']]], + 'mail, Cc, library does not, BCC batches, custom \' Cc\'' => ['mail', 'Cc', false, true, ' Cc', [[' Cc: custom@co.example', 'Bcc: b1@x.com, b2@x.com'], [' Cc: custom@co.example', 'Bcc: b3@x.com']], [[' Cc: custom@co.example', 'Bcc: b1@x.com, b2@x.com'], [' Cc: custom@co.example', 'Bcc: b3@x.com']]], + 'mail, Bcc, library writes it, one message, custom \'Bcc\'' => ['mail', 'Bcc', true, false, 'Bcc', [['Bcc: b@x.com']], [['Bcc: b@x.com']]], + 'mail, Bcc, library writes it, one message, custom \'bcc\'' => ['mail', 'Bcc', true, false, 'bcc', [['Bcc: b@x.com']], [['Bcc: b@x.com', 'bcc: custom@co.example']]], + 'mail, Bcc, library writes it, one message, custom \'BCC\'' => ['mail', 'Bcc', true, false, 'BCC', [['Bcc: b@x.com']], [['BCC: custom@co.example', 'Bcc: b@x.com']]], + 'mail, Bcc, library writes it, one message, custom \'bCC\'' => ['mail', 'Bcc', true, false, 'bCC', [['Bcc: b@x.com']], [['Bcc: b@x.com', 'bCC: custom@co.example']]], + 'mail, Bcc, library writes it, one message, custom \'Bcc \'' => ['mail', 'Bcc', true, false, 'Bcc ', [['Bcc: b@x.com']], [['Bcc : custom@co.example', 'Bcc: b@x.com']]], + 'mail, Bcc, library writes it, one message, custom \' Bcc\'' => ['mail', 'Bcc', true, false, ' Bcc', [['Bcc: b@x.com']], [[' Bcc: custom@co.example', 'Bcc: b@x.com']]], + 'mail, Bcc, library writes it, BCC batches, custom \'Bcc\'' => ['mail', 'Bcc', true, true, 'Bcc', [['Bcc: b1@x.com, b2@x.com'], ['Bcc: b3@x.com']], [['Bcc: b1@x.com, b2@x.com'], ['Bcc: b3@x.com']]], + 'mail, Bcc, library writes it, BCC batches, custom \'bcc\'' => ['mail', 'Bcc', true, true, 'bcc', [['Bcc: b1@x.com, b2@x.com'], ['Bcc: b3@x.com']], [['Bcc: b1@x.com, b2@x.com', 'bcc: custom@co.example'], ['Bcc: b3@x.com', 'bcc: custom@co.example']]], + 'mail, Bcc, library writes it, BCC batches, custom \'BCC\'' => ['mail', 'Bcc', true, true, 'BCC', [['Bcc: b1@x.com, b2@x.com'], ['Bcc: b3@x.com']], [['BCC: custom@co.example', 'Bcc: b1@x.com, b2@x.com'], ['BCC: custom@co.example', 'Bcc: b3@x.com']]], + 'mail, Bcc, library writes it, BCC batches, custom \'bCC\'' => ['mail', 'Bcc', true, true, 'bCC', [['Bcc: b1@x.com, b2@x.com'], ['Bcc: b3@x.com']], [['Bcc: b1@x.com, b2@x.com', 'bCC: custom@co.example'], ['Bcc: b3@x.com', 'bCC: custom@co.example']]], + 'mail, Bcc, library writes it, BCC batches, custom \'Bcc \'' => ['mail', 'Bcc', true, true, 'Bcc ', [['Bcc: b1@x.com, b2@x.com'], ['Bcc: b3@x.com']], [['Bcc : custom@co.example', 'Bcc: b1@x.com, b2@x.com'], ['Bcc : custom@co.example', 'Bcc: b3@x.com']]], + 'mail, Bcc, library writes it, BCC batches, custom \' Bcc\'' => ['mail', 'Bcc', true, true, ' Bcc', [['Bcc: b1@x.com, b2@x.com'], ['Bcc: b3@x.com']], [[' Bcc: custom@co.example', 'Bcc: b1@x.com, b2@x.com'], [' Bcc: custom@co.example', 'Bcc: b3@x.com']]], + 'mail, Bcc, library does not, one message, custom \'Bcc\'' => ['mail', 'Bcc', false, false, 'Bcc', [['Bcc: custom@co.example']], [['Bcc: custom@co.example']]], + 'mail, Bcc, library does not, one message, custom \'bcc\'' => ['mail', 'Bcc', false, false, 'bcc', [['bcc: custom@co.example']], [['bcc: custom@co.example']]], + 'mail, Bcc, library does not, one message, custom \'BCC\'' => ['mail', 'Bcc', false, false, 'BCC', [['BCC: custom@co.example']], [['BCC: custom@co.example']]], + 'mail, Bcc, library does not, one message, custom \'bCC\'' => ['mail', 'Bcc', false, false, 'bCC', [['bCC: custom@co.example']], [['bCC: custom@co.example']]], + 'mail, Bcc, library does not, one message, custom \'Bcc \'' => ['mail', 'Bcc', false, false, 'Bcc ', [['Bcc : custom@co.example']], [['Bcc : custom@co.example']]], + 'mail, Bcc, library does not, one message, custom \' Bcc\'' => ['mail', 'Bcc', false, false, ' Bcc', [[' Bcc: custom@co.example']], [[' Bcc: custom@co.example']]], + 'sendmail, To, library writes it, one message, custom \'To\'' => ['sendmail', 'To', true, false, 'To', [['To: t@x.com']], [['To: t@x.com']]], + 'sendmail, To, library writes it, one message, custom \'to\'' => ['sendmail', 'To', true, false, 'to', [['To: t@x.com', 'to: custom@co.example']], [['To: t@x.com', 'to: custom@co.example']]], + 'sendmail, To, library writes it, one message, custom \'TO\'' => ['sendmail', 'To', true, false, 'TO', [['TO: custom@co.example', 'To: t@x.com']], [['TO: custom@co.example', 'To: t@x.com']]], + 'sendmail, To, library writes it, one message, custom \'tO\'' => ['sendmail', 'To', true, false, 'tO', [['To: t@x.com', 'tO: custom@co.example']], [['To: t@x.com', 'tO: custom@co.example']]], + 'sendmail, To, library writes it, one message, custom \'To \'' => ['sendmail', 'To', true, false, 'To ', [['To : custom@co.example', 'To: t@x.com']], [['To : custom@co.example', 'To: t@x.com']]], + 'sendmail, To, library writes it, one message, custom \' To\'' => ['sendmail', 'To', true, false, ' To', [[' To: custom@co.example', 'To: t@x.com']], [[' To: custom@co.example', 'To: t@x.com']]], + 'sendmail, To, library writes it, BCC batches, custom \'To\'' => ['sendmail', 'To', true, true, 'To', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['Bcc: b3@x.com']], [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['Bcc: b3@x.com']]], + 'sendmail, To, library writes it, BCC batches, custom \'to\'' => ['sendmail', 'To', true, true, 'to', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com', 'to: custom@co.example'], ['Bcc: b3@x.com', 'to: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com', 'to: custom@co.example'], ['Bcc: b3@x.com', 'to: custom@co.example']]], + 'sendmail, To, library writes it, BCC batches, custom \'TO\'' => ['sendmail', 'To', true, true, 'TO', [['Bcc: b1@x.com, b2@x.com', 'TO: custom@co.example', 'To: t@x.com'], ['Bcc: b3@x.com', 'TO: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'TO: custom@co.example', 'To: t@x.com'], ['Bcc: b3@x.com', 'TO: custom@co.example']]], + 'sendmail, To, library writes it, BCC batches, custom \'tO\'' => ['sendmail', 'To', true, true, 'tO', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com', 'tO: custom@co.example'], ['Bcc: b3@x.com', 'tO: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com', 'tO: custom@co.example'], ['Bcc: b3@x.com', 'tO: custom@co.example']]], + 'sendmail, To, library writes it, BCC batches, custom \'To \'' => ['sendmail', 'To', true, true, 'To ', [['Bcc: b1@x.com, b2@x.com', 'To : custom@co.example', 'To: t@x.com'], ['Bcc: b3@x.com', 'To : custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'To : custom@co.example', 'To: t@x.com'], ['Bcc: b3@x.com', 'To : custom@co.example']]], + 'sendmail, To, library writes it, BCC batches, custom \' To\'' => ['sendmail', 'To', true, true, ' To', [[' To: custom@co.example', 'Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], [' To: custom@co.example', 'Bcc: b3@x.com']], [[' To: custom@co.example', 'Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], [' To: custom@co.example', 'Bcc: b3@x.com']]], + 'sendmail, To, library does not, one message, custom \'To\'' => ['sendmail', 'To', false, false, 'To', [['Bcc: b@x.com']], [['Bcc: b@x.com']]], + 'sendmail, To, library does not, one message, custom \'to\'' => ['sendmail', 'To', false, false, 'to', [['Bcc: b@x.com', 'to: custom@co.example']], [['Bcc: b@x.com', 'to: custom@co.example']]], + 'sendmail, To, library does not, one message, custom \'TO\'' => ['sendmail', 'To', false, false, 'TO', [['Bcc: b@x.com', 'TO: custom@co.example']], [['Bcc: b@x.com', 'TO: custom@co.example']]], + 'sendmail, To, library does not, one message, custom \'tO\'' => ['sendmail', 'To', false, false, 'tO', [['Bcc: b@x.com', 'tO: custom@co.example']], [['Bcc: b@x.com', 'tO: custom@co.example']]], + 'sendmail, To, library does not, one message, custom \'To \'' => ['sendmail', 'To', false, false, 'To ', [['Bcc: b@x.com', 'To : custom@co.example']], [['Bcc: b@x.com', 'To : custom@co.example']]], + 'sendmail, To, library does not, one message, custom \' To\'' => ['sendmail', 'To', false, false, ' To', [[' To: custom@co.example', 'Bcc: b@x.com']], [[' To: custom@co.example', 'Bcc: b@x.com']]], + 'sendmail, To, library does not, BCC batches, custom \'To\'' => ['sendmail', 'To', false, true, 'To', [['Bcc: b1@x.com, b2@x.com'], ['Bcc: b3@x.com']], [['Bcc: b1@x.com, b2@x.com'], ['Bcc: b3@x.com']]], + 'sendmail, To, library does not, BCC batches, custom \'to\'' => ['sendmail', 'To', false, true, 'to', [['Bcc: b1@x.com, b2@x.com', 'to: custom@co.example'], ['Bcc: b3@x.com', 'to: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'to: custom@co.example'], ['Bcc: b3@x.com', 'to: custom@co.example']]], + 'sendmail, To, library does not, BCC batches, custom \'TO\'' => ['sendmail', 'To', false, true, 'TO', [['Bcc: b1@x.com, b2@x.com', 'TO: custom@co.example'], ['Bcc: b3@x.com', 'TO: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'TO: custom@co.example'], ['Bcc: b3@x.com', 'TO: custom@co.example']]], + 'sendmail, To, library does not, BCC batches, custom \'tO\'' => ['sendmail', 'To', false, true, 'tO', [['Bcc: b1@x.com, b2@x.com', 'tO: custom@co.example'], ['Bcc: b3@x.com', 'tO: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'tO: custom@co.example'], ['Bcc: b3@x.com', 'tO: custom@co.example']]], + 'sendmail, To, library does not, BCC batches, custom \'To \'' => ['sendmail', 'To', false, true, 'To ', [['Bcc: b1@x.com, b2@x.com', 'To : custom@co.example'], ['Bcc: b3@x.com', 'To : custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'To : custom@co.example'], ['Bcc: b3@x.com', 'To : custom@co.example']]], + 'sendmail, To, library does not, BCC batches, custom \' To\'' => ['sendmail', 'To', false, true, ' To', [[' To: custom@co.example', 'Bcc: b1@x.com, b2@x.com'], [' To: custom@co.example', 'Bcc: b3@x.com']], [[' To: custom@co.example', 'Bcc: b1@x.com, b2@x.com'], [' To: custom@co.example', 'Bcc: b3@x.com']]], + 'sendmail, Cc, library writes it, one message, custom \'Cc\'' => ['sendmail', 'Cc', true, false, 'Cc', [['Cc: c@x.com', 'To: t@x.com']], [['Cc: c@x.com', 'To: t@x.com']]], + 'sendmail, Cc, library writes it, one message, custom \'cc\'' => ['sendmail', 'Cc', true, false, 'cc', [['Cc: c@x.com', 'To: t@x.com']], [['Cc: c@x.com', 'To: t@x.com', 'cc: custom@co.example']]], + 'sendmail, Cc, library writes it, one message, custom \'CC\'' => ['sendmail', 'Cc', true, false, 'CC', [['Cc: c@x.com', 'To: t@x.com']], [['CC: custom@co.example', 'Cc: c@x.com', 'To: t@x.com']]], + 'sendmail, Cc, library writes it, one message, custom \'cC\'' => ['sendmail', 'Cc', true, false, 'cC', [['Cc: c@x.com', 'To: t@x.com']], [['Cc: c@x.com', 'To: t@x.com', 'cC: custom@co.example']]], + 'sendmail, Cc, library writes it, one message, custom \'Cc \'' => ['sendmail', 'Cc', true, false, 'Cc ', [['Cc: c@x.com', 'To: t@x.com']], [['Cc : custom@co.example', 'Cc: c@x.com', 'To: t@x.com']]], + 'sendmail, Cc, library writes it, one message, custom \' Cc\'' => ['sendmail', 'Cc', true, false, ' Cc', [['Cc: c@x.com', 'To: t@x.com']], [[' Cc: custom@co.example', 'Cc: c@x.com', 'To: t@x.com']]], + 'sendmail, Cc, library writes it, BCC batches, custom \'Cc\'' => ['sendmail', 'Cc', true, true, 'Cc', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']]], + 'sendmail, Cc, library writes it, BCC batches, custom \'cc\'' => ['sendmail', 'Cc', true, true, 'cc', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'To: t@x.com', 'cc: custom@co.example'], ['Bcc: b3@x.com', 'Cc: c@x.com', 'cc: custom@co.example']]], + 'sendmail, Cc, library writes it, BCC batches, custom \'CC\'' => ['sendmail', 'Cc', true, true, 'CC', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [['Bcc: b1@x.com, b2@x.com', 'CC: custom@co.example', 'Cc: c@x.com', 'To: t@x.com'], ['Bcc: b3@x.com', 'CC: custom@co.example', 'Cc: c@x.com']]], + 'sendmail, Cc, library writes it, BCC batches, custom \'cC\'' => ['sendmail', 'Cc', true, true, 'cC', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'To: t@x.com', 'cC: custom@co.example'], ['Bcc: b3@x.com', 'Cc: c@x.com', 'cC: custom@co.example']]], + 'sendmail, Cc, library writes it, BCC batches, custom \'Cc \'' => ['sendmail', 'Cc', true, true, 'Cc ', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [['Bcc: b1@x.com, b2@x.com', 'Cc : custom@co.example', 'Cc: c@x.com', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc : custom@co.example', 'Cc: c@x.com']]], + 'sendmail, Cc, library writes it, BCC batches, custom \' Cc\'' => ['sendmail', 'Cc', true, true, ' Cc', [['Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc: c@x.com']], [[' Cc: custom@co.example', 'Bcc: b1@x.com, b2@x.com', 'Cc: c@x.com', 'To: t@x.com'], [' Cc: custom@co.example', 'Bcc: b3@x.com', 'Cc: c@x.com']]], + 'sendmail, Cc, library does not, one message, custom \'Cc\'' => ['sendmail', 'Cc', false, false, 'Cc', [['Cc: custom@co.example', 'To: t@x.com']], [['Cc: custom@co.example', 'To: t@x.com']]], + 'sendmail, Cc, library does not, one message, custom \'cc\'' => ['sendmail', 'Cc', false, false, 'cc', [['To: t@x.com', 'cc: custom@co.example']], [['To: t@x.com', 'cc: custom@co.example']]], + 'sendmail, Cc, library does not, one message, custom \'CC\'' => ['sendmail', 'Cc', false, false, 'CC', [['CC: custom@co.example', 'To: t@x.com']], [['CC: custom@co.example', 'To: t@x.com']]], + 'sendmail, Cc, library does not, one message, custom \'cC\'' => ['sendmail', 'Cc', false, false, 'cC', [['To: t@x.com', 'cC: custom@co.example']], [['To: t@x.com', 'cC: custom@co.example']]], + 'sendmail, Cc, library does not, one message, custom \'Cc \'' => ['sendmail', 'Cc', false, false, 'Cc ', [['Cc : custom@co.example', 'To: t@x.com']], [['Cc : custom@co.example', 'To: t@x.com']]], + 'sendmail, Cc, library does not, one message, custom \' Cc\'' => ['sendmail', 'Cc', false, false, ' Cc', [[' Cc: custom@co.example', 'To: t@x.com']], [[' Cc: custom@co.example', 'To: t@x.com']]], + 'sendmail, Cc, library does not, BCC batches, custom \'Cc\'' => ['sendmail', 'Cc', false, true, 'Cc', [['Bcc: b1@x.com, b2@x.com', 'Cc: custom@co.example', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'Cc: custom@co.example', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc: custom@co.example']]], + 'sendmail, Cc, library does not, BCC batches, custom \'cc\'' => ['sendmail', 'Cc', false, true, 'cc', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com', 'cc: custom@co.example'], ['Bcc: b3@x.com', 'cc: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com', 'cc: custom@co.example'], ['Bcc: b3@x.com', 'cc: custom@co.example']]], + 'sendmail, Cc, library does not, BCC batches, custom \'CC\'' => ['sendmail', 'Cc', false, true, 'CC', [['Bcc: b1@x.com, b2@x.com', 'CC: custom@co.example', 'To: t@x.com'], ['Bcc: b3@x.com', 'CC: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'CC: custom@co.example', 'To: t@x.com'], ['Bcc: b3@x.com', 'CC: custom@co.example']]], + 'sendmail, Cc, library does not, BCC batches, custom \'cC\'' => ['sendmail', 'Cc', false, true, 'cC', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com', 'cC: custom@co.example'], ['Bcc: b3@x.com', 'cC: custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com', 'cC: custom@co.example'], ['Bcc: b3@x.com', 'cC: custom@co.example']]], + 'sendmail, Cc, library does not, BCC batches, custom \'Cc \'' => ['sendmail', 'Cc', false, true, 'Cc ', [['Bcc: b1@x.com, b2@x.com', 'Cc : custom@co.example', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc : custom@co.example']], [['Bcc: b1@x.com, b2@x.com', 'Cc : custom@co.example', 'To: t@x.com'], ['Bcc: b3@x.com', 'Cc : custom@co.example']]], + 'sendmail, Cc, library does not, BCC batches, custom \' Cc\'' => ['sendmail', 'Cc', false, true, ' Cc', [[' Cc: custom@co.example', 'Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], [' Cc: custom@co.example', 'Bcc: b3@x.com']], [[' Cc: custom@co.example', 'Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], [' Cc: custom@co.example', 'Bcc: b3@x.com']]], + 'sendmail, Bcc, library writes it, one message, custom \'Bcc\'' => ['sendmail', 'Bcc', true, false, 'Bcc', [['Bcc: b@x.com', 'To: t@x.com']], [['Bcc: b@x.com', 'To: t@x.com']]], + 'sendmail, Bcc, library writes it, one message, custom \'bcc\'' => ['sendmail', 'Bcc', true, false, 'bcc', [['Bcc: b@x.com', 'To: t@x.com']], [['Bcc: b@x.com', 'To: t@x.com', 'bcc: custom@co.example']]], + 'sendmail, Bcc, library writes it, one message, custom \'BCC\'' => ['sendmail', 'Bcc', true, false, 'BCC', [['Bcc: b@x.com', 'To: t@x.com']], [['BCC: custom@co.example', 'Bcc: b@x.com', 'To: t@x.com']]], + 'sendmail, Bcc, library writes it, one message, custom \'bCC\'' => ['sendmail', 'Bcc', true, false, 'bCC', [['Bcc: b@x.com', 'To: t@x.com']], [['Bcc: b@x.com', 'To: t@x.com', 'bCC: custom@co.example']]], + 'sendmail, Bcc, library writes it, one message, custom \'Bcc \'' => ['sendmail', 'Bcc', true, false, 'Bcc ', [['Bcc: b@x.com', 'To: t@x.com']], [['Bcc : custom@co.example', 'Bcc: b@x.com', 'To: t@x.com']]], + 'sendmail, Bcc, library writes it, one message, custom \' Bcc\'' => ['sendmail', 'Bcc', true, false, ' Bcc', [['Bcc: b@x.com', 'To: t@x.com']], [[' Bcc: custom@co.example', 'Bcc: b@x.com', 'To: t@x.com']]], + 'sendmail, Bcc, library writes it, BCC batches, custom \'Bcc\'' => ['sendmail', 'Bcc', true, true, 'Bcc', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['Bcc: b3@x.com']], [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['Bcc: b3@x.com']]], + 'sendmail, Bcc, library writes it, BCC batches, custom \'bcc\'' => ['sendmail', 'Bcc', true, true, 'bcc', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['Bcc: b3@x.com']], [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com', 'bcc: custom@co.example'], ['Bcc: b3@x.com', 'bcc: custom@co.example']]], + 'sendmail, Bcc, library writes it, BCC batches, custom \'BCC\'' => ['sendmail', 'Bcc', true, true, 'BCC', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['Bcc: b3@x.com']], [['BCC: custom@co.example', 'Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['BCC: custom@co.example', 'Bcc: b3@x.com']]], + 'sendmail, Bcc, library writes it, BCC batches, custom \'bCC\'' => ['sendmail', 'Bcc', true, true, 'bCC', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['Bcc: b3@x.com']], [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com', 'bCC: custom@co.example'], ['Bcc: b3@x.com', 'bCC: custom@co.example']]], + 'sendmail, Bcc, library writes it, BCC batches, custom \'Bcc \'' => ['sendmail', 'Bcc', true, true, 'Bcc ', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['Bcc: b3@x.com']], [['Bcc : custom@co.example', 'Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['Bcc : custom@co.example', 'Bcc: b3@x.com']]], + 'sendmail, Bcc, library writes it, BCC batches, custom \' Bcc\'' => ['sendmail', 'Bcc', true, true, ' Bcc', [['Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], ['Bcc: b3@x.com']], [[' Bcc: custom@co.example', 'Bcc: b1@x.com, b2@x.com', 'To: t@x.com'], [' Bcc: custom@co.example', 'Bcc: b3@x.com']]], + 'sendmail, Bcc, library does not, one message, custom \'Bcc\'' => ['sendmail', 'Bcc', false, false, 'Bcc', [['Bcc: custom@co.example', 'To: t@x.com']], [['Bcc: custom@co.example', 'To: t@x.com']]], + 'sendmail, Bcc, library does not, one message, custom \'bcc\'' => ['sendmail', 'Bcc', false, false, 'bcc', [['To: t@x.com', 'bcc: custom@co.example']], [['To: t@x.com', 'bcc: custom@co.example']]], + 'sendmail, Bcc, library does not, one message, custom \'BCC\'' => ['sendmail', 'Bcc', false, false, 'BCC', [['BCC: custom@co.example', 'To: t@x.com']], [['BCC: custom@co.example', 'To: t@x.com']]], + 'sendmail, Bcc, library does not, one message, custom \'bCC\'' => ['sendmail', 'Bcc', false, false, 'bCC', [['To: t@x.com', 'bCC: custom@co.example']], [['To: t@x.com', 'bCC: custom@co.example']]], + 'sendmail, Bcc, library does not, one message, custom \'Bcc \'' => ['sendmail', 'Bcc', false, false, 'Bcc ', [['Bcc : custom@co.example', 'To: t@x.com']], [['Bcc : custom@co.example', 'To: t@x.com']]], + 'sendmail, Bcc, library does not, one message, custom \' Bcc\'' => ['sendmail', 'Bcc', false, false, ' Bcc', [[' Bcc: custom@co.example', 'To: t@x.com']], [[' Bcc: custom@co.example', 'To: t@x.com']]], + 'smtp, To, library writes it, one message, custom \'To\'' => ['smtp', 'To', true, false, 'To', [['To: t@x.com']], [['To: t@x.com']]], + 'smtp, To, library writes it, one message, custom \'to\'' => ['smtp', 'To', true, false, 'to', [['To: t@x.com', 'to: custom@co.example']], [['To: t@x.com', 'to: custom@co.example']]], + 'smtp, To, library writes it, one message, custom \'TO\'' => ['smtp', 'To', true, false, 'TO', [['TO: custom@co.example', 'To: t@x.com']], [['TO: custom@co.example', 'To: t@x.com']]], + 'smtp, To, library writes it, one message, custom \'tO\'' => ['smtp', 'To', true, false, 'tO', [['To: t@x.com', 'tO: custom@co.example']], [['To: t@x.com', 'tO: custom@co.example']]], + 'smtp, To, library writes it, one message, custom \'To \'' => ['smtp', 'To', true, false, 'To ', [['To : custom@co.example', 'To: t@x.com']], [['To : custom@co.example', 'To: t@x.com']]], + 'smtp, To, library writes it, one message, custom \' To\'' => ['smtp', 'To', true, false, ' To', [[' To: custom@co.example', 'To: t@x.com']], [[' To: custom@co.example', 'To: t@x.com']]], + 'smtp, To, library writes it, BCC batches, custom \'To\'' => ['smtp', 'To', true, true, 'To', [['To: t@x.com'], []], [['To: t@x.com'], []]], + 'smtp, To, library writes it, BCC batches, custom \'to\'' => ['smtp', 'To', true, true, 'to', [['To: t@x.com', 'to: custom@co.example'], ['to: custom@co.example']], [['To: t@x.com', 'to: custom@co.example'], ['to: custom@co.example']]], + 'smtp, To, library writes it, BCC batches, custom \'TO\'' => ['smtp', 'To', true, true, 'TO', [['TO: custom@co.example', 'To: t@x.com'], ['TO: custom@co.example']], [['TO: custom@co.example', 'To: t@x.com'], ['TO: custom@co.example']]], + 'smtp, To, library writes it, BCC batches, custom \'tO\'' => ['smtp', 'To', true, true, 'tO', [['To: t@x.com', 'tO: custom@co.example'], ['tO: custom@co.example']], [['To: t@x.com', 'tO: custom@co.example'], ['tO: custom@co.example']]], + 'smtp, To, library writes it, BCC batches, custom \'To \'' => ['smtp', 'To', true, true, 'To ', [['To : custom@co.example', 'To: t@x.com'], ['To : custom@co.example']], [['To : custom@co.example', 'To: t@x.com'], ['To : custom@co.example']]], + 'smtp, To, library writes it, BCC batches, custom \' To\'' => ['smtp', 'To', true, true, ' To', [[' To: custom@co.example', 'To: t@x.com'], [' To: custom@co.example']], [[' To: custom@co.example', 'To: t@x.com'], [' To: custom@co.example']]], + 'smtp, To, library does not, one message, custom \'To\'' => ['smtp', 'To', false, false, 'To', [[]], [[]]], + 'smtp, To, library does not, one message, custom \'to\'' => ['smtp', 'To', false, false, 'to', [['to: custom@co.example']], [['to: custom@co.example']]], + 'smtp, To, library does not, one message, custom \'TO\'' => ['smtp', 'To', false, false, 'TO', [['TO: custom@co.example']], [['TO: custom@co.example']]], + 'smtp, To, library does not, one message, custom \'tO\'' => ['smtp', 'To', false, false, 'tO', [['tO: custom@co.example']], [['tO: custom@co.example']]], + 'smtp, To, library does not, one message, custom \'To \'' => ['smtp', 'To', false, false, 'To ', [['To : custom@co.example']], [['To : custom@co.example']]], + 'smtp, To, library does not, one message, custom \' To\'' => ['smtp', 'To', false, false, ' To', [[' To: custom@co.example']], [[' To: custom@co.example']]], + 'smtp, To, library does not, BCC batches, custom \'To\'' => ['smtp', 'To', false, true, 'To', [[], []], [[], []]], + 'smtp, To, library does not, BCC batches, custom \'to\'' => ['smtp', 'To', false, true, 'to', [['to: custom@co.example'], ['to: custom@co.example']], [['to: custom@co.example'], ['to: custom@co.example']]], + 'smtp, To, library does not, BCC batches, custom \'TO\'' => ['smtp', 'To', false, true, 'TO', [['TO: custom@co.example'], ['TO: custom@co.example']], [['TO: custom@co.example'], ['TO: custom@co.example']]], + 'smtp, To, library does not, BCC batches, custom \'tO\'' => ['smtp', 'To', false, true, 'tO', [['tO: custom@co.example'], ['tO: custom@co.example']], [['tO: custom@co.example'], ['tO: custom@co.example']]], + 'smtp, To, library does not, BCC batches, custom \'To \'' => ['smtp', 'To', false, true, 'To ', [['To : custom@co.example'], ['To : custom@co.example']], [['To : custom@co.example'], ['To : custom@co.example']]], + 'smtp, To, library does not, BCC batches, custom \' To\'' => ['smtp', 'To', false, true, ' To', [[' To: custom@co.example'], [' To: custom@co.example']], [[' To: custom@co.example'], [' To: custom@co.example']]], + 'smtp, Cc, library writes it, one message, custom \'Cc\'' => ['smtp', 'Cc', true, false, 'Cc', [['Cc: c@x.com', 'To: t@x.com']], [['Cc: c@x.com', 'To: t@x.com']]], + 'smtp, Cc, library writes it, one message, custom \'cc\'' => ['smtp', 'Cc', true, false, 'cc', [['Cc: c@x.com', 'To: t@x.com']], [['Cc: c@x.com', 'To: t@x.com', 'cc: custom@co.example']]], + 'smtp, Cc, library writes it, one message, custom \'CC\'' => ['smtp', 'Cc', true, false, 'CC', [['Cc: c@x.com', 'To: t@x.com']], [['CC: custom@co.example', 'Cc: c@x.com', 'To: t@x.com']]], + 'smtp, Cc, library writes it, one message, custom \'cC\'' => ['smtp', 'Cc', true, false, 'cC', [['Cc: c@x.com', 'To: t@x.com']], [['Cc: c@x.com', 'To: t@x.com', 'cC: custom@co.example']]], + 'smtp, Cc, library writes it, one message, custom \'Cc \'' => ['smtp', 'Cc', true, false, 'Cc ', [['Cc: c@x.com', 'To: t@x.com']], [['Cc : custom@co.example', 'Cc: c@x.com', 'To: t@x.com']]], + 'smtp, Cc, library writes it, one message, custom \' Cc\'' => ['smtp', 'Cc', true, false, ' Cc', [['Cc: c@x.com', 'To: t@x.com']], [[' Cc: custom@co.example', 'Cc: c@x.com', 'To: t@x.com']]], + 'smtp, Cc, library writes it, BCC batches, custom \'Cc\'' => ['smtp', 'Cc', true, true, 'Cc', [['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com']], [['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com']]], + 'smtp, Cc, library writes it, BCC batches, custom \'cc\'' => ['smtp', 'Cc', true, true, 'cc', [['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com']], [['Cc: c@x.com', 'To: t@x.com', 'cc: custom@co.example'], ['Cc: c@x.com', 'cc: custom@co.example']]], + 'smtp, Cc, library writes it, BCC batches, custom \'CC\'' => ['smtp', 'Cc', true, true, 'CC', [['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com']], [['CC: custom@co.example', 'Cc: c@x.com', 'To: t@x.com'], ['CC: custom@co.example', 'Cc: c@x.com']]], + 'smtp, Cc, library writes it, BCC batches, custom \'cC\'' => ['smtp', 'Cc', true, true, 'cC', [['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com']], [['Cc: c@x.com', 'To: t@x.com', 'cC: custom@co.example'], ['Cc: c@x.com', 'cC: custom@co.example']]], + 'smtp, Cc, library writes it, BCC batches, custom \'Cc \'' => ['smtp', 'Cc', true, true, 'Cc ', [['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com']], [['Cc : custom@co.example', 'Cc: c@x.com', 'To: t@x.com'], ['Cc : custom@co.example', 'Cc: c@x.com']]], + 'smtp, Cc, library writes it, BCC batches, custom \' Cc\'' => ['smtp', 'Cc', true, true, ' Cc', [['Cc: c@x.com', 'To: t@x.com'], ['Cc: c@x.com']], [[' Cc: custom@co.example', 'Cc: c@x.com', 'To: t@x.com'], [' Cc: custom@co.example', 'Cc: c@x.com']]], + 'smtp, Cc, library does not, one message, custom \'Cc\'' => ['smtp', 'Cc', false, false, 'Cc', [['Cc: custom@co.example', 'To: t@x.com']], [['Cc: custom@co.example', 'To: t@x.com']]], + 'smtp, Cc, library does not, one message, custom \'cc\'' => ['smtp', 'Cc', false, false, 'cc', [['To: t@x.com', 'cc: custom@co.example']], [['To: t@x.com', 'cc: custom@co.example']]], + 'smtp, Cc, library does not, one message, custom \'CC\'' => ['smtp', 'Cc', false, false, 'CC', [['CC: custom@co.example', 'To: t@x.com']], [['CC: custom@co.example', 'To: t@x.com']]], + 'smtp, Cc, library does not, one message, custom \'cC\'' => ['smtp', 'Cc', false, false, 'cC', [['To: t@x.com', 'cC: custom@co.example']], [['To: t@x.com', 'cC: custom@co.example']]], + 'smtp, Cc, library does not, one message, custom \'Cc \'' => ['smtp', 'Cc', false, false, 'Cc ', [['Cc : custom@co.example', 'To: t@x.com']], [['Cc : custom@co.example', 'To: t@x.com']]], + 'smtp, Cc, library does not, one message, custom \' Cc\'' => ['smtp', 'Cc', false, false, ' Cc', [[' Cc: custom@co.example', 'To: t@x.com']], [[' Cc: custom@co.example', 'To: t@x.com']]], + 'smtp, Cc, library does not, BCC batches, custom \'Cc\'' => ['smtp', 'Cc', false, true, 'Cc', [['Cc: custom@co.example', 'To: t@x.com'], ['Cc: custom@co.example']], [['Cc: custom@co.example', 'To: t@x.com'], ['Cc: custom@co.example']]], + 'smtp, Cc, library does not, BCC batches, custom \'cc\'' => ['smtp', 'Cc', false, true, 'cc', [['To: t@x.com', 'cc: custom@co.example'], ['cc: custom@co.example']], [['To: t@x.com', 'cc: custom@co.example'], ['cc: custom@co.example']]], + 'smtp, Cc, library does not, BCC batches, custom \'CC\'' => ['smtp', 'Cc', false, true, 'CC', [['CC: custom@co.example', 'To: t@x.com'], ['CC: custom@co.example']], [['CC: custom@co.example', 'To: t@x.com'], ['CC: custom@co.example']]], + 'smtp, Cc, library does not, BCC batches, custom \'cC\'' => ['smtp', 'Cc', false, true, 'cC', [['To: t@x.com', 'cC: custom@co.example'], ['cC: custom@co.example']], [['To: t@x.com', 'cC: custom@co.example'], ['cC: custom@co.example']]], + 'smtp, Cc, library does not, BCC batches, custom \'Cc \'' => ['smtp', 'Cc', false, true, 'Cc ', [['Cc : custom@co.example', 'To: t@x.com'], ['Cc : custom@co.example']], [['Cc : custom@co.example', 'To: t@x.com'], ['Cc : custom@co.example']]], + 'smtp, Cc, library does not, BCC batches, custom \' Cc\'' => ['smtp', 'Cc', false, true, ' Cc', [[' Cc: custom@co.example', 'To: t@x.com'], [' Cc: custom@co.example']], [[' Cc: custom@co.example', 'To: t@x.com'], [' Cc: custom@co.example']]], + 'smtp, Bcc, library writes it, one message, custom \'Bcc\'' => ['smtp', 'Bcc', true, false, 'Bcc', [['Bcc: custom@co.example', 'To: t@x.com']], [['Bcc: custom@co.example', 'To: t@x.com']]], + 'smtp, Bcc, library writes it, one message, custom \'bcc\'' => ['smtp', 'Bcc', true, false, 'bcc', [['To: t@x.com', 'bcc: custom@co.example']], [['To: t@x.com', 'bcc: custom@co.example']]], + 'smtp, Bcc, library writes it, one message, custom \'BCC\'' => ['smtp', 'Bcc', true, false, 'BCC', [['BCC: custom@co.example', 'To: t@x.com']], [['BCC: custom@co.example', 'To: t@x.com']]], + 'smtp, Bcc, library writes it, one message, custom \'bCC\'' => ['smtp', 'Bcc', true, false, 'bCC', [['To: t@x.com', 'bCC: custom@co.example']], [['To: t@x.com', 'bCC: custom@co.example']]], + 'smtp, Bcc, library writes it, one message, custom \'Bcc \'' => ['smtp', 'Bcc', true, false, 'Bcc ', [['Bcc : custom@co.example', 'To: t@x.com']], [['Bcc : custom@co.example', 'To: t@x.com']]], + 'smtp, Bcc, library writes it, one message, custom \' Bcc\'' => ['smtp', 'Bcc', true, false, ' Bcc', [[' Bcc: custom@co.example', 'To: t@x.com']], [[' Bcc: custom@co.example', 'To: t@x.com']]], + 'smtp, Bcc, library writes it, BCC batches, custom \'Bcc\'' => ['smtp', 'Bcc', true, true, 'Bcc', [['Bcc: custom@co.example', 'To: t@x.com'], ['Bcc: custom@co.example']], [['Bcc: custom@co.example', 'To: t@x.com'], ['Bcc: custom@co.example']]], + 'smtp, Bcc, library writes it, BCC batches, custom \'bcc\'' => ['smtp', 'Bcc', true, true, 'bcc', [['To: t@x.com', 'bcc: custom@co.example'], ['bcc: custom@co.example']], [['To: t@x.com', 'bcc: custom@co.example'], ['bcc: custom@co.example']]], + 'smtp, Bcc, library writes it, BCC batches, custom \'BCC\'' => ['smtp', 'Bcc', true, true, 'BCC', [['BCC: custom@co.example', 'To: t@x.com'], ['BCC: custom@co.example']], [['BCC: custom@co.example', 'To: t@x.com'], ['BCC: custom@co.example']]], + 'smtp, Bcc, library writes it, BCC batches, custom \'bCC\'' => ['smtp', 'Bcc', true, true, 'bCC', [['To: t@x.com', 'bCC: custom@co.example'], ['bCC: custom@co.example']], [['To: t@x.com', 'bCC: custom@co.example'], ['bCC: custom@co.example']]], + 'smtp, Bcc, library writes it, BCC batches, custom \'Bcc \'' => ['smtp', 'Bcc', true, true, 'Bcc ', [['Bcc : custom@co.example', 'To: t@x.com'], ['Bcc : custom@co.example']], [['Bcc : custom@co.example', 'To: t@x.com'], ['Bcc : custom@co.example']]], + 'smtp, Bcc, library writes it, BCC batches, custom \' Bcc\'' => ['smtp', 'Bcc', true, true, ' Bcc', [[' Bcc: custom@co.example', 'To: t@x.com'], [' Bcc: custom@co.example']], [[' Bcc: custom@co.example', 'To: t@x.com'], [' Bcc: custom@co.example']]], + 'smtp, Bcc, library does not, one message, custom \'Bcc\'' => ['smtp', 'Bcc', false, false, 'Bcc', [['Bcc: custom@co.example', 'To: t@x.com']], [['Bcc: custom@co.example', 'To: t@x.com']]], + 'smtp, Bcc, library does not, one message, custom \'bcc\'' => ['smtp', 'Bcc', false, false, 'bcc', [['To: t@x.com', 'bcc: custom@co.example']], [['To: t@x.com', 'bcc: custom@co.example']]], + 'smtp, Bcc, library does not, one message, custom \'BCC\'' => ['smtp', 'Bcc', false, false, 'BCC', [['BCC: custom@co.example', 'To: t@x.com']], [['BCC: custom@co.example', 'To: t@x.com']]], + 'smtp, Bcc, library does not, one message, custom \'bCC\'' => ['smtp', 'Bcc', false, false, 'bCC', [['To: t@x.com', 'bCC: custom@co.example']], [['To: t@x.com', 'bCC: custom@co.example']]], + 'smtp, Bcc, library does not, one message, custom \'Bcc \'' => ['smtp', 'Bcc', false, false, 'Bcc ', [['Bcc : custom@co.example', 'To: t@x.com']], [['Bcc : custom@co.example', 'To: t@x.com']]], + 'smtp, Bcc, library does not, one message, custom \' Bcc\'' => ['smtp', 'Bcc', false, false, ' Bcc', [[' Bcc: custom@co.example', 'To: t@x.com']], [[' Bcc: custom@co.example', 'To: t@x.com']]], ]; } /** * @dataProvider matrix * - * @param list $expected The header lines now. - * @param list $expectedBase The header lines 3.0.0 produced. + * @param list> $expected Header lines per message, now. + * @param list> $expectedBase Header lines per message, 3.0.0. */ public function testCustomRecipientHeaders( string $transport, string $header, bool $libraryWrites, + bool $batch, string $customName, array $expected, array $expectedBase, ): void { - $captured = ''; + $messages = []; $client = null; $mailer = Mailer::newInstance(['newline' => "\n"]); if ($transport === 'mail') { $mailer->setTransport(new MailTransport( new EmailValidator(), - static function (string $to, string $subject, string $message, string $headers) use (&$captured): bool { - $captured = $headers; + static function (string $to, string $subject, string $message, string $headers) use (&$messages): bool { + $messages[] = $headers; return true; }, @@ -109,79 +274,95 @@ static function (string $to, string $subject, string $message, string $headers) $mailer->setTransport(new SendmailTransport( '/usr/sbin/sendmail', new EmailValidator(), - static function (string $command, string $message) use (&$captured): int { - $captured = $message; + static function (string $command, string $message) use (&$messages): int { + $messages[] = $message; return 0; }, )); } else { - $recipients = $header === 'To' || !$libraryWrites ? 1 : 2; - $replies = ["220 hi\r\n", "250 ehlo\r\n", "250 from\r\n"]; - for ($i = 0; $i < $recipients; ++$i) { - $replies[] = "250 rcpt\r\n"; - } - $client = new FakeSocketClient([...$replies, "354 data\r\n", "250 queued\r\n", "221 bye\r\n"]); + $client = new RespondingSocketClient(); $mailer->setTransport( new SmtpTransport($client, 'mx.example.com', 25, 5, '', false, '', '', false, false, true, 'localhost', "\n"), ); } - $mailer->setFrom('s@example.com')->setSubject('Subject')->setMessage('Body'); + $mailer->setFrom('s@x.com')->setSubject('S')->setMessage('B'); if ($header === 'To') { - $libraryWrites ? $mailer->setTo('t@x.com') : $mailer->setBCC('b@x.com'); + if ($libraryWrites) { + $mailer->setTo('t@x.com'); + } elseif (!$batch) { + $mailer->setBCC('b@x.com'); + } } else { $mailer->setTo('t@x.com'); - if ($libraryWrites) { - $header === 'Cc' ? $mailer->setCC('c@x.com') : $mailer->setBCC('b@x.com'); + if ($header === 'Cc' && $libraryWrites) { + $mailer->setCC('c@x.com'); } + if ($header === 'Bcc' && $libraryWrites && !$batch) { + $mailer->setBCC('b@x.com'); + } + } + if ($batch) { + $mailer->setBCC(['b1@x.com', 'b2@x.com', 'b3@x.com'], 2); } $mailer->setHeader($customName, 'custom@co.example'); $mailer->send(); if ($client !== null) { - $captured = $client->conversation(); + $messages = $client->conversations(); } - $lines = array_map('rtrim', array_values(array_filter( - explode("\n", $captured), - static fn(string $line): bool => preg_match('/^(to|cc|bcc):/i', $line) === 1, - ))); - sort($lines); + $actual = array_map(static function (string $message): array { + $lines = array_map('rtrim', array_values(array_filter( + explode("\n", $message), + static fn(string $line): bool => preg_match('/^\s*(to|cc|bcc)\s*:/i', $line) === 1, + ))); + sort($lines); + + return $lines; + }, $messages); - self::assertSame($expected, $lines); + self::assertSame($expected, $actual); } /** - * Compares the two recorded columns (it does not run the mailer) and allows - * the difference from 3.0.0 to be only of the two intended kinds, so a changed - * expectation that drops or adds anything else shows up here. + * Compares the two recorded columns (it does not run the mailer): `To` rows + * must be identical to 3.0.0, and for `Cc` and `Bcc` the only line that may + * be missing is the custom header's own, and only where the library writes + * that header. * * @dataProvider matrix * - * @param list $expected - * @param list $expectedBase + * @param list> $expected + * @param list> $expectedBase */ public function testTheOutputDiffersFromThe300OutputOnlyWhereIntended( string $transport, string $header, bool $libraryWrites, + bool $batch, string $customName, array $expected, array $expectedBase, ): void { - $removed = array_diff($expectedBase, $expected); - $added = array_diff($expected, $expectedBase); - $customLine = $customName . ': custom@co.example'; - - // Only the custom header's own line may disappear, and only when the - // library writes that header (it used to come out as a duplicate)... - $mayRemove = $libraryWrites ? [$customLine] : []; - // ...and the only line that may appear is a custom To that 3.0.0 lost on - // sendmail and SMTP although the library wrote no To. - $mayAdd = $header === 'To' && !$libraryWrites && $transport !== 'mail' ? [$customLine] : []; - - self::assertSame([], array_values(array_diff($removed, $mayRemove)), 'Unexpected line removed.'); - self::assertSame([], array_values(array_diff($added, $mayAdd)), 'Unexpected line added.'); + if ($header === 'To') { + self::assertSame($expectedBase, $expected, 'A custom To must behave exactly as in 3.0.0.'); + + return; + } + + self::assertCount(count($expectedBase), $expected); + foreach ($expectedBase as $index => $baseLines) { + self::assertSame( + [], + array_values(array_diff($expected[$index], $baseLines)), + 'No line may appear that 3.0.0 did not write.', + ); + foreach (array_diff($baseLines, $expected[$index]) as $removed) { + self::assertStringEndsWith(': custom@co.example', $removed); + self::assertTrue($libraryWrites, 'A custom header the library does not write must not be removed.'); + } + } } } diff --git a/tests/Support/RespondingSocketClient.php b/tests/Support/RespondingSocketClient.php new file mode 100644 index 0000000..623ddf9 --- /dev/null +++ b/tests/Support/RespondingSocketClient.php @@ -0,0 +1,79 @@ + */ + public array $writes = []; + + private bool $open = false; + + private string $next = "220 hi\r\n"; + + public function open(string $host, int $port, int $timeout): void + { + $this->open = true; + $this->next = "220 hi\r\n"; + } + + public function isOpen(): bool + { + return $this->open; + } + + public function write(string $bytes): void + { + $this->writes[] = $bytes; + $this->next = match (true) { + str_starts_with($bytes, 'DATA') => "354 go\r\n", + str_starts_with($bytes, 'QUIT') => "221 bye\r\n", + default => "250 ok\r\n", + }; + } + + public function readLine(int $length = 512): string + { + return $this->next; + } + + public function enableCrypto(int $cryptoMethod): bool + { + return true; + } + + public function close(): void + { + $this->open = false; + } + + /** + * Everything written, as one string per connection (split after each QUIT). + * + * @return list + */ + public function conversations(): array + { + $all = implode('', $this->writes); + $parts = []; + foreach (\explode("QUIT\n", $all) as $part) { + if ($part !== '') { + $parts[] = $part; + } + } + + return $parts; + } +} From ab6284ed141040cc89fa9f38d55eec823d2cc029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammet=20=C5=9Eafak?= Date: Wed, 25 Nov 2026 19:40:57 +0300 Subject: [PATCH 2/2] fix: make a display name safe as in 3.0.0 instead of rejecting it A display name with a CR, LF or NUL was refused although 3.0.0 wrote it safely (an ASCII name is quoted with control characters backslash-escaped, a non-ASCII name is Q-encoded with the line breaks removed). Address no longer rejects the name or the raw input, only the address part; QEncoder again encodes a NUL (=00) as 3.0.0 did. Tests pin 168 whole messages against the 3.0.0 output and check that the rendered header block gains no header from the name. Also: limit the "same as 3.0.0" claim for custom To to the spellings that are accepted (spaces around the name, not tabs), test a trailing LF and tabs in header names, document the leading space in isValidName(), and list the behaviour changes under Changed in the changelog. --- CHANGELOG.md | 44 +++++++--- docs/API-Reference.md | 2 +- docs/Encoding-and-Headers.md | 6 +- docs/Error-Handling.md | 8 +- docs/FAQ.md | 6 +- docs/Sending-Mail.md | 7 +- src/Encoding/QEncoder.php | 2 +- src/Message/Address.php | 21 ++--- src/Message/HeaderRenderer.php | 8 +- tests/DisplayNameTest.php | 127 +++++++++++++++++++++++++++ tests/Encoding/QEncoderTest.php | 15 +++- tests/HeaderInjectionTest.php | 46 +++++----- tests/Message/AddressTest.php | 24 +++-- tests/Message/HeaderRendererTest.php | 2 + 14 files changed, 245 insertions(+), 73 deletions(-) create mode 100644 tests/DisplayNameTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 15c38a9..ede78a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,16 +8,20 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Security -- A CR, LF or NUL in an address, display name or return path is now rejected - with an `InvalidAddressException` **whether or not** the `validate` option is - on (`Address`, `setFrom()`, `setReplyTo()`, `setTo()`, `setCC()`, `setBCC()`). +- A CR, LF or NUL in an address or return path is now rejected with an + `InvalidAddressException` **whether or not** the `validate` option is on + (`Address`, `setFrom()`, `setReplyTo()`, `setTo()`, `setCC()`, `setBCC()`). A + display name is not rejected: as in 3.0.0, an ASCII name is quoted with its + control characters backslash-escaped and a non-ASCII name is Q-encoded with the + line breaks removed, so it cannot start another header. Before, `validate => false` also switched off the only line-break check, so a request-supplied address could add headers or SMTP commands. Leading and trailing line breaks are still trimmed, and a recipient string is still split on whitespace. - `setHeader()` throws a `MailerException` for a header name containing a CR, LF or NUL. A header value keeps its documented behaviour: CR and LF are stripped, - and NUL is now stripped too. The subject is stripped of NUL as well. + and NUL is now stripped too. The subject keeps its behaviour too: CR and LF are + removed and a NUL is Q-encoded (`=00`), never written raw. - Attachment names, dispositions and MIME types containing a CR, LF or NUL throw an `AttachmentException`; the `userAgent` and `charset` options containing one throw a `ConfigurationException`. @@ -25,6 +29,27 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). (`InvalidAddressException`) or a client hostname containing a CR, LF or NUL (`TransportException`) before it connects, instead of writing it to the socket. +### Changed + +Behaviours that differ from 3.0.0 and are not security fixes in themselves: + +- `setHeader()` throws a `MailerException` for a header name that is empty, has a + tab or other whitespace, an inner space, a colon, or a non-ASCII or control + character. 3.0.0 wrote `X: y: value` or a name with a tab as given; a colon or + empty name produced a broken header. Spaces around a name are still accepted. +- A custom header with the same name as one the library writes is no longer + written as a second line: `from`, `date`, `message-id`, `user-agent`, `x-mailer`, + `x-sender`, `x-priority`, `mime-version`, `reply-to`, `return-path`, + `content-type`, `content-transfer-encoding` (any case), `cc` next to + `setCC()`, `bcc` next to `setBCC()` on sendmail and `mail()`, and `subject` (any case, + and also the exact `Subject` on `mail()`). Before, 3.0.0 wrote both. A custom `cc`/`bcc` that followed a real CC + or BCC list used to add that address as a recipient on sendmail and `mail()`; + it no longer does. `To` is not affected. +- A header the library overrides keeps the position it has in the library's own + list instead of the custom header's position. +- `"a>b"@example.com`-style addresses (a `<` or `>` inside the address) are + rejected; the SMTP server greeting must be `220`. + ### Fixed - **SMTP connection state after a failure.** A failed greeting, `EHLO`, `STARTTLS`, @@ -37,9 +62,7 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - `StreamSocketClient::enableCrypto()` refuses to start TLS while clear-text bytes are still in the read buffer (STARTTLS response injection). - The `Message-ID` domain is taken from the parsed sender, not from the raw - `fromEmail` option, and `Address::parse()` checks the whole input before the - display name is split off, so a CR, LF or NUL cannot hide in the part that is - discarded. + `fromEmail` option, so a CR, LF or NUL in that option cannot reach the header. - A `<` or `>` inside an address is rejected (even with `validate` off): it could close the SMTP path and add parameters (`RCPT TO: NOTIFY=NEVER>`). - `setHeader()`: a header the library writes itself for the message (`Cc` with CC @@ -48,9 +71,10 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). next to a real BCC list, is dropped instead of added as a second header). A header the library does not write is kept as before. `To` is not special-cased: a custom `To` behaves exactly as in 3.0.0 on `mail()`, sendmail and SMTP, - including in BCC batches, so it adds no recipient that 3.0.0 did not. A name - with a tab, an inner space, a colon, or any non-printable or non-ASCII - character throws a `MailerException`; spaces around a name are still accepted. + including in BCC batches, so it adds no recipient that 3.0.0 did not, for every + spelling of the name that is still accepted (letter case, and spaces around + it). A name with a tab or other whitespace, an inner space, a colon, or any + non-printable or non-ASCII character throws a `MailerException` (see Changed). - `setAltMessage()` strips carriage returns like `setMessage()`. The SMTP dot-stuffing now also handles a bare `"\r"` or `"\n\r"` newline. - A `"` or `\` in an attachment name is escaped inside the quoted `name` diff --git a/docs/API-Reference.md b/docs/API-Reference.md index 69cb98f..ade6903 100644 --- a/docs/API-Reference.md +++ b/docs/API-Reference.md @@ -50,7 +50,7 @@ with that batch size. **Throws** `InvalidAddressException` (same line-break rule ### `setSubject(string $subject): self` Sets the subject. Encoded (RFC 2047 Q) automatically when it contains non-ASCII. -CR, LF and NUL are removed when the message is built. +CR and LF are removed when the message is built; a NUL is Q-encoded (`=00`). ### `setMessage(string $body): self` Sets the body (plain text or HTML depending on the mail type). Carriage returns diff --git a/docs/Encoding-and-Headers.md b/docs/Encoding-and-Headers.md index b8d0b91..3c71df3 100644 --- a/docs/Encoding-and-Headers.md +++ b/docs/Encoding-and-Headers.md @@ -30,8 +30,10 @@ $mailer->setSubject('Café ☕'); ``` Encoding happens when the message is built, so you always pass plain strings. -Line breaks and NUL are removed from a subject; a display name containing one is -rejected with an `InvalidAddressException`. With a `UTF-8` charset, bytes that are +Line breaks are removed from a subject (a NUL is Q-encoded as `=00`). A display +name with a line break or NUL is made safe the same way it always was: an ASCII +name is quoted with the control characters backslash-escaped, a non-ASCII name is +Q-encoded with the line breaks removed; it is not rejected. With a `UTF-8` charset, bytes that are not valid UTF-8 are replaced with `?` before encoding. With any other charset the bytes are encoded as they are and must already be in that charset (the mailer does not convert them). diff --git a/docs/Error-Handling.md b/docs/Error-Handling.md index a4d2658..3f117df 100644 --- a/docs/Error-Handling.md +++ b/docs/Error-Handling.md @@ -14,7 +14,7 @@ MailerException (extends \RuntimeException) | Exception | Thrown when | Thrown by | | --- | --- | --- | -| `InvalidAddressException` | An address fails validation (and validation is on), or an address or return path contains a CR, LF, NUL, `<` or `>`, or a display name a CR, LF or NUL (always). | `setFrom()`, `setReplyTo()`, `setTo()`, `setCC()`, `setBCC()`, `send()` | +| `InvalidAddressException` | An address fails validation (and validation is on), or an address or return path contains a CR, LF, NUL, `<` or `>` (always). | `setFrom()`, `setReplyTo()`, `setTo()`, `setCC()`, `setBCC()`, `send()` | | `ConfigurationException` | A required value is missing: no sender, no recipient, empty SMTP host, auth without credentials; or `userAgent`/`charset` contains a CR, LF or NUL. | `send()`, `new Mailer()` | | `AttachmentException` | A file can't be read, its type can't be detected, the name, disposition or MIME type contains a CR, LF or NUL, or `setAttachmentCID()` finds no match. | `attach()`, `attachContent()`, `setAttachmentCID()` | | `MailerException` (the base type) | A custom header name is not a valid field name; a MIME boundary from `setBoundaryFactory()` is empty or contains a quote, CR, LF or NUL; a header or subject handed to a transport by hand contains a line break other than a fold. | `setHeader()`, `send()` | @@ -58,12 +58,14 @@ $mailer = Mailer::newInstance(['validate' => false]); ``` > **Security:** `validate => false` removes the format check on addresses, but -> not the character check. A CR, LF or NUL inside an address or a display name +> not the character check. A CR, LF or NUL inside an address > (`user@example.com\r\nBcc: attacker@example.test`), or a `<` or `>` inside an > address (`user@example.com> NOTIFY=NEVER`), is rejected with an > `InvalidAddressException` whether validation is on or off, so a request-supplied > address cannot start another header or add parameters to an SMTP command. Only -> a leading or trailing line break is trimmed. The check does not limit how many +> a leading or trailing line break is trimmed. A display name is never rejected: +> a line break or NUL in it is escaped or Q-encoded when the header is written, +> so it cannot start another header either. The check does not limit how many > recipients a string names: `"a@x.com,b@x.com"` is two. Avoid turning validation > off for addresses that come from user input, and pass a single untrusted > address as `setTo([$address])`. diff --git a/docs/FAQ.md b/docs/FAQ.md index af29f03..e3362a6 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -59,12 +59,14 @@ $mailer = Mailer::newInstance(['validate' => false]); ``` > **Security:** `validate => false` removes the format check on addresses, but -> not the character check. A CR, LF or NUL inside an address or a display name +> not the character check. A CR, LF or NUL inside an address > (`user@example.com\r\nBcc: attacker@example.test`), or a `<` or `>` inside an > address (`user@example.com> NOTIFY=NEVER`), is rejected with an > `InvalidAddressException` whether validation is on or off, so a request-supplied > address cannot start another header or add parameters to an SMTP command. Only -> a leading or trailing line break is trimmed. The check does not limit how many +> a leading or trailing line break is trimmed. A display name is never rejected: +> a line break or NUL in it is escaped or Q-encoded when the header is written, +> so it cannot start another header either. The check does not limit how many > recipients a string names: `"a@x.com,b@x.com"` is two. Avoid turning validation > off for addresses that come from user input, and pass a single untrusted > address as `setTo([$address])`. diff --git a/docs/Sending-Mail.md b/docs/Sending-Mail.md index 1979815..e68614d 100644 --- a/docs/Sending-Mail.md +++ b/docs/Sending-Mail.md @@ -72,9 +72,10 @@ try { Disable validation with `'validate' => false` only if you need to accept addresses the PHP filter rejects, and only for addresses you control. A CR, LF, -NUL, `<` or `>` inside an address, or a CR, LF or NUL in a display name, is -rejected with an `InvalidAddressException` even then, so a request-supplied -address cannot start another header or add parameters to an SMTP command — see +NUL, `<` or `>` inside an address is rejected with an `InvalidAddressException` +even then, so a request-supplied address cannot start another header or add +parameters to an SMTP command (a display name is not rejected; a line break or +NUL in it is escaped or Q-encoded when the header is written) — see [Error Handling](Error-Handling.md#fail-fast-on-bad-input). That check does **not** stop a request-supplied *string* from naming several diff --git a/src/Encoding/QEncoder.php b/src/Encoding/QEncoder.php index b12d8e1..e33719b 100644 --- a/src/Encoding/QEncoder.php +++ b/src/Encoding/QEncoder.php @@ -42,7 +42,7 @@ public function __construct( public function encode(string $str): string { - $str = str_replace(["\r", "\n", "\0"], '', $str); + $str = str_replace(["\r", "\n"], '', $str); $chars = null; if ($this->charset === 'UTF-8') { diff --git a/src/Message/Address.php b/src/Message/Address.php index 6e819da..1241385 100644 --- a/src/Message/Address.php +++ b/src/Message/Address.php @@ -31,35 +31,30 @@ final class Address { /** * @throws InvalidAddressException When the address contains a CR, LF, NUL, - * `<` or `>`, or the name a CR, LF or NUL. - * This is checked even when address - * validation is off. + * `<` or `>`. This is checked even when + * address validation is off. */ public function __construct( private readonly string $email, private readonly string $name = '', ) { + // Only the address is refused. A display name with a line break or NUL + // is not rejected: it is made safe when it is written (an ASCII name is + // escaped, a non-ASCII name is Q-encoded with the line breaks removed), + // as it always was. EmailValidator::assertSafeAddresses([$email]); - if (EmailValidator::hasLineBreakOrNul($name)) { - throw new InvalidAddressException('A display name must not contain a line break or a NUL character.'); - } } /** * Parses `"Name" `, `` or `email` into an Address. A display * name passed explicitly takes precedence over one embedded in `$raw`. * - * @throws InvalidAddressException When the address contains a CR, LF, NUL, - * `<` or `>`, or the name a CR, LF or NUL. + * @throws InvalidAddressException When the address part contains a CR, LF, + * NUL, `<` or `>`. */ public static function parse(string $raw, string $name = ''): self { $raw = trim($raw); - // Checked before the display name is split off, so a line break cannot - // hide in the part that is discarded when an explicit name wins. - if (EmailValidator::hasLineBreakOrNul($raw)) { - throw new InvalidAddressException('An e-mail address must not contain a line break or a NUL character.'); - } if (preg_match('/^(.*)<(.+)>$/', $raw, $matches) === 1) { if ($name === '') { $name = trim($matches[1], " \t\""); diff --git a/src/Message/HeaderRenderer.php b/src/Message/HeaderRenderer.php index d7b9612..ddd0ccf 100644 --- a/src/Message/HeaderRenderer.php +++ b/src/Message/HeaderRenderer.php @@ -105,9 +105,11 @@ public static function without(array $headers, array $names): array /** * Whether `$name` is usable as a header field name: printable ASCII other - * than `:`, optionally with spaces around it (`To ` was accepted before, and - * RFC 5322 allows whitespace before the colon). A line break, a NUL, a tab, - * a colon or an inner space is not. + * than `:`, optionally with spaces around it, as 3.0.0 accepted. RFC 5322 + * allows whitespace before the colon; a space before the name makes the line + * a continuation of the previous header, which is what 3.0.0 wrote too. A + * line break, a NUL, a tab or other whitespace, a colon or an inner space is + * not valid. * * @internal */ diff --git a/tests/DisplayNameTest.php b/tests/DisplayNameTest.php new file mode 100644 index 0000000..1395ffd --- /dev/null +++ b/tests/DisplayNameTest.php @@ -0,0 +1,127 @@ + name, From value, Reply-To value (3.0.0) + */ + public static function names(): array + { + return [ + 'name 0' => ['Ali', '"Ali" ', '"Ali" '], + 'name 1' => ["Ali\r\nBcc: e@z.com", '"Ali\\r\\nBcc: e@z.com" ', '"Ali\\r\\nBcc: e@z.com" '], + 'name 2' => ["Ali\nBcc: e@z.com", '"Ali\\nBcc: e@z.com" ', '"Ali\\nBcc: e@z.com" '], + 'name 3' => ["Ali\rBcc: e@z.com", '"Ali\\rBcc: e@z.com" ', '"Ali\\rBcc: e@z.com" '], + 'name 4' => ["Ali\0Veli", '"Ali\\000Veli" ', '"Ali\\000Veli" '], + 'name 5' => ["Ali\r\n", '"Ali" ', '"Ali" '], + 'name 6' => ["\r\nAli", '"Ali" ', '"Ali" '], + 'name 7' => ["Şali\r\nBcc: e@z.com", '=?UTF-8?Q?=C5=9EaliBcc:=20e@z.com?= ', '=?UTF-8?Q?=C5=9EaliBcc:=20e@z.com?= '], + 'name 8' => ["Şali\nX", '=?UTF-8?Q?=C5=9EaliX?= ', '=?UTF-8?Q?=C5=9EaliX?= '], + 'name 9' => ["Şali\rX", '=?UTF-8?Q?=C5=9EaliX?= ', '=?UTF-8?Q?=C5=9EaliX?= '], + 'name 10' => ["Şali\0X", '=?UTF-8?Q?=C5=9Eali=00X?= ', '=?UTF-8?Q?=C5=9Eali=00X?= '], + 'name 11' => ["Ali \"Q\" \\ 'x'", "\"Ali \\\"Q\\\" \\\\ \\'x\\'\" ", "\"Ali \\\"Q\\\" \\\\ \\'x\\'\" "], + 'name 12' => ["Şali\r\nBcc: e@z.com Şali\r\nBcc: e@z.com Şali\r\nBcc: e@z.com Şali\r\nBcc: e@z.com Şali\r\nBcc: e@z.com Şali\r\nBcc: e@z.com Şali\r\nBcc: e@z.com Şali\r\nBcc: e@z.com ", "=?UTF-8?Q?=C5=9EaliBcc:=20e@z.com=20=C5=9EaliBcc:=20e@z.com=20=C5=9EaliB?=\n =?UTF-8?Q?cc:=20e@z.com=20=C5=9EaliBcc:=20e@z.com=20=C5=9EaliBcc:=20e@z.c?=\n =?UTF-8?Q?om=20=C5=9EaliBcc:=20e@z.com=20=C5=9EaliBcc:=20e@z.com=20=C5=9E?=\n =?UTF-8?Q?aliBcc:=20e@z.com?= ", "=?UTF-8?Q?=C5=9EaliBcc:=20e@z.com=20=C5=9EaliBcc:=20e@z.com=20=C5=9EaliB?=\n =?UTF-8?Q?cc:=20e@z.com=20=C5=9EaliBcc:=20e@z.com=20=C5=9EaliBcc:=20e@z.c?=\n =?UTF-8?Q?om=20=C5=9EaliBcc:=20e@z.com=20=C5=9EaliBcc:=20e@z.com=20=C5=9E?=\n =?UTF-8?Q?aliBcc:=20e@z.com?= "], + 'name 13' => ["A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB A\r\nB ", '"A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB" ', '"A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB A\\r\\nB" '], + ]; + } + + /** + * @return array + */ + public static function namesWithValidation(): array + { + $out = []; + foreach (self::names() as $label => $row) { + $out[$label . ', validate on'] = [...$row, true]; + $out[$label . ', validate off'] = [...$row, false]; + } + + return $out; + } + + /** + * @dataProvider namesWithValidation + */ + public function testTheSenderNameIsWrittenAsIn300(string $name, string $from, string $replyTo, bool $validate): void + { + $transport = new RecordingTransport(); + Mailer::newInstance(['validate' => $validate])->setTransport($transport) + ->setFrom('s@x.com', $name)->setTo('t@x.com')->send(); + + $this->assertNameIsSafe($transport, $from, $replyTo === '' ? '' : $this->swap($replyTo, 's@x.com')); + } + + /** + * @dataProvider namesWithValidation + */ + public function testTheReplyToNameIsWrittenAsIn300(string $name, string $from, string $replyTo, bool $validate): void + { + $transport = new RecordingTransport(); + Mailer::newInstance(['validate' => $validate])->setTransport($transport) + ->setFrom('s@x.com')->setReplyTo('r@x.com', $name)->setTo('t@x.com')->send(); + + $this->assertSame($replyTo, $transport->last()->headers['Reply-To']); + $this->assertRenderedHeadersAreSafe($transport); + } + + /** + * @dataProvider namesWithValidation + */ + public function testTheConfiguredSenderNameIsWrittenAsIn300(string $name, string $from, string $replyTo, bool $validate): void + { + $transport = new RecordingTransport(); + Mailer::newInstance(['validate' => $validate, 'fromEmail' => 's@x.com', 'fromName' => $name]) + ->setTransport($transport)->setTo('t@x.com')->send(); + + $this->assertNameIsSafe($transport, $from, $this->swap($replyTo, 's@x.com')); + } + + private function assertNameIsSafe(RecordingTransport $transport, string $from, string $replyTo): void + { + $headers = $transport->last()->headers; + $this->assertSame($from, $headers['From']); + $this->assertSame($replyTo, $headers['Reply-To']); + $this->assertRenderedHeadersAreSafe($transport); + } + + /** + * Rendering is where a line break in a value would be refused; it must also + * not have produced a header of its own from the name. + */ + private function assertRenderedHeadersAreSafe(RecordingTransport $transport): void + { + $block = HeaderRenderer::render($transport->last()->headers, "\n"); + + $this->assertSame(0, preg_match('/^Bcc:/mi', $block), 'The name must not start a Bcc header.'); + $this->assertSame(1, preg_match_all('/^From:/m', $block)); + $this->assertSame(1, preg_match_all('/^Reply-To:/m', $block)); + $this->assertStringNotContainsString("\0", $block); + } + + /** + * The recorded Reply-To value uses r@x.com; with the sender as Reply-To the + * address is the sender's. + */ + private function swap(string $replyTo, string $address): string + { + return str_replace('', '<' . $address . '>', $replyTo); + } +} diff --git a/tests/Encoding/QEncoderTest.php b/tests/Encoding/QEncoderTest.php index 07dda2e..00083a2 100644 --- a/tests/Encoding/QEncoderTest.php +++ b/tests/Encoding/QEncoderTest.php @@ -63,12 +63,21 @@ public function testLongInputIsFoldedWithinTheLineLimit(): void } } - public function testStripsLineBreaksAndNul(): void + public function testStripsLineBreaks(): void { - $encoded = $this->utf8()->encode("Hello\r\nBcc: x@example.test\0"); + $encoded = $this->utf8()->encode("Hello\r\nBcc: x@example.test"); $this->assertSame('HelloBcc: x@example.test', iconv_mime_decode($encoded, 0, 'UTF-8')); - $this->assertStringNotContainsString("\0", $encoded); + } + + public function testANulIsEncodedNeverWrittenRaw(): void + { + foreach (["Hello\0X", "Hell\u{F6}\0X"] as $subject) { + $encoded = $this->utf8()->encode($subject); + + $this->assertStringNotContainsString("\0", $encoded); + $this->assertStringContainsString('=00', $encoded); + } } public function testInvalidUtf8IsReplacedWithoutNotices(): void diff --git a/tests/HeaderInjectionTest.php b/tests/HeaderInjectionTest.php index 7e57745..aea649e 100644 --- a/tests/HeaderInjectionTest.php +++ b/tests/HeaderInjectionTest.php @@ -65,16 +65,6 @@ public function testSetFromRejectsAnUnsafeAddressRegardlessOfValidate(bool $vali $this->mailer(['validate' => $validate])->setFrom("a@example.com\r\nBcc: attacker@example.test"); } - /** - * @dataProvider validationModes - */ - public function testSetFromRejectsAnUnsafeNameRegardlessOfValidate(bool $validate): void - { - $this->expectException(InvalidAddressException::class); - - $this->mailer(['validate' => $validate])->setFrom('a@example.com', "Name\r\nBcc: attacker@example.test"); - } - /** * @dataProvider validationModes */ @@ -171,19 +161,25 @@ public function testSetHeaderStillStripsLineBreaksAndNulFromTheValue(): void self::assertSame('valueBcc: attacker@example.test', $value); } - public function testSubjectLineBreaksAndNulAreStrippedFromTheHeader(): void + public function testSubjectLineBreaksAreStrippedAndNulIsEncoded(): void { $this->mailer(['validate' => false]) ->setFrom('s@example.com') ->setTo('a@example.com') - ->setSubject("Hello\r\nBcc: attacker@example.test\0") + ->setSubject("Hello\r\nBcc: attacker@example.test") ->send(); // Decoding shows what the header says: with the line breaks left in, the // Q-encoded value would decode to "Hello\r\nBcc: ...". $subject = $this->transport->last()->subject; self::assertSame('HelloBcc: attacker@example.test', iconv_mime_decode($subject, 0, 'UTF-8')); - self::assertStringNotContainsString("\0", $subject); + } + + public function testNulInTheSubjectIsEncodedNotWrittenRaw(): void + { + $this->mailer()->setFrom('s@example.com')->setTo('a@example.com')->setSubject("Hel\0lo")->send(); + + self::assertStringNotContainsString("\0", $this->transport->last()->subject); } public function testAnUnsafeConfiguredSenderIsRejectedAtSendTime(): void @@ -232,25 +228,24 @@ public function testMessageIdIsBuiltFromTheParsedConfiguredSender(string $email, /** * @dataProvider validationModes */ - public function testAHiddenLineBreakInTheConfiguredSenderIsRejected(bool $validate): void + public function testALineBreakHiddenInTheConfiguredSenderNameCannotReachTheMessageId(bool $validate): void { - // The explicit name wins, so the part of the address that carries the line - // breaks is the discarded "display name" of the embedded form. + // The address part is "a@example.com"; the rest is split off as a display + // name, which an explicit fromName replaces. Nothing of it may be written. $mailer = $this->mailer([ 'fromName' => 'App', 'fromEmail' => "x@y\rBcc: evil@z.com\rX: ", 'validate' => $validate, ]); - try { - $mailer->getMessageID(); - self::fail('getMessageID() accepted an unsafe configured sender.'); - } catch (InvalidAddressException) { - $this->addToAssertionCount(1); - } + self::assertSame(1, preg_match('/\A<[^\r\n\0]+@example\.com>\z/', $mailer->getMessageID())); - $this->expectException(InvalidAddressException::class); $mailer->setTo('rcpt@example.com')->send(); + + $headers = $this->transport->last()->headers; + self::assertSame('"App" ', $headers['From']); + self::assertSame(1, preg_match('/\A<[^\r\n\0]+@example\.com>\z/', $headers['Message-ID'])); + self::assertNotContains('evil@z.com', $headers); } /** @@ -347,6 +342,11 @@ public static function invalidHeaderNames(): array { return [ 'only spaces' => [' '], + 'trailing LF' => ["X-A\n"], + 'trailing CR' => ["X-A\r"], + 'trailing tab' => ["To\t"], + 'leading tab' => ["\tTo"], + 'vertical tab' => ["To\x0b"], 'inner space' => ['X Y'], 'colon' => ['X: y'], 'trailing colon' => ['X-A:'], diff --git a/tests/Message/AddressTest.php b/tests/Message/AddressTest.php index b2fd3f7..f928d53 100644 --- a/tests/Message/AddressTest.php +++ b/tests/Message/AddressTest.php @@ -54,7 +54,7 @@ public function testExplicitNameWinsOverEmbeddedName(): void /** * @dataProvider unsafeParts */ - public function testRejectsALineBreakOrNulInTheAddressOrName(string $email, string $name): void + public function testRejectsALineBreakOrNulInTheAddress(string $email, string $name): void { $this->expectException(InvalidAddressException::class); @@ -71,12 +71,10 @@ public static function unsafeParts(): array 'LF in the address' => ["a@example.com\nBcc: x@example.test", ''], 'CR in the address' => ["a@example.com\rBcc: x@example.test", ''], 'NUL in the address' => ["a@example.com\0", ''], - 'CRLF in the name' => ['a@example.com', "Name\r\nBcc: x@example.test"], - 'NUL in the name' => ['a@example.com', "Na\0me"], ]; } - public function testParseRejectsALineBreakInsideAnEmbeddedNameOrAddress(): void + public function testParseRejectsALineBreakInsideTheAddressPart(): void { $this->expectException(InvalidAddressException::class); @@ -90,13 +88,21 @@ public function testParseStillTrimsASurroundingLineBreak(): void $this->assertSame('a@example.com', $address->getEmail()); } - public function testParseRejectsALineBreakHiddenInTheNameThatWouldBeDiscarded(): void + public function testAnAddressIsNotRejectedForALineBreakInTheNameItCarries(): void { - $this->expectException(InvalidAddressException::class); + // The regex lets `.` match a CR, so the "name" part of this raw value + // holds the line break; the address is the clean part inside <...>. + $address = Address::parse("Evil\rBcc: x@example.test "); + + $this->assertSame('a@example.com', $address->getEmail()); + $this->assertSame("Evil\rBcc: x@example.test", $address->getName()); + } + + public function testADisplayNameWithALineBreakOrNulIsKeptNotRejected(): void + { + $address = new Address('a@example.com', "Na\r\nme\0"); - // The explicit name wins, so the embedded "name" is thrown away; the - // line break must be caught before that. - Address::parse("Evil\rBcc: x@example.test ", 'Explicit'); + $this->assertSame("Na\r\nme\0", $address->getName()); } /** diff --git a/tests/Message/HeaderRendererTest.php b/tests/Message/HeaderRendererTest.php index 324a06b..85f122d 100644 --- a/tests/Message/HeaderRendererTest.php +++ b/tests/Message/HeaderRendererTest.php @@ -77,6 +77,8 @@ public static function invalidNames(): array { return [ 'CRLF' => ["X\r\nBcc"], + 'trailing LF' => ["X-A\n"], + 'tab' => ["X-A\t"], 'space' => ['X A'], 'colon' => ['X:A'], ];