Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: How to Queue mails to send later

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

like image 394
Gabriel Matusevich Avatar asked May 07 '14 04:05

Gabriel Matusevich


1 Answers

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.

like image 139
Wogan Avatar answered Oct 15 '22 21:10

Wogan