Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: what's the difference between Mail::queue and $this->dispatch?

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!

like image 393
Coffee Avatar asked Jul 12 '16 10:07

Coffee


People also ask

What is Dispatch in Laravel queue?

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 ...

What is the use of queue in Laravel?

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.


1 Answers

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()

  • Uses the facade accessor
  • Actual method lives at Illuminate\Mail\Mailer::queue()
  • Does the heavy lifting for you, by taking in the email template / view, any associated send data, and a callback function if we want to specify one
  • Can be called really anywhere, no extra configuration or new classes need creating
  • Will push the new email to be sent directly onto the queue for you.

$this->dispatch(new SendReminderEmail())

  • Manual dispatching of job (in this case, the SendReminderEmail job instance)
  • Needs to be used in a controller, as the associated dispatch method is only available within the DispatchesJobs trait.
  • This method dispatches the associated job. In this example, it is implied that SendReminderEmail will actually be doing the pushing of a new email send to the queue.
  • Used if you want increased control over the actual job dispatched. You can add any other functionality surrounding dispatching an email here. For instance, maybe there is custom logic to filter the content of an email before it's sent.
  • You will need to manually create the job, vs. the 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!

like image 115
1000Nettles Avatar answered Oct 12 '22 22:10

1000Nettles