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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21823586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:03:05  来源:igfitidea点击:

Sent mail to multiple address using Laravel 4 queue

phplaravellaravel-4

提问by Chinmoy

I'm using Laravel 4 , tying to send mail to multiple user using Queueing Mail , my code looks like -

我正在使用 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('[email protected]', 'Mail Notification');
      foreach ($mailuserlist as $value) {
            $message->to($value['email'],$value['firstname'].' '.$value['lastname']);
    }
    $message->subject('Testing mail');
});

..it's not at all working . How can i send ail to multiple address ??

..它根本不起作用。我如何将 ail 发送到多个地址?

回答by itsazzad

It should be possible in two ways as we can see in the source code framework/src/Illuminate/Mail/Message.php:

正如我们在源代码framework/src/Illuminate/Mail/Message.php 中看到的那样,应该可以通过两种方式实现:

  1. Chaining
  2. Using array
  1. 链接
  2. 使用数组

Chaining:

链接:

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

Using array of addresses:

使用地址数组:

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

回答by Koala Yeung

Queueing Maildoesn't seem to support sending 1 mail to multiple user. I think you should queue 1 mail for each of your recipients:

排队邮件似乎不支持向多个用户发送 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('[email protected]', 'Mail Notification')
          ->to($mailuser['email'],
            $mailuser['firstname'].' '.$mailuser['lastname'])
          ->subject('Testing mail');
    });
}

回答by Knnwulf

You can get all emails to send

您可以获得所有要发送的电子邮件

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

And delete the key of array for get an array only with the emails

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

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

And then run the Mail::queue

然后运行 ​​Mail::queue

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

回答by vampire

**

**

Sending multiple users

发送多个用户

**

**

$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);
            }

        });
    }