Home > Blockchain >  Symfony 5 Mailer Recipient more than one adress does not working
Symfony 5 Mailer Recipient more than one adress does not working

Time:07-08

In the Symfony Documentation there is this entry:

https://symfony.com/doc/current/mailer.html#email-addresses

...you can pass multiple addresses to each method:

$toAddresses = ['[email protected]', new Address('[email protected]')];
$email = (new Email())
    ->to(...$toAddresses)
    ->cc('[email protected]', '[email protected]')

    // ...

;

So this is my array:

$recipients = [
    '[email protected]',
    '[email protected]'
];

And when I try to send this like this:

$recipients = [
    '[email protected]',
    '[email protected]'
];
$email->to($recipients);

I got an error:

An address can be an instance of Address or a string ("array") given).

What is wrong here? Ok - Let's try to send it with a string:

When I try to send it like this:

$recipients = "[email protected],[email protected]";
$email->to($recipients);

I got another error:

Email "[email protected],[email protected]" does not comply with addr-spec of RFC 2822.

Can anyone explain how to send emails with the symfony mailer to more than one ->to() Address?

CodePudding user response:

You should unpack your array. to() method accepts multiple parameters and each parameter must be string or an instance of Address

So, in your code, you need to add ... before $recipients to unpack the array.

$recipients = [
    '[email protected]',
    '[email protected]'
];

$email->to(...$recipients);
  • Related