Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffde0f1

Browse files
committed
[Mime] Re-allow addIdHeader to be used for 'In-Reply-To' and 'References' headers
1 parent b1521f3 commit ffde0f1

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/Symfony/Component/Mime/Header/Headers.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ final class Headers
3434
'cc' => MailboxListHeader::class,
3535
'bcc' => MailboxListHeader::class,
3636
'message-id' => IdentificationHeader::class,
37-
'in-reply-to' => UnstructuredHeader::class, // `In-Reply-To` and `References` are less strict than RFC 2822 (3.6.4) to allow users entering the original email's ...
38-
'references' => UnstructuredHeader::class, // ... `Message-ID`, even if that is no valid `msg-id`
37+
'in-reply-to' => [UnstructuredHeader::class, IdentificationHeader::class], // `In-Reply-To` and `References` are less strict than RFC 2822 (3.6.4) to allow users entering the original email's ...
38+
'references' => [UnstructuredHeader::class, IdentificationHeader::class], // ... `Message-ID`, even if that is no valid `msg-id`
3939
'return-path' => PathHeader::class,
4040
];
4141

@@ -137,7 +137,11 @@ public function addParameterizedHeader(string $name, string $value, array $param
137137
*/
138138
public function addHeader(string $name, mixed $argument, array $more = []): static
139139
{
140-
$parts = explode('\\', self::HEADER_CLASS_MAP[strtolower($name)] ?? UnstructuredHeader::class);
140+
$headerClass = self::HEADER_CLASS_MAP[strtolower($name)] ?? UnstructuredHeader::class;
141+
if (\is_array($headerClass)) {
142+
$headerClass = $headerClass[0];
143+
}
144+
$parts = explode('\\', $headerClass);
141145
$method = 'add'.ucfirst(array_pop($parts));
142146
if ('addUnstructuredHeader' === $method) {
143147
$method = 'addTextHeader';
@@ -220,10 +224,22 @@ public static function isUniqueHeader(string $name): bool
220224
public static function checkHeaderClass(HeaderInterface $header): void
221225
{
222226
$name = strtolower($header->getName());
227+
$headerClasses = self::HEADER_CLASS_MAP[$name] ?? [];
228+
if (!\is_array($headerClasses)) {
229+
$headerClasses = [$headerClasses];
230+
}
231+
232+
if (!$headerClasses) {
233+
return;
234+
}
223235

224-
if (($c = self::HEADER_CLASS_MAP[$name] ?? null) && !$header instanceof $c) {
225-
throw new LogicException(sprintf('The "%s" header must be an instance of "%s" (got "%s").', $header->getName(), $c, get_debug_type($header)));
236+
foreach ($headerClasses as $c) {
237+
if ($header instanceof $c) {
238+
return;
239+
}
226240
}
241+
242+
throw new LogicException(sprintf('The "%s" header must be an instance of "%s" (got "%s").', $header->getName(), implode('" or "', $headerClasses), get_debug_type($header)));
227243
}
228244

229245
public function toString(): string

src/Symfony/Component/Mime/Tests/Header/HeadersTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,27 @@ public function testInReplyToAcceptsNonIdentifierValues()
287287
$this->assertEquals('foobar', $headers->get('In-Reply-To')->getBody());
288288
}
289289

290+
public function testInReplyToAcceptsIdentifierValues()
291+
{
292+
$headers = new Headers();
293+
$headers->addIdHeader('In-Reply-To', '[email protected]');
294+
$this->assertEquals('<[email protected]>', $headers->get('In-Reply-To')->getBodyAsString());
295+
}
296+
290297
public function testReferencesAcceptsNonIdentifierValues()
291298
{
292299
$headers = new Headers();
293300
$headers->addTextHeader('References', 'foobar');
294301
$this->assertEquals('foobar', $headers->get('References')->getBody());
295302
}
296303

304+
public function testReferencesAcceptsIdentifierValues()
305+
{
306+
$headers = new Headers();
307+
$headers->addIdHeader('References', '[email protected]');
308+
$this->assertEquals('<[email protected]>', $headers->get('References')->getBodyAsString());
309+
}
310+
297311
public function testHeaderBody()
298312
{
299313
$headers = new Headers();

0 commit comments

Comments
 (0)