我想在队列中发送邮件,并且在发送邮件时没有等待 https://laravel.com/docs/5.7/queues#connections-vs-queues
我运行命令来创建表jobs
:
php artisan queue:table
php artisan migrate
我创建了一个发送邮件的工作:php artisan make:job SendEmailJob
并编辑代码:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mail;
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public $body;
public $emailto;
public function __construct($body,$email)
{
//
$this->body=$body;
$this->emailto=$email;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$email=$this->emailto;
Mail::send("body_email.confirm_order",['Body'=> $this->body], function($message) use ($email)
{
$message->from(env('MAIL_USERNAME'),"Eye glasses");
$message->subject("Confirm Email");
$message->to($email);
});
}
}
我从控制器呼叫队列:
use App\Jobs\SendEmailJob;
public function index()
{
$Body="test";
$email="daitb@vnitsolutions.com";
SendEmailJob::dispatch($Body, $email);
$calendars= AppointmentModel::GetAppointmentofDoctor($id,$datetime);
return view('frontend.appointment',["calendars"=>$calendars]);
}
将QUEUE_DRIVER=database
添加到文件.env
运行命令:
php artisan queue:work
如果我运行控制器,则进程仍在等待发送邮件完成并运行其他进程。 我尝试更改为:
SendEmailJob::dispatch($Body, $email)->delay(now()->addMinutes(3));
没有延迟,仍然在5s之后发送邮件。
为什么在laravel中发送邮件时队列仍在等待? 我使用的是win 32。