`Hello I’m working on cron in laravel projectin this i want to send some details from database to perticular user every day morning 10AM but when i run command php artisan daily-stats-email-notification:cron it not send any mail
Below I provide you some sample code please help me in this
Below is my Cron file —
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
use AppModelsDeptProjectMasterDao;
use AppModelsProjectContactMasterDao;
use AppModelsBroadcastDao;
use AppModelsCampaignMasterDao;
use AppServicesCommonFunction;
use AppModelsConfigurationDao;
use AppModelsCronServicesDao;
class DailyStatsEmailNotificationCron extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'daily-stats-email-notification:cron';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$obj = new BroadcastDao();
$pobj = new ProjectContactMasterDao();
$campignobj = new CampaignMasterDao();
$deptObj = new DeptProjectMasterDao();
$cobj = new ConfigurationDao();
$cronObj = new CronServicesDao();
$config = $cobj->loadConfig();
$emails = [];
$end = date("Y-m-d");
$start = date('Y-m-d');
$records = $obj->getRows(["((TO_CHAR(entrytime,'YYYY-MM-DD') BETWEEN '".$start."' AND '".$end."') OR (TO_CHAR(schedule_datetime,'YYYY-MM-DD') BETWEEN '".$start."' AND '".$end."'))"]);
// Checkpoint: Log the number of records fetched
Log::info('Number of records fetched: ' . count($records));
if(is_object($records) && is_countable($records) && count($records) > 0){
foreach($records as $record){
$campaignRec = $campignobj->getRows(["id='".$record->campaign_parent_id."'"]);
// Log campaign records
Log::info('Campaign records: ' . json_encode($campaignRec));
if(is_object($campaignRec) && is_countable($campaignRec) && count($campaignRec) > 0){
foreach($campaignRec as $campaignRecord){
if(property_exists($record,'sent')){
$emails[$campaignRecord->dept_id][$campaignRecord->dept_prj_id]['Sent'][] = $record->sent;
}
if(property_exists($record,'delivered')){
$emails[$campaignRecord->dept_id][$campaignRecord->dept_prj_id]['Delivered'][] = $record->delivered;
}
if(property_exists($record,'dbtick')){
$emails[$campaignRecord->dept_id][$campaignRecord->dept_prj_id]['Read'][] = $record->dbtick;
}
if(property_exists($record,'failed')){
$emails[$campaignRecord->dept_id][$campaignRecord->dept_prj_id]['Failed'][] = $record->failed;
}
}
}
}
// Checkpoint: Log the content of the emails array
Log::info('Contents of the emails array: ' . json_encode($emails));
if(is_array($emails) && is_countable($emails) && count($emails) > 0){
$cronObj->addEditRecord(['service_name' => "'".$this->signature."'", 'status' => 1, 'running_time' => "'".date("Y-m-d H:i:s")."'"],0);
$cronInsertedId = $cronObj->insertedid;
foreach($emails as $deptid => $newArray){
if(is_array($newArray) && is_countable($newArray) && count($newArray) > 0){
foreach($newArray as $projectid => $new_Array){
$projectObject = $pobj->getRows(["projectid='".$projectid."'"]);
// Checkpoint: Log the number of project records fetched
Log::info('Number of project records fetched: ' . count($projectObject));
$projectTitle = CommonFunction::getRecordById("AppModelsDeptProjectMasterDao",$projectid);
$deptObject = $deptObj->getRecordById($deptid);
if(is_object($projectObject) && is_countable($projectObject) && count($projectObject) > 0){
foreach($projectObject as $project_Object){
if($project_Object->email != ''){
// $to = $project_Object->email;
$to = "[email protected]";
Log::info("Email address: $to");
$title = $config['COMPANY_NAME'];
$subject = "Today's stats summary report";
$message = "<p>Hello ".$project_Object->name.",</p>";
$message .= "<p>Today's stats summary report - (".date('d-m-Y').")</p>";
$message .= "<p>Department: ".$deptObject->department_title."</p>";
$message .= "<p>Project: ".$projectTitle."</p>";
// Checkpoint: Log the email message
Log::info('Email message: ' . $message);
// Checkpoint: Log email sending status
Log::info('Attempting to send email...');
if(array_key_exists('Sent',$new_Array)){
$message .= "<p>Total Sent: ".array_sum($new_Array['Sent'])."</p>";
}
if(array_key_exists('Delivered',$new_Array)){
$message .= "<p>Total Delivered: ".array_sum($new_Array['Delivered'])."</p>";
}
if(array_key_exists('Read',$new_Array)){
$message .= "<p>Total Read: ".array_sum($new_Array['Read'])."</p>";
}
if(array_key_exists('Failed',$new_Array)){
$message .= "<p>Total Failed: ".array_sum($new_Array['Failed'])."</p>";
}
$setup = array('title' => $title);
$response = CommonFunction::sendEmail($to,$subject,$message,$setup);
if($response){
$cronObj->addEditRecord(['status' => 2, 'completed_time' => "'".date("Y-m-d H:i:s")."'"],$cronInsertedId);
Log::info("Today's stats summary report email has been sent to ".$project_Object->email);
}
else{
Log::info("Today's stats summary report email has not been sent to ".$project_Object->email);
}
}
}
}
}
}
}
}
}
}
}
in kernal.php–
$schedule->command(‘daily-stats-email-notification:cron’)->dailyAt(’23:45′);
in my cron file when i run command i get data from database Log::info(‘Campaign records: ‘ . json_encode($campaignRec)); this log give me record but when i come to Log::info(‘Contents of the emails array: ‘ . json_encode($emails)); this log it empty and below all logs are empty i don’n know what is wrong in it `