Symfony 5 邮件收件人多个地址不起作用

gol*_*ife 3 php symfony

在 Symfony 文档中有这样的条目:

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

...您可以将多个地址传递给每个方法:

$toAddresses = ['foo@example.com', new Address('bar@example.com')];
$email = (new Email())
    ->to(...$toAddresses)
    ->cc('cc1@example.com', 'cc2@example.com')

    // ...
Run Code Online (Sandbox Code Playgroud)

;

这是我的数组:

$recipients = [
    'test1@test.de',
    'test2@test.de'
];
Run Code Online (Sandbox Code Playgroud)

当我尝试像这样发送时:

$recipients = [
    'test1@test.de',
    'test2@test.de'
];
$email->to($recipients);
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

An address can be an instance of Address or a string ("array") given).
Run Code Online (Sandbox Code Playgroud)

这里有什么问题吗?好的 - 让我们尝试用字符串发送它:

当我尝试像这样发送时:

$recipients = "test1@test.de,test2@test.de";
$email->to($recipients);
Run Code Online (Sandbox Code Playgroud)

我收到另一个错误:

电子邮件“test1@test.de,test2@test.de”不符合 RFC 2822 的地址规范。

任何人都可以解释如何使用 symfony 邮件程序将电子邮件发送到多个->to()地址吗?

BAB*_*AFI 15

您应该解压您的阵列。to()方法接受多个参数,每个参数必须是字符串或Address的实例

因此,在您的代码中,您需要添加...before$recipients来解压数组。

$recipients = [
    'test1@test.de',
    'test2@test.de'
];

$email->to(...$recipients);
Run Code Online (Sandbox Code Playgroud)