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('mymail@gmail.com')->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('mymail@gmail.com')->send(new EmailNotifier('12345678aZ*'));

But doesn't work in a Command.

Thanks for the help.

kiyah
  • 1,502
  • 2
  • 18
  • 27
  • 1
    What do you have in your`.env` file? That's where the mail driver would be configured. – Matthew Daly Jan 27 '18 at 13:34
  • yes, the .env file is configured and i can send mails in a controller with the same code: Mail::to('mymail@gmail.com')->send(new EmailNotifier('12345678aZ*')); But in a command didn't work. – Tesit Setter Jan 27 '18 at 14:26
  • We need more than that. What driver are you using? We don't need your username and password but it would be helpful to know if you are using SMTP, Mailgun or what have you. – Matthew Daly Jan 27 '18 at 14:28
  • i use smtp from the same shared hosting – Tesit Setter Jan 27 '18 at 15:10

0 Answers0