我正在尝试使用laravel(5.5)命令在共享主机中发送电子邮件,这是我的命令:
<?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());
}
}
}
这是我在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');
}
}
运行 php artisan emailnotifier:notify 会返回消息:
“电子邮件发送成功!”
但我的电子邮件没有收到任何东西。
我把这一行放在控制器中并且有效:
Mail::to('mymail@gmail.com')->send(new EmailNotifier('12345678aZ*'));
但是在命令中不起作用。
感谢您的帮助。