php - cron job - sending email automatically on checking if expiry is close in laravel

I'm trying to send emails via laravel automatic when something gets expire.
Mail>EpiryAlert.php
class ExpireAlert extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
private $alerts;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($alerts)
{
$this->alerts = $alerts;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.alerts')
->with('alerts', $this->alerts);
}
}
Console>Commands>PassportExpire.php
class PassportExpire extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'expire:passport';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sends a Passport Expiry to a Admin via email';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$now = Carbon::now('Asia/Dubai')->toDateTimeString();
$passports = Passport::query()
->where(['send_alert' => 1])
->get();
$data = [];
foreach($passports as $key=>$passport){
$data[$key]['id'] = $passport->id;
$data[$key]['passport_no'] = $passport->passport_no;
$data[$key]['name'] = $passport->name;
$data[$key]['id_no_cnic'] = $passport->id_no_cnic;
$data[$key]['issue_date'] = $passport->issue_date;
$data[$key]['expiry_date'] = $passport->expiry_date;
$data[$key]['post_designation'] = $passport->post_designation;
$data[$key]['dob'] = $passport->dob;
$to = Carbon::createFromFormat('Y-m-d H:s:i', $now);
$from = Carbon::createFromFormat('Y-m-d H:s:i', $passport->expiry_date);
$data[$key]['now'] = $now;
if( Carbon::parse($passport->expiry_date) > Carbon::now() ){
$number_of_days = Carbon::parse(Carbon::now('Asia/Dubai'))->diffInDays($passport->expiry_date);
$data[$key]['days'] = $number_of_days;
$data[$key]['days_in_human'] = Carbon::parse($passport->expiry_date)->diffForHumans();
if( $number_of_days == 7){
$data[$key]['alert_for_7_days'] = 'Alert will be sent now 7 days';
}
if( $number_of_days == 15){
$data[$key]['alert_for_15_days'] = 'Alert will be sent now 15 days';
}
if( $number_of_days == 30){
$data[$key]['alert_for_30_days'] = 'Alert will be sent now 30 days';
}
}else{
$data[$key]['days'] = 0;
$data[$key]['days_in_human'] = Carbon::parse($passport->expiry_date)->diffForHumans();
}
}
$alert = [
'passport_no' => 'ex12312312',
'name' => 'test name',
'expiry_date' => '01-11-2020',
'id_no_cnic' => '12312-3123123-1'
];
$this->sendEmailToAdmin($alert);
}
private function sendEmailToAdmin($alerts){
$alerts = [
'passport_no' => 'ex12312312',
'name' => 'test name',
'expiry_date' => '01-11-2020',
'id_no_cnic' => '12312-3123123-1'
];
Mail::to('[email protected]')->send(new ExpireAlert($alerts));
DB::table('tbl_test')->insert(
['data_text' => 'test data']
);
print_r($alerts);
}
}
Console>Kernal.php
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
PassportExpire::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('expire:passport --force')->everyMinute();
}
}
when ever i'm running this command php artisan expire:passport i'm getting the response success but its not sending email with command but when i'm opening via route and its sending email here is route.
use App\Mail\ExpireAlert;
use Illuminate\Support\Facades\Mail;
Route::get('/mail', function () {
$alerts = [
'passport_no' => 'ex12312312',
'name' => 'test name',
'expiry_date' => '01-11-2020',
'id_no_cnic' => '12312-3123123-1'
];
Mail::to('[email protected]')->send(new ExpireAlert($alerts));
return new ExpireAlert($alerts);
});
can you please tell me how to solve the issue or what is the issue.
please check this command on server/hosting
/usr/local/bin/php /home2/junaidquyyum/public_html/fas/artisan expire:passport
this is my server path,
home2/junaidquyyum/public_html/fas
fas is a folder where my laravel app is
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function illuminate\encryption\openssl_cipher_iv_length()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.