0

I'm trying to send email with laravel (5.5) commands in shared hosting, this is my command:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use App\Mail\EmailNotifier;

class EmailNotifier extends Command {
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'emailnotifier:notify';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send email';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() {     
        try {
            Mail::to('[email protected]')->send(new EmailNotifier('12345678aZ*'));    
            $this->info("The emails are send successfully!");  
        }

        catch(\Exception $ex){
            $this->info($ex->getMessage());
        }
    }
}

And this is my code in kernel.php:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel {
    //schedule methods 

    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\EmailNotifier::class,
    ];



    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule) {  
        $schedule->command('emailnotifier:notify')->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands() {
        $this->load(__DIR__.'/Commands');
        require base_path('routes/console.php');
    }
}

Running php artisan emailnotifier:notify returns the message: "The emails are send successfully!" But my email didn't receive anything. I put this line in a Controller and works: Mail::to('[email protected]')->send(new EmailNotifier('12345678aZ*'));

But doesn't work in a Command.

Thanks for the help.

kiyah
  • 1,502
  • 2
  • 18
  • 27

0 Answers0