当我使用 laravel 队列时电子邮件发送成功,但在 cosnole 中它返回失败 laravel 队列

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

我正在使用 laravel 队列向多个用户发送邮件,但是当我运行命令时

laravel queue:work

但是运行后失败,但我在邮箱中收到电子邮件,所以,如何解决这个问题,如果有人有任何想法,请帮助我..
以下是我面临的问题,我已将图像更改为文本格式,现在您可以看到...

> php artisan queue:work 2024-12-18 04:54:20
App\Jobs\AfterAndBeforeBooking0bSendEmailJob 2024-12-18 04:54:24 RUNNING
App\Jobs\AfterAndBeforeBooking0bSendEmailJob 4,768.43ms  FAIL
<?php

namespace App\Jobs;

use Log;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Mail\AfterAndBeforeBookingObSendEmail;

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

    protected $multi_action_quotes;
    protected $follow_up_quotes;
    protected $future_credit;
    protected $unpaid_quotes;
    protected $partially_quotes;
    protected $update_pax_data;
    protected $mail_from;

    /**
     * Create a new job instance.
     */
    public function __construct($multi_action_quotes, $follow_up_quotes, $future_credit, $unpaid_quotes, $partially_quotes, $update_pax_data, $mail_from)
    {
        $this->multi_action_quotes = $multi_action_quotes;
        $this->follow_up_quotes    = $follow_up_quotes;
        $this->future_credit       = $future_credit;
        $this->unpaid_quotes       = $unpaid_quotes;
        $this->partially_quotes    = $partially_quotes;
        $this->update_pax_data     = $update_pax_data;
        $this->mail_from           = $mail_from;
    }

    /**
     * Execute the job to send email notifications based on the provided data.
     */
    public function handle()
    {
        logger()->info('My Log message before Exception');

        $emails = [];
        $names = [];
        $datasets = [
            $this->multi_action_quotes,
            $this->follow_up_quotes,
            $this->future_credit,
            $this->unpaid_quotes,
            $this->partially_quotes,
            $this->update_pax_data,
        ];

        if ($datasets) {
            // Collect all unique emails from the datasets
            foreach ($datasets as $dataset) {
                if (isset($dataset['data']) && $dataset['data'] !== null) {
                    foreach ($dataset['data'] as $item) {
                        if (!empty($item['created_user_email'])) {
                            $emails[] = $item['created_user_email'];
                        }
                        if (!empty($item['report_to_user_email'])) {
                            $emails[] = $item['report_to_user_email'];
                        }
                    }
                }
            }

            $emails = array_unique($emails);
            $users = User::whereIn('email', $emails)->get(['email', 'name']);

            foreach ($users as $user) {
                $names[$user->email] = $user->name;
            }

            // Send emails to each user with their relevant data
            foreach ($emails as $email) {
                $userName = $names[$email] ?? 'THO User';

                // Filter datasets for the current user
                $filteredData = [
                    'multi_action_quotes' => $this->filterDataByEmail($this->multi_action_quotes, $email),
                    'follow_up_quotes'    => $this->filterDataByEmail($this->follow_up_quotes, $email),
                    'future_credit'       => $this->filterDataByEmail($this->future_credit, $email),
                    'unpaid_quotes'       => $this->filterDataByEmail($this->unpaid_quotes, $email),
                    'partially_quotes'    => $this->filterDataByEmail($this->partially_quotes, $email),
                    'update_pax_data'     => $this->filterDataByEmail($this->update_pax_data, $email),
                ];

                // Skip email if no relevant data for this user
                if (array_filter($filteredData)) {
                    Mail::to($email)->send(new AfterAndBeforeBookingObSendEmail(
                        $filteredData['multi_action_quotes'],
                        $filteredData['follow_up_quotes'],
                        $filteredData['future_credit'],
                        $filteredData['unpaid_quotes'],
                        $filteredData['partially_quotes'],
                        $filteredData['update_pax_data'],
                        $this->mail_from,
                        $userName
                    ));
                }
            }
            throw new \Exception("Message is Logged before the Exception but the job is failed", 1);
        } else {
            Log::error('Error in AfterAndBeforeBookingObSendEmailJob: No datasets provided');
        }
        
    }

    /**
     * Filters a dataset by the given email address.
     *
     * @param array|null $dataset
     * @param string $email
     * @return array|null
     */
    private function filterDataByEmail($dataset, $email)
    {

        if (!isset($dataset['data']) || $dataset['data'] === null) {
            return null;
        }

        // Filter items where the user is either creator or assigned
        $filteredData = array_filter($dataset['data'], function ($item) use ($email) {
            return $item['created_user_email'] === $email || $item['report_to_user_email'] === $email;
        });
        return !empty($filteredData) ? ['data' => array_values($filteredData)] : null;
    }
}
php laravel email queue laravel-jobs
1个回答
0
投票

如果队列中有错误,如果您迁移了该表,您可以在“failed_jobs”表中检查它。

我认为您的队列连接存在问题。如果您正在使用数据库队列连接,如下所示的queue.php中。

“连接”=> [

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'retry_after' => 90,
    ]

]

在 .env "QUEUE_CONNECTION=database" 中在构造函数中添加以下行

$this->onConnection('数据库');

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