首页 > 解决方案 > Laravel 5.6 中的计划任务(发送邮件)未执行

问题描述

我尝试每天向有未读项目的用户发送一封电子邮件。为此,使用 Laravel 5.6,我安排如下关闭:

use DB;
use Mail;
use App\Mail\UnreadLinks;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function() {
            $unreadLinks = DB::table('links')
                  ->leftJoin('users', 'users.id', '=', 'links.recipient_id')
                  ->select('users.email')
                  ->where('links.read_at', null)
                  ->groupBy('users.email')
                  ->get();

            foreach ($unreadLinks as $unreadLink) {
                Mail::to($unreadLink->email)->send(new UnreadLinks());
            }
        })->dailyAt('20:00');
    }
}

问题是我没有收到任何电子邮件。

我在这里想念什么吗?

标签: phplaravel

解决方案


我终于找到了原因:dailyAt('20:00')相对于服务器的时间,请求的时间是未来 2 小时。

为了让它完美地工作,我不得不将它设置dailyAt18:00


推荐阅读