It’s my first time trying to implement Task Scheduling, I’m trying to send automatic E-mails at a certain time:
Before implementing my cron I first tested my email sending code manually in a normal class to see if there is no error, and there was no error, the email was sent successfully.
After that, I started implementing the Task Scheduling
Democron.php
protected $signature = 'demo:cron';
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$tasks = Task::all();
$date = Carbon::now()->toDateTimeString();
foreach ($tasks as $task) {
if($task->completed_at != null){
$validad = $task->completed_at;
$receiver_id = User::findOrFail($task->user_id);
if($date > $validad){
$details = [
'task_id' =>$task->id,
'receiver_id' => $receiver_id
];
$subject = 'TeamWork - Você tem tarefas em atraso!';
$view = 'emails.project.delaydtask';
Mail::to($receiver_id->email)->send(new SendMail($details, $subject, $view));
Log::info('Email enviado com sucesso para '.$receiver_id->email);
}
}
}
}
Kernel.php
protected $commands = [
DemoCron::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('demo:cron')
->twiceDaily(12, 15)
->timezone('Africa/Maputo');
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
I added to CRON JOBS on CPANEL
and set twiceDaily at 12 and 15
/usr/local/bin/php /.......myProjectPath/artisan schedule:run >> /dev/null 2>&1
I printed a LOG in my DemoCron.php to see if it really works
Result 1: when I select schedule once per minute it prints my LOG respecting all the conditions that are in my Democron.php , but it doesn’t send the email.
Result 2: When I select a certain time (Twice per day or once a day) my LOG does not print anything and it does not send the email.
What am I doing wrong? Help me please!
UPDATE
my SendMail class that i use to send emails manually works perfectly,
but the scheduled emails are not going
class SendMail extends Mailable
{
use Queueable, SerializesModels;
public $details, $subject, $view;
public function __construct($details, $subject, $view)
{
$this->details = $details;
$this->subject = $subject;
$this->view = $view;
}
public function build()
{
return $this->subject($this->subject)
->view($this->view, ['details' => $this->details]);
}
}
4
Answers
After trying several times I found a workaround.
1- create a new controller
I created a new controller called MailController instead of using the Kernel.php and Democron.php classes that I generated through Laravel Scheduling
2-add a new route
added a new route without Auth
3-Added a cronjob on Cpanel
First of all lets check all things:
implements ShouldQueue
;All right all things validated and still not sending email:
Finally
* * * * *
and in the kernel of my laravel project I verify if some command must be executed with the laravel commands like->twiceDaily(12, 15)
.Try all things and if the error still, please update this thread!
I had the same problem,
i tried a new smtp email server
instead of
I don’t know if it’s about the encryption or host features,
but it worked for me
For me it was my apache config file (php.ini)
I initially followed the recommended "disable_functions" configuration that ConfigServer said to do, and that’s what caused the issue for me. Thankfully I recently did this change and remembered, otherwise I would’ve never figured it out
Looks like adding proc_open in the disabled_functions causes this
Open up your php.ini and find "disable_functions", then delete/remove "proc_open" from that list
All I had to do was save the file and my schedule instantly worked, didn’t have to restart anything – but you may need to try to restart yours
Hope this helps anyone else who comes across this