Reputation: 3746
i want send mail in queue and have not waiting when send mail https://laravel.com/docs/5.7/queues#connections-vs-queues
i run command to create table jobs
:
php artisan queue:table
php artisan migrate
I create a job to send mail: php artisan make:job SendEmailJob
and edit code :
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);
});
}
}
I call queue from controller:
use App\Jobs\SendEmailJob;
public function index()
{
$Body="test";
$email="[email protected]";
SendEmailJob::dispatch($Body, $email);
$calendars= AppointmentModel::GetAppointmentofDoctor($id,$datetime);
return view('frontend.appointment',["calendars"=>$calendars]);
}
add QUEUE_DRIVER=database
to file .env
run command:
php artisan queue:work
If i run controller, process still waiting send mail finish and run other process. i try change to:
SendEmailJob::dispatch($Body, $email)->delay(now()->addMinutes(3));
It not delay,it still send mail after 5s.
Why queue still waiting when send mail in laravel? I using win 32.
Upvotes: 0
Views: 1904
Reputation: 3746
My Problem fixed by change QUEUE_CONNECTION=sync
to QUEUE_CONNECTION=database
in .env
file
Upvotes: 1