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.
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.
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) {
//
});
You can dispatch the job with the delay method.
For exampe this job will be run in one day from now:
dispatch(YourJob::class)->delay(now()->addDay());
Doc ref about delayed job https://laravel.com/docs/8.x/queues#delayed-dispatching