如何解决Laravel Scheduler Cron Job
我已经创建了一个名为SendAutoEmail的命令,并且我正在以此运行命令
PHP artisan send:autoemail
命令正常工作,但是没有发送电子邮件,我已经将直接电子邮件功能添加到命令中,并且还尝试将电子邮件功能添加到Controller方法中,并将该控制器调用到Command文件中,但是电子邮件没有发送,但是如果我正在尝试通过url成功地发送电子邮件来调用相同的方法,我不知道是什么问题,这是代码
命令> SendAutoEmail.PHP
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Http\Controllers\PassportController;
class SendAutoEmail extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:autoemail';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$controller = new PassportController();//your controller name
$controller->sendEMail();
dd($controller);
\Log::info('Cron is working fine!');
$this->info('Send:AutoEmail cron Command RUn Seccessfully');
return 0;
}
}
控制台> Kernel.PHP
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Console\Commands\SendAutoEmail;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
SendAutoEmail::class,];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('send:autoemail')
}
}
public function sendEMail(){
$alerts = [
'passport_no' => 'ex12312312','name' => 'test name','expiry_date' => '01-11-2020','id_no_cnic' => '12312-3123123-1'
];
$emailSent = Mail::to('test.email@gmail.com')->send(new ExpireAlert($alerts));
DB::table('tbl_test')->insert(
['data_text' => 'test data new']
);
dd($emailSent);
// here on dd($emailSent) i'm getting 0 in response
DB::table('tbl_test')->insert(
['data_text' => 'test data']
);
print_r($alerts);
}
解决方法
您运行调度程序了吗?
php artisan日程:运行
然后,您应该在数据库内的队列表中看到您的邮件。
现在,要执行队列:
php artisan queue:work
在生产中,建议使用软件包Supervisor。它的作用是确保工作程序始终在运行。
来源:https://laravel.com/docs/8.x/queues#running-the-queue-worker
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。