skip to Main Content

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


  1. Chosen as BEST ANSWER

    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

    
    class MailController extends Controller
    {
        public function delayedtask(){
            try {
                
                $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 SendMailQueue($details, $subject, $view));
                            Log::info('Email enviado com sucesso para '.$receiver_id->email);
                        }
                    }
                }
    
                return "Done!";
    
                } catch (Exception $e) {
                    return "Something went wrong!";
                }
        }
    }
    
    

    2-add a new route

    added a new route without Auth

    Route::get('/delayedtask',[MailController::class, 'delayedtask']);
    

    3-Added a cronjob on Cpanel

       curl -s "https://myWebsiteURL/delayedtask">/dev/null 2>&1
    

  2. First of all lets check all things:

    • Verify your mail configurations in your .env;
    • Verify in your email class if have implements ShouldQueue;
    • If you are implementing ShouldQueue, you must have to verify too your queue´s configuration in .env;
    • If is not implementing ShouldQueue, don´t miss time verifying queue´s config;

    All right all things validated and still not sending email:

    • Add the Send mail in try catch and log the catch if something went wrong;
    • If don´t log nothing in try catch, try to create an command that just send a simple email;
    • If dosen´t work try to send an email by your mail in Cpanel, because this should be the problem;

    Finally

    • In my cases using cPanel, I always create the croon task to all seconds like * * * * * 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!

    Login or Signup to reply.
  3. I had the same problem,
    i tried a new smtp email server

    MAIL_HOST=pro.eu.turbo-smtp.com
    
    MAIL_ENCRYPTION=ssl
    

    instead of

    MAIL_HOST=smtpauth.online.net
    
    MAIL_ENCRYPTION=tls
    

    I don’t know if it’s about the encryption or host features,
    but it worked for me

    Login or Signup to reply.
  4. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search