微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel Scheduler Cron Job

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?