Skip to content

symfony/mime does not allow to add some headers as a strings, only as arrays #51584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sc0rp10 opened this issue Sep 6, 2023 · 0 comments · Fixed by #51601
Closed

symfony/mime does not allow to add some headers as a strings, only as arrays #51584

sc0rp10 opened this issue Sep 6, 2023 · 0 comments · Fixed by #51601

Comments

@sc0rp10
Copy link

sc0rp10 commented Sep 6, 2023

Symfony version(s) affected

6.3.3

Description

Hi, currently Symfony\Component\Mime\Header\Headers does not allow to set Mime headers in general way: some headers must be strings, some - arrays, but this fact cannot be guessed from the method signature or documentation. We observed this issue only in runtime - when the Symfony\Component\Mime\Header\Headers::addMailboxListHeader(): Argument #2 ($addresses) must be of type array, string given exception reported.

We wrote a common method that sends email with simple signature public function send(string $email, string $subject, string $content, array $headers = []): void;.
We assumed that the $headers is array<string, string> and that array is completely controlled by a client of our library, but at the moment we should have knowledge "which data type do we need to use given header name" and that knowledge is hard-coded inside the private constant Headers::HEADER_CLASS_MAP.

I think it's better to hide that knowledge inside the lybrary code and make client's code more simplier and clear.

How to reproduce

// First, run "composer require symfony/mime"
// Then, execute this file:

<?php
require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\Mime\Email;

$headers = [
    'x-foo' => 'bar',
    'reply-to' => 'foo@bar.com',
];

$message = (new Email())
    ->from('bar@foo.com')
    ->to('baz@foo.com')
    ->subject('hello')
    ->html('')
;

foreach ($headers as $name => $value) {
    $message->getHeaders()->addTextHeader($name, $value);
    /* when $name == 'reply-to' the Exception "Argument #2 ($addresses) must be of type array, string given" will be thrown */
}

Possible Solution

it's better to check the argument type inside the addHeader method when $method === 'addMailboxListHeader', because it's not clear for the client that calls the addHeader which exact data type have to be used here.
Suggested variant is:

      elseif ($method === 'addMailboxListHeader' && !\is_array($argument)) {
            $argument = [$argument];
        }

Additional Context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants