Reputation: 79
I'm trying to run a schedule task on cpanel.
So I'm using the following code :
Command : notify.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Mail\NotifyContratExpire;
use Mail;
use Carbon\Carbon;
use App\Models\Projet_Casting;
use App\Models\Filiale;
class notify extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'emails:send';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send Emails notify to all users for contrats expirés';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$today_date = Carbon::now()->addDays(60);
$contrat_email = Projet_Casting::join('contrats','contrats.id_contrat','=','projets_castings.id_contrat')->leftjoin('projets','projets.id_projet','=','projets_castings.id_projet')->join('castings','castings.id_casting','=','contrats.id_casting')->join('filiales','projets.id_filiale','=','filiales.id_filiale')->where('contrats.actif',1)->where('contrats.date_fin_contrat', '<=', $today_date)->distinct('filiales.id_filiale')->get();
$emails = array();
foreach($contrat_email as $key) {
$emails[] = json_decode($key->adm_email);
}
$email_send = array();
foreach ($emails as $key3 => $value) {
$filiale =Filiale::where('adm_email', json_encode($value))->select('filiales.id_filiale')->value('filiales.id_filiale');
$contras = Projet_Casting::join('contrats','contrats.id_contrat','=','projets_castings.id_contrat')->leftjoin('projets','projets.id_projet','=','projets_castings.id_projet')->join('castings','castings.id_casting','=','contrats.id_casting')->where('contrats.actif',1)->where('contrats.date_fin_contrat', '<=', $today_date)->where('projets.id_filiale',$filiale)->get();
foreach($value as $val =>$v){
Mail::to($v)->send(new NotifyContratExpire($contras));
}
}
}
}
MailController : NotifyContratExpire
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NotifyContratExpire extends Mailable
{
use Queueable, SerializesModels;
public $contras;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($contras)
{
$this->contras = $contras;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject("contrats")->view('Template_Mails.template_email')->with('contras',$this->contras);
}
}
And kernel.php :
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Mail;
use Illuminate\Console\Command;
use App\Mail\NotifyContratExpire;
use Carbon\Carbon;
use App\Models\Projet_Casting;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
/*'\App\Console\Commands\notify'*/
Commands\notify::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command('emails:send')->everyMinute()->withoutOverlapping(60);
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
And the email view :
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class="container-fluid">
<div class="row">
<table class="data-table data-table-standard responsive nowrap"
data-order="[[ 1, "desc" ]]">
<thead>
<tr>
<th>numero_contrat</th>
<th>numero_projet</th>
<th>casting</th>
<th>casting</th>
</tr>
</thead>
<tbody>
@foreach($contras as $contra)
<tr>
<td>
<p class="list-item-heading">{{$contra->numero_contrat}}</p>
</td>
<td>
<p class="text-muted">{{$contra ->numero_projet}}</p>
</td>
<td>
<p class="text-muted">{{$contra->nom}}</p>
</td>
<td>
<p class="text-muted">{{$contra->prenom}}</p>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</body>
</html>
On cpanel I have the following structure :
And I created a cron JOV for everyMinute with the following command :
/usr/local/bin/php /home/current_user/laravel/artisan schedule:run
the mails are not sent and I do not know where the error is or how to display the errors to know how to solve this problem, when I run the execution in localhost, it works very well and the emails are sent.
If you have any idea , please help
Upvotes: 1
Views: 121
Reputation: 118
You can try this
/path/to/php /path-to-your-project/artisan schedule:run >>/dev/null 2>&1
Upvotes: 2