Search code examples
laravel-5scheduling

How to send report in in scheduled time


In laravel 5.8 I have a report with a button “Send Email” by clicking on this button ajax request is run, with content of a report in “report_html” var to control like:

public function sentReportEmailContent()
{
    $request= request();
    $requestData= $request->all();

    $report_html= $requestData['report_html'];

    $loggedUser= Auth::user();

    $reportAvailableSpacesByZonesAcceptorsArray = config('app.reportAvailableSpacesByZonesAcceptorsArray', []);
    $site_name                                  = config('app.name', '');
    if ( count($reportAvailableSpacesByZonesAcceptorsArray) == 0 ) {
        return response()->json(['error_code' => 1, 'message' => 'There are no receiver emails specified !'], HTTP_RESPONSE_INTERNAL_SERVER_ERROR);
    }

    $to= $reportAvailableSpacesByZonesAcceptorsArray[0];
    $subject= 'Available Spaces By Zones report was sent at ' . $site_name;
    $additiveVars= [ 'html'=> $report_html ];

    unset($reportAvailableSpacesByZonesAcceptorsArray[0]);
    $cc= $reportAvailableSpacesByZonesAcceptorsArray;
    \Mail::to($to)->send( new SendgridMail( 'emailContainer', $to, $cc, $subject , $additiveVars ) );

    return response()->json(['error_code' => 0, 'message' => '', 'user'=> $loggedUser->id], HTTP_RESPONSE_OK);
}

and with Sendgrid service report is sent to users defined in config ok.

Now I need to run this report and send email to recievers in scheduler.

I created a new command :

php artisan make:command reportAvailableSpacesByZones  --command=report:available-spaces-by-zones

which has handle method:

public function handle()
{
    \Log::info( 'Report run # ' . time()  );
}

which is triggered in scheduled time. But how can I run my report and sent it's content like it is done manually ?

Modified block : My report is run by (local )url : http://local-boxbooking2.com/admin/report/available-spaces-by-zones I remade so that if to run url http://local-boxbooking2.com/admin/report/available-spaces-by-zones/send-email-on-open

in browser report is opened and checking “send-email-on-open” javascript function is triggered to sent by email (with Sendgrid service ) content of the page(report actually)

I tried to trigger command by cron tasks : In app/Console/Commands/reportAvailableSpacesByZones.php :

class reportAvailableSpacesByZones extends Command
{
    public function handle()
    {
        \Log::info( 'Report From inside app/Console/Commands/reportAvailableSpacesByZones.php run # ' . time()  );
        return redirect()->to('/admin/report/available-spaces-by-zones/send-email-on-open');
    }

I see log info , but no reports by email. Which way is correct ?

Thanks!


Solution

  • In app/Console/Kernal.php add the command to the protected commands array

     'App\Console\Commands\reportAvailableSpacesByZones',
    

    in the scheudle method add

      $schedule->command('cron:reportAvailableSpacesByZones')->weeklyOn(2, '20:30');
    

    other commands available

    https://laravel.com/docs/5.8/scheduling

    on the server crontab

    * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1