Laravel 使用密件抄送字段时的 RFC 电子邮件合规性

Laravel RFC email compliance when using BCC fields

我知道这很常见,但看不到任何明确的答案。

代码如下:

$message->to('admin@website.com', 'All Users')
  ->bcc('test@hotmail.com,test2@hotmail.com', 'name,name2')
  ->subject(Input::get('emailsubject'));

这将引发此错误:

Address in mailbox given [test@hotmail.com,test2@hotmail.com] does not comply with RFC 2822, 3.6.2.

如果我只使用一封电子邮件就可以正常工作,我应该如何为列表创建数组?

从 DB 中提取时当前看起来像这样:

  $query = DB::table('users')->get();
  $bcclist = "";
  $bccnamelist = "";
  foreach ($query as $key=>$user) {
    if (filter_var($user->email, FILTER_VALIDATE_EMAIL)) {
        $bcclist .= $user->email.",";
        $bccnamelist .= $user->username.",";
    }
  }

然后使用它:

->bcc($bcclist, $bccnamelist)

电子邮件应该如何格式化?

Laravel 在后台使用 SwiftMailer,所以它是一个数组:

// without names
->bcc(['test@hotmail.com', 'test2@hotmail.com'])

// with names
->bcc(['test@hotmail.com' => 'name','test2@hotmail.com' => 'name2'])

从表面上看,您可以使用 array_combine($bcclist, $bccnamelist) 生成此数组。

http://swiftmailer.org/docs/messages.html#setting-cc-recipients