使用Laravel 4队列将邮件发送到多个地址

时间:2014-02-17 07:56:59

标签: php laravel laravel-4

我正在使用Laravel 4,绑定使用排队邮件向多个用户发送邮件,我的代码看起来像 -

$mailuserlist=DB::table('table')
            ->join('some_table')
            ->select('some_thing')
            ->where('somecondition'))->get();

Mail::queue('mail_template', $data, function($message) use ($mailuserlist)
{
      $message->from('test@desto.co.in', 'Mail Notification');
      foreach ($mailuserlist as $value) {
            $message->to($value['email'],$value['firstname'].' '.$value['lastname']);
    }
    $message->subject('Testing mail');
});

..它根本不起作用。我怎样才能将所有地址发送到多个地址?

4 个答案:

答案 0 :(得分:6)

我们可以通过两种方式查看源代码framework/src/Illuminate/Mail/Message.php

  1. 链接
  2. 使用数组
  3. <强>链

    ->to($address1, $name1)->to($address2, $name2)->to($address3, $name3)...
    

    使用地址数组:

    ->to(array($address1,$address2,$address3,...), array($name1,$name2,$name3,...))
    

答案 1 :(得分:4)

Queueing Mail似乎不支持向多个用户发送1封邮件。我认为你应该为每个收件人排队1封邮件:

$mailuserlist=DB::table('table')
        ->join('some_table')
        ->select('some_thing')
        ->where('somecondition'))->get();

foreach ($mailuserlist as $mailuser) {
    Mail::queue('mail_template', $data, function($message) use ($mailuser) {
        $message
          ->from('test@desto.co.in', 'Mail Notification')
          ->to($mailuser['email'],
            $mailuser['firstname'].' '.$mailuser['lastname'])
          ->subject('Testing mail');
    });
}

答案 2 :(得分:0)

您可以收到要发送的所有电子邮件

$users = User::select('email')->get()->toArray();

删除数组的键,仅使用电子邮件获取数组

$emails = array_pluck($users, 'email');

然后运行Mail :: queue

Mail::queue('mail.your_view', [], function($message) use ($emails) {
    $message->from('test@from.com', 'Mail Notification');
    $message->to($emails);
    $message->subject('Your Subject');
});

答案 3 :(得分:0)

**

发送多个用户

**

$users = UsersGroup::where(['groups_id' => $group->id])->get();
    if(!$users->count()) {
        Mail::send('emails.groups.delete-group', [
            'group' => $group->title,
        ], function ($message) use ($group, $users) {

            $message->from(Config::get('mail.from.address'), Config::get('mail.from.name'))
                ->subject(Lang::get('groups.group_deleted'));

            foreach ($users as $user) {
                $message = $message->to($user->users->email);
            }

        });
    }