Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/Symfony/Component/Mime/Header/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ final class Headers
'cc' => MailboxListHeader::class,
'bcc' => MailboxListHeader::class,
'message-id' => IdentificationHeader::class,
'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 ...
'references' => UnstructuredHeader::class, // ... `Message-ID`, even if that is no valid `msg-id`
'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 ...
'references' => [UnstructuredHeader::class, IdentificationHeader::class], // ... `Message-ID`, even if that is no valid `msg-id`
'return-path' => PathHeader::class,
];

Expand Down Expand Up @@ -137,7 +137,11 @@ public function addParameterizedHeader(string $name, string $value, array $param
*/
public function addHeader(string $name, mixed $argument, array $more = []): static
{
$parts = explode('\\', self::HEADER_CLASS_MAP[strtolower($name)] ?? UnstructuredHeader::class);
$headerClass = self::HEADER_CLASS_MAP[strtolower($name)] ?? UnstructuredHeader::class;
if (\is_array($headerClass)) {
$headerClass = $headerClass[0];
}
$parts = explode('\\', $headerClass);
$method = 'add'.ucfirst(array_pop($parts));
if ('addUnstructuredHeader' === $method) {
$method = 'addTextHeader';
Expand Down Expand Up @@ -220,10 +224,22 @@ public static function isUniqueHeader(string $name): bool
public static function checkHeaderClass(HeaderInterface $header): void
{
$name = strtolower($header->getName());
$headerClasses = self::HEADER_CLASS_MAP[$name] ?? [];
if (!\is_array($headerClasses)) {
$headerClasses = [$headerClasses];
}

if (!$headerClasses) {
return;
}

if (($c = self::HEADER_CLASS_MAP[$name] ?? null) && !$header instanceof $c) {
throw new LogicException(sprintf('The "%s" header must be an instance of "%s" (got "%s").', $header->getName(), $c, get_debug_type($header)));
foreach ($headerClasses as $c) {
if ($header instanceof $c) {
return;
}
}

throw new LogicException(sprintf('The "%s" header must be an instance of "%s" (got "%s").', $header->getName(), implode('" or "', $headerClasses), get_debug_type($header)));
}

public function toString(): string
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Mime/Tests/Header/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,27 @@ public function testInReplyToAcceptsNonIdentifierValues()
$this->assertEquals('foobar', $headers->get('In-Reply-To')->getBody());
}

public function testInReplyToAcceptsIdentifierValues()
{
$headers = new Headers();
$headers->addIdHeader('In-Reply-To', 'foo@bar.com');
$this->assertEquals('<foo@bar.com>', $headers->get('In-Reply-To')->getBodyAsString());
}

public function testReferencesAcceptsNonIdentifierValues()
{
$headers = new Headers();
$headers->addTextHeader('References', 'foobar');
$this->assertEquals('foobar', $headers->get('References')->getBody());
}

public function testReferencesAcceptsIdentifierValues()
{
$headers = new Headers();
$headers->addIdHeader('References', 'foo@bar.com');
$this->assertEquals('<foo@bar.com>', $headers->get('References')->getBodyAsString());
}

public function testHeaderBody()
{
$headers = new Headers();
Expand Down