'Laravel Schedule not sending email

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]);
    }
}


Solution 1:[1]

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!

Solution 2:[2]

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

Solution 3:[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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samuel Lujan
Solution 2 valdimiro einstein
Solution 3