Running a Cron job at some time in future, but only one time (Laravel)

Viewed 253

I have to send a email using cron job in laravel, the flow is that the user will select the time for the email to be sent. ones done, that email would be sent at that time but only ones, we dont have to send it again and again.

2 Answers

I suggest using Laravels queues, more specifically the Mailer::later and the Mailer::laterOn method for that regard. You can delay the sending of the mail and it's only getting executed once.

YourMailable::later(now()->addMinutes(10))

or without a mailable:

Mail::later(now()->addMinutes(10), 'emails.view', $data, function ($message) {
    //
});

You can also specify a specifc queue within your Mailable:

use Illuminate\Mail\Mailable;

class YourMailable extends Mailable
{
    protected string $queue = 'mails-queue';

    // ...
}

With the Mail facade you can use the laterOn method:

Mail::laterOn('mails-queue', now()->addMinutes(10), 'emails.view', $data, function ($message) {
    //
});
Related