Laravel Mail ::发送邮件 - >循环播放

时间:2014-01-14 09:52:49

标签: laravel

我尝试从数据库填写电子邮件的接收者。如果我直接将地址写入$ mail->以便一切正常但不能使用此代码?:

$users = User::where('message_receiver','=',true)->get();
$data = array(
    'url'   =>  Config::get('app.url'),
    'name'  => 'test'
);

Mail::send('emails.message', $data, function($mail) use ($users)
{
    $mail->from('test@localhost.de','test@localhost.de');
    foreach ($users AS $user) {
        $mail->to($user->email, $user->firstname.' '.$user->surname);
    }
    $mail->subject('New Message');
});
循环中的

print_r($user->email)会转储正确的地址!

1 个答案:

答案 0 :(得分:1)

我从不与Laravel合作,而是通过此链接http://bundles.laravel.com/bundle/Messages

据我了解,要将电子邮件发送到多个电子邮件地址,您必须先将所有地址添加到数组中,然后将该数组传递给$message->to()方法,不是吗?

这是该链接的示例:

Message::send(function($message)
{
    $message->to(array('someone@gmail.com', 'email@address.com' => 'name'));
    $message->cc('more@addresses.com');
    $messages->bcc(array('evenmore@address.com' => 'Another name', 'onelast@address.com'));

    $message->from('me@gmail.com', 'Bob Marley');
    $message->subject('Hello!');
    $message->body('I really like spamming people!');
});

所以,在我的想法中,你应该尝试从$users创建数组,然后将其传递给方法,如下所示:

Mail::send('emails.message', $data, function($mail) use ($users)
{
    $mail->from('test@localhost.de','test@localhost.de');
    foreach ($users AS $user) {
        $receivers = array_add($receivers, $user->email, $user->email);
    }
    $mail->to($receivers);
    $mail->subject('New Message');
});

我不确定我是否正确使用了数组,但我认为你已经知道如何做到这一点。