Laravel: How to Queue mails to send later

Gabriel Matusevich picture Gabriel Matusevich · May 7, 2014 · Viewed 10.9k times · Source

Im trying to use the Mail::queue to send and email, but when I call this function it simple sends the mail, and the response is delayed ... I thought that the point of using Mail::queue was to queue ....

I want the response to came instantly, not having to wait for the email to be sent

for eg

Mail::queue('emails.template', $data, function($message) {
    $message->to('[email protected]');
    $message->subject('Notificacion');
});

return Response::json(array('error' => 0, 'message' => 'Ok'));

I want to receive the response without waiting for the mail to be sent. How can I do that???

Answer

Wogan picture Wogan · May 13, 2014

What queue driver (app/config/queue.php - 'default' param) are you using? If you're using sync, and haven't set up one of the others, then you're using the synchronous driver, which does exactly what the name says: Runs your queued task as soon as the task is created.

You need to configure an MQ server for Laravel to talk to. You can get a free iron.io account for this, and then you need to configure it, for instance:

'iron' => array(
    'driver'  => 'iron',
    'project' => 'iron-io-project-id',
    'token'   => 'iron-io-queue-token',
    'queue'   => 'queue-name',
),

Then when you use Mail::queue() it will push the instruction to iron.io. You'll then have to have another thread listening on the queue - just run php artisan queue:listen and leave it running while messages are pushed to the queue.