Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mozew's avatar
Level 6

How to send SMS 5 days before the date with Laravel?

After the Excel output, I want to send an SMS to the person's mobile number with the date entered in Excel 5 days before that date.

I also used the job command.

Controller.php

public function import(Request $request)
{
    Excel::import(new OtherImport, $request->file('file')->store('temp'));
    $insuranceData = Other::query()->select('personal_insurance', 'mobile')->get();

    $sentMobileNumbers = [];

    foreach ($insuranceData as $data) {
        $insuranceDate = $data->personal_insurance;
        $mobileNumber = $data->mobile;

        if (!empty($insuranceDate) && !in_array($mobileNumber, $sentMobileNumbers)) {
            try {
                $jalaliDate = Jalalian::fromFormat('Y/m/d', $insuranceDate);
                $carbonDate = Carbon::create($jalaliDate->getYear(), $jalaliDate->getMonth(), $jalaliDate->getDay());
                $smsDate = $carbonDate->subDay(5); 

                if ($smsDate->isPast()) {
                    SendSmsJob::dispatch([$mobileNumber]);
                    $sentMobileNumbers[] = $mobileNumber; 
                }
            } catch (\Exception $e) {
                dd($e->getMessage());
            }
        }
    }
    return back();
}
0 likes
1 reply
Snapey's avatar

usual way is to write a command that looks for records that need action in exactly 5 days time then using the scheduler run this command daily

1 like

Please or to participate in this conversation.