Warm tip: This article is reproduced from serverfault.com, please click

Queue delay , sending an email at a certain time

发布于 2016-08-22 05:26:56

What want to achieve is to send and email , at a chosen time (by the user) so my approach was to calculate the time difference then make that the delay time on a queue.

but it seems it's not working as i expect all emails get delivered together and not at the right time

Controller

    public function notifyme($add)
{

    $created = Carbon::now()->addMinutes($add);
    $now = Carbon::now();

    $days = 1440*$created->diff($now)->days;
    $hours = 60*$created->diff($now)->h;
    $minutes = $days + $hours + $created->diff($now)->i;

    $user = Auth::user();


    $user->notify((new notifyme($user))->delay($minutes));

     return redirect('/notif');
}

route

Route::get('notifyme/{add}', 'HomeController@notifyme');

.env

QUEUE_DRIVER="database"

im using laravel 5.3 so the notifyme controller implements the ShouldQueue contract.

now when i run php artisan queue:work or php artisan queue:listen and test this the email sending is delayed, but when i do it again, (while the first job is still delayed) both emails get sent together immediately or after sometime (not at the right time $add)

Any idea what is wrong here? is there a better approach? like using a schedule? or what?

Questioner
Achraf Khouadja
Viewed
0
Aarti 2016-08-22 14:49:42

One of approach can be followed to achieve Queue delay for sending an email is to use Laravel inbuilt Mail function with queue facility. You can take reference from Laravel Delay Mail Queue

EG:

 Mail::later(5, 'emails.welcome', $data, function ($message) {
//
});

5 is delayed seconds.