Laravel v.11 作业不发送电子邮件,但没有给出错误

问题描述 投票:0回答:1

我在 Laravel 11 中遇到了无法解决的问题:

我创建了一个命令来检索要通过电子邮件发送的数据,并且该命令可以正常工作:

class SendEvent5Reminder extends Command {
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send:send-event5-reminder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle() {
        // Get today's date
        $today = Carbon::today();

        // Calculate the date 5 days from today
        $backDate = $today->copy()->subDays(5);

        // Find all events expiring within 5 days
        $events = Event::whereBetween('start', [$backDate->format('Y-m-d'), $today->format('Y-m-d')])->get();


        // Check if there are any events found
        if ($events->isEmpty()) {
            Log::info('No events expiring within 5 days.');
            return 0;
        }

        // Get the administrator user
        $adminUser = User::get();

        if (!$adminUser) {
            Log::info('No administrator user found.');
            return 1;
        }

        // Dispatch the job to send reminder emails
        foreach ($events as $sendEvent) {
            RemindEmail5Job::dispatch($adminUser, $sendEvent);
        }

        Log::info('Reminder emails sent successfully.');

        return 0;
    }

}

然后我创建作业:

class RemindEmail5Job implements ShouldQueue {
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $adminUser;
    protected $sendEvent;

    /**
     * Create a new job instance.
     */
    public function __construct($adminUser, Event $sendEvent) {
        $this->adminUser = $adminUser;
        $this->sendEvent = $sendEvent;
    }

    /**
     * Execute the job.
     */
    public function handle(): void {
                Log::info('TEST: Start Job email!');
        $email = new Remind5Event($this->sendEvent);
        Mail::to($this->adminUser->first()->email)->send($email);
>event));
    }
}

但是handle()方法不起作用,而且似乎根本没有考虑到这个方法,实际上放在那里用于测试的日志没有写入。

我就是不知道我哪里错了。

通过 Tinker 发送测试电子邮件工作正常,但经过多次测试后,该工作不起作用。 为什么?这一切看起来都是正确的,但它没有通过handle()方法,我只是不明白。

laravel email jobs
1个回答
0
投票

在我用头撞墙并尝试了很多东西几天后,突然出现了解决方案,而且很简单:不要使用 job,因为 job 中的 handle() 方法(以这种方式使用)确实不起作用(这是一个错误?...我不知道)。

而是直接从 artisan 命令handle() 方法调用电子邮件类来发送电子邮件,如下所示(在本例中位于 foreach 内)

namespace App\Console\Commands;

use App\Models\Event;
use App\Models\User;
use App\Mail\RemindEvent;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

class SendEvent5Reminder extends Command {
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send:send-event5-reminder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle() {
        // Get today's date
        $today = Carbon::today();

        // Calculate the date 5 days from today
        $backDate = $today->copy()->subDays(5);

        // Find all events expiring within 5 days
        $events = Event::whereBetween('start', [$backDate->format('Y-m-d'), $today->format('Y-m-d')])->get();


        // Check if there are any events found
        if ($events->isEmpty()) {
            Log::info('No events expiring within 5 days.');
            return 0;
        }

        // Get the administrator user
        $adminUser = User::get();

        if (!$adminUser) {
            Log::info('No administrator user found.');
            return 1;
        }

        // Dispatch the job to send reminder emails
        foreach ($events as $sendEvent) {
            $email = new RemindEvent($sendEvent);
            Mail::to($adminUser->first()->email)->send($email);
        }

        Log::info('Reminder emails sent successfully.');

        return 0;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.