在 laravel 8 中将 mail::send 更改为 mail::queue 时出现未定义的 属性 错误异常

Getting undefined property error exception when changing mail::send to mail::queue in laravel 8

这是有效的代码:

Mail::to($emails)->send(new ExceptionOccurred($e));

然后我将其更改为:

Mail::to($emails)->queue(new ExceptionOccurred($e));

当我这样做时出现错误:

ErrorException: Undefined property: App\Mail\ExceptionOccurred::$content in C:\inetpub\wwwroot\laravel\app\Mail\ExceptionOccurred.php:33

这是ExceptionOccurred.php:

namespace App\Mail;

use Illuminate\Bus\Queueable;
// use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ExceptionOccurred extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($content)
    {
        $this->content = $content;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('mail.ExceptionOccurred')
            ->subject('Exception on live instance')
            ->with('content', $this->content);
    }
}

这是异常处理程序的相关部分:

if ( $exception instanceof Exception ) {
    $e = FlattenException::create($exception);
} else {
    $e = $exception;
}

$emails = json_decode( env('MAINTAINER_EMAILS') );

if (app()->environment('production') || app()->environment('testing') ) {
    Mail::to($emails)->send(new ExceptionOccurred($e));
}

重申一下,Mail::send() 有效,但 Mail::queue() 无效。我相信队列设置正确。

您必须在构造之前定义内容属性,如下所示:

namespace App\Mail;

use Illuminate\Bus\Queueable;
// use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ExceptionOccurred extends Mailable
{
    use Queueable, SerializesModels;
 
    public $content;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($content)
    {
        $this->content = $content;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('mail.ExceptionOccurred')
            ->subject('Exception on live instance')
            ->with('content', $this->content);
    }
}

参考:https://laravel.com/docs/8.x/mail#view-data