title: cron job - sending email automatically on checking if expiry is close in laravel
body: <p>I'm trying to send emails via laravel automatic when something gets expire.</p>
  <p>Mail&gt;EpiryAlert.php</p>
  <pre><code>class ExpireAlert extends Mailable implements ShouldQueue
  {
      use Queueable, SerializesModels;
  
      private $alerts;
  
      /**
       * Create a new message instance.
       *
       * @return void
       */
      public function __construct($alerts)
      {
          $this-&gt;alerts = $alerts;
      }
  
      /**
       * Build the message.
       *
       * @return $this
       */
      public function build()
      {
          return $this-&gt;markdown('emails.alerts')
          -&gt;with('alerts', $this-&gt;alerts);
      }
  }
  </code></pre>
  <p>Console&gt;Commands&gt;PassportExpire.php</p>
  <pre><code>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')-&gt;toDateTimeString();
          $passports = Passport::query()
              -&gt;where(['send_alert' =&gt; 1])
              -&gt;get();
          $data = [];
          foreach($passports as $key=&gt;$passport){
              $data[$key]['id'] = $passport-&gt;id;
              $data[$key]['passport_no'] = $passport-&gt;passport_no;
              $data[$key]['name'] = $passport-&gt;name;
              $data[$key]['id_no_cnic'] = $passport-&gt;id_no_cnic;
              $data[$key]['issue_date'] = $passport-&gt;issue_date;
              $data[$key]['expiry_date'] = $passport-&gt;expiry_date;
              $data[$key]['post_designation'] = $passport-&gt;post_designation;
              $data[$key]['dob'] = $passport-&gt;dob;
  
              $to = Carbon::createFromFormat('Y-m-d H:s:i', $now);
              $from =   Carbon::createFromFormat('Y-m-d H:s:i', $passport-&gt;expiry_date);
              $data[$key]['now']   =   $now;
              
              if( Carbon::parse($passport-&gt;expiry_date) &gt; Carbon::now() ){
                  $number_of_days = Carbon::parse(Carbon::now('Asia/Dubai'))-&gt;diffInDays($passport-&gt;expiry_date);
                  $data[$key]['days']   =    $number_of_days;
                  $data[$key]['days_in_human']   =    Carbon::parse($passport-&gt;expiry_date)-&gt;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-&gt;expiry_date)-&gt;diffForHumans();
              }
          }
          $alert = [
              'passport_no'   =&gt;  'ex12312312',
              'name'  =&gt;  'test name',
              'expiry_date'   =&gt;  '01-11-2020',
              'id_no_cnic'    =&gt;  '12312-3123123-1'
          ];
          $this-&gt;sendEmailToAdmin($alert);
      }
  
      private function sendEmailToAdmin($alerts){
          $alerts = [
              'passport_no'   =&gt;  'ex12312312',
              'name'  =&gt;  'test name',
              'expiry_date'   =&gt;  '01-11-2020',
              'id_no_cnic'    =&gt;  '12312-3123123-1'
          ];
          Mail::to('salman.promate@gmail.com')-&gt;send(new ExpireAlert($alerts));
          DB::table('tbl_test')-&gt;insert(
              ['data_text' =&gt; 'test data']
          );
          print_r($alerts);
      }
  }
  </code></pre>
  <p>Console&gt;Kernal.php</p>
  <pre><code>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-&gt;command('expire:passport --force')-&gt;everyMinute();
      }
  }
  </code></pre>
  <p>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.</p>
  <pre><code>use App\Mail\ExpireAlert;
  use Illuminate\Support\Facades\Mail;
  Route::get('/mail', function () {
      $alerts = [
          'passport_no'   =&gt;  'ex12312312',
          'name'  =&gt;  'test name',
          'expiry_date'   =&gt;  '01-11-2020',
          'id_no_cnic'    =&gt;  '12312-3123123-1'
      ];
      Mail::to('salman.promate@gmail.com')-&gt;send(new ExpireAlert($alerts));
      return new ExpireAlert($alerts);
  });
  </code></pre>
  <p>can you please tell me how to solve the issue or what is the issue.</p>
  <p>please check this command on server/hosting</p>
  <blockquote>
  <p>/usr/local/bin/php /home2/junaidquyyum/public_html/fas/artisan
  expire:passport</p>
  </blockquote>
  <p>this is my server path,</p>
  <blockquote>
  <p>home2/junaidquyyum/public_html/fas</p>
  </blockquote>
  <p>fas is a folder where my laravel app is</p>