【问题标题】:Laravel Mailable - Trying to get property 'email' of non-objectLaravel Mailable - 试图获取非对象的属性“电子邮件”
【发布时间】:2021-06-26 12:46:02
【问题描述】:

我有以下邮件:

<?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;
    }

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

谢谢

【问题讨论】:

  • /vendor/laravel/framework/src/Illuminate/Mail/Mailable.php 第 612 行在您的版本中是什么样的?当前版本将该行作为注释,没有帮助。
  • 也只是为了确认您的 ENV TENANT_NAME 和 TENANT_EMAIL 设置正确?
  • ENV 变量均设置正确。在 Laravel 版本 7 上运行
  • 好的,那么第612行有什么?
  • 我已更新我的问题以包含此内容。 'address' =&gt; $recipient-&gt;email,

标签: php laravel redis queue


【解决方案1】:

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

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

php artisan queue:restart

【讨论】:

  • 我已经重新启动了几次队列,并且在每次代码更改之后,问题仍然存在。
  • 还注意到我正在运行php artisan queue:listen 这在每次代码更改后都已重新启动。
【解决方案2】:

您是否缓存了您的配置?如果是这样,请使用php artisan config:clear 清理它并尝试再次发送邮件。

会有用的

【讨论】:

    猜你喜欢
    • 2015-06-12
    • 1970-01-01
    • 2015-09-22
    • 2017-03-20
    • 2014-03-25
    • 2015-01-25
    • 2014-06-23
    • 1970-01-01
    相关资源
    最近更新 更多