首页 > 解决方案 > Laravel Mailable - 试图获取非对象的属性“电子邮件”

问题描述

我有以下可邮寄的:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class UserLoginDetailsMail extends Mailable
{
    use Queueable, SerializesModels;

    protected String $username;
    protected String $email;
    protected String $password;

    /**
     * Create a new message instance.
     *
     * @param String $username
     * @param String $email
     * @param String $password
     */
    public function __construct(String $username, String $email, String $password)
    {
        $this->username = $username;
        $this->email = $email;
        $this->password = $password;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this
            ->from('test@test.com', env('TENANT_NAME'))
            ->replyTo(env('TENANT_EMAIL'))
            ->to($this->email)
            ->subject('Test')
            ->view('api.emails.user-login-details')
            ->with([
                'username' => $this->username,
                'password' => $this->password,
            ]);
    }
}

像这样被解雇:

Mail::queue(new UserLoginDetailsMail('test', "test@test.com", "test"));

但是,我排队的作业遇到以下错误:

ErrorException: Trying to get property 'email' of non-object in /vendor/laravel/framework/src/Illuminate/Mail/Mailable.php:612

/**
     * Set the recipients of the message.
     *
     * All recipients are stored internally as [['name' => ?, 'address' => ?]]
     *
     * @param  object|array|string  $address
     * @param  string|null  $name
     * @param  string  $property
     * @return $this
     */
    protected function setAddress($address, $name = null, $property = 'to')
    {
        foreach ($this->addressesToArray($address, $name) as $recipient) {
            $recipient = $this->normalizeRecipient($recipient);

            $this->{$property}[] = [
                'name' => $recipient->name ?? null,
                'address' => $recipient->email,
            ];
        }

        return $this;
    }

这个排队的邮件正在另一个排队的作业中被触发,不确定这是否会产生任何影响。

谢谢

标签: phplaravelredisqueue

解决方案


代码看起来不错。不过,您可能在没有重新启动队列的情况下对代码进行了更改。根据 Laravel 文档:

由于队列工作者是长期存在的进程,因此如果不重新启动,他们不会注意到代码的更改。因此,使用队列工作者部署应用程序的最简单方法是在部署过程中重新启动工作者。您可以通过发出 queue:restart 命令优雅地重新启动所有工作人员:

php artisan queue:restart

推荐阅读