File: /var/www/html/owlcrm/app/Console/Commands/SendEmails.php
<?php
namespace App\Console\Commands;
use App\Mail\ReminderEmail;
use App\Models\User;
use App\Models\Reminder; // Make sure to import the Reminder model
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:reminders';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send reminder emails to users based on reminder date and notification status';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->info('Reminder task run');
$currentDate = Carbon::now()->format('Y-m-d');
// Fetch reminders where notification is enabled and the date matches
$reminders = Reminder::where('is_notified', 0)
->whereDate('reminder_date', $currentDate)
->get();
if ($reminders->isNotEmpty()) {
foreach ($reminders as $reminder) {
$user = User::find($reminder->reminder_to);
if ($user) {
Mail::to($user->email)->send(new ReminderEmail($reminder));
$reminder->is_notified = 1;
$reminder->save();
$this->info('Reminder emails sent successfully to email: ' . $user->email);
} else {
$this->info('User not found');
}
}
} else {
$this->info('There is No reminder for today');
}
}
}