<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Jobs\CreateMessage;
use App\Jobs\SendUserEmail;
class UserController extends Controller
* 这个方法我们用来模拟发送消息队列.
* @param int $id
* @return \Illuminate\Http\Response
public function show($id)
$datas = \DB::table('user')->where('status',1)->get();
foreach($datas as $data){
$job = (new SendUserEmail($data->name,$data->email));
$this->dispatch($job);
return redirect('/user');
编辑队列文件
SendUserEmail.php
<?php
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
class SendUserEmail extends Job implements SelfHandling, ShouldQueue
use InteractsWithQueue, SerializesModels;
protected $name;
protected $email;
* Create a new job instance.
* @return void
public function __construct($name, $email)
$this->name = $name;
$this->email = $email;
* Execute the job.
* @return void
public function handle()
// 如果参试大于三次
if ($this->attempts() > 3) {
\Log::info($this->name.'邮件参试失败过多');
}else{
// 每次进来休息3秒钟
sleep(3);
// 休息10秒钟
//$this->release(10);
$url = 'http://www.ydma.cn';
$title = '测试邮件';
$to = $this->email;
// 邮件发送
$flag = Mail::send('email.test', ['name' => $this->name, 'url' => $url], function ($message) use ($to, $title) {
// 发送
$message->to($to)->subject('【亲爱的程序猿】' . $title);
echo date('Y-m-d H:i:s')."\n".$to.'的邮件已发送...';
if($flag){
\Log::info($this->name.'邮件发送成功');
}else{
\Log::info($this->name.'邮件发送失败');
* 处理一个失败的任务
* @return void
public function failed()
\Log::error($this->name.'队列任务执行失败'."\n".date('Y-m-d H:i:s'));