So I'm currently reading Laravel docs about mail queuing, and I've lost the idea, what's the difference between Mail::queue(<params>)
and $this->dispatch(new SendMail(<params>))
. Because in Mail section of Laravel docs the first variant is given, but then it is said "don't forget to configure your queues first". I went to Queue configuring section and there I found that "to add something to queue simply use $this->dispatch($job)
".
So, I've made both variants: one of them looks as follows:
Mail::queue('emails.template',
['name'=>$name, 'msg'=>$message],
function($msg) use ($email){
$msg->to($email)
->subject('Application received');
}
);
And the second, in the same controller, but other method, is just:
$this->dispatch(new SendEmail($name, $message, $email));
In second variant I use job, which handle()
method is the same as code before, just with Mail::send
.
And in both variants the user has to wait the same long time, php artisan queue:listen
remains silent, but the email is sent successfully.
What should I do to clearify my situation? Would highly appreciate any possible help!
Dispatching jobs to queue using the command bus gives you extra control; you can set the selected connection , queue , and delay from within your job class, decide if the command should be queued or run instantly, send the job through a pipeline before running it, actually you can even handle the whole queueing process ...
The Laravel queue service provides a unified API across a variety of different queue back-ends. Queues allow you to defer the processing of a time consuming task, such as sending an e-mail, until a later time which drastically speeds up web requests to your application.
Like many pieces of Laravel, there's often times a simple way of doing something, and then a way which gives you more control. The two methods of dispatching an email are slightly different. Let's take a look at both of them...
Mail::queue()
Illuminate\Mail\Mailer::queue()
$this->dispatch(new SendReminderEmail())
SendReminderEmail
job instance)dispatch
method is only available within the DispatchesJobs
trait.SendReminderEmail
will actually be doing the pushing of a new email send to the queue. Mail
facade already includes the job, hidden away from our implementation.It's a little unfortunate that the 5.2 documentation uses mailing as an example for the dispatching of an event, as most developers would just reach for Mail::queue()
. The confusion is warrented here.
As for speed of the queue, I'm unsure why your queue would be slower than expected, sorry!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With