7
votes

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!

1
1. Use mail::send(), if you want the mail to be sent by same worker. 2. Use mail::queue(), if you have a separate queue for mails and handled by separate worker.Mohammad Sharaf Ali

1 Answers

1
votes

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!