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

3
在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')

    // ...

;

所以这是我的数组:

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

当我尝试像这样发送它时:

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

我收到了一个错误:
An address can be an instance of Address or a string ("array") given).

这里有什么问题?好的 - 让我们尝试用字符串发送它:
当我尝试像这样发送它时:
$recipients = "test1@test.de,test2@test.de";
$email->to($recipients);

我又遇到了一个错误:

邮箱“test1@test.de,test2@test.de”不符合RFC 2822的addr-spec规范。

有人能解释一下如何使用Symfony邮件程序发送电子邮件至多个->to()地址吗?

3
从你发布的第一个代码块中尝试使用$email->to(...$recipients); - Chris Haas
2个回答

15

你应该使用unpack函数来展开你的数组。 to()方法接受多个参数,每个参数必须是字符串Address的实例。

因此,在你的代码中,需要在$recipients之前添加...来展开数组。

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

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

0

你也可以这样做;

// Record(s) from database using entityManager.
$users = $em-> ....;
$recipients = array_map(function ($user) {
    return new Address($user->getEmail(), $user->getFirstname());
}, $users);

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

什么是“this”?$em-> ... 看起来对我来说不像是有效的 PHP 代码。 - Nico Haase
$entityManager,如果您正在尝试从数据库中检索记录。 - Abiye Chris. I. Surulere
请通过编辑您的答案添加所有澄清。这对解决问题有何影响? - Nico Haase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接