【问题标题】:Application fails to send email with Mail::...queue()应用程序无法使用 Mail::...queue() 发送电子邮件
【发布时间】:2020-11-26 23:28:20
【问题描述】:

我在使用queue 时遇到了打开邮件的问题。如果我使用带有send() 的邮件,一切正常。

控制器:

Mail::to($order_data->client_email)
    ->cc([
        ['email' => $order_data->seller->email],
        ['email' => auth()->user()->email]
    ])
    ->queue(new SendOrderConfirmation($order_data));

可邮寄:

class SendOrderConfirmation extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Defines a public variable $order_data that we will be using to pass in parameters from our controller.
     */
    public $order_data;

    /**
     * Create a new message instance.
     */
    public function __construct($data)
    {
        // set email data
        $this->order_data = $data;

        // Set Reply to address
        // Basically, the name and email from who's sending this email
        $this->replyto(auth()->user()->email, auth()->user()->name);

        // Set from
        $this->from(auth()->user()->email, auth()->user()->name);

        // set email subject
        $this->subject('Laminar - Confirmação da Encomenda N.º '.$this->order_data->order_nr);
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('send_emails.Lamimail.SendOrderConfirmation');
    }
}

如果我使用 queue() 触发电子邮件,我会收到有关望远镜作业的错误消息:

Trying to get property 'name' of non-object 
(View: path\resources\views\send_emails\Lamimail\SendOrderConfirmation.blade.php)

但是,在邮件视图中,名称是一个简单的auth()->user()->name

有人知道我在队列中失踪了吗?

问候

解决方案,基于@Ersoy 的反馈。注意public $sender_name; 可邮寄:

{
    use Queueable, SerializesModels;

    /**
     * Defines a public variable $order_data that we will be using to pass in parameters from our controller.
     */
    public $order_data;

    /**
     * Will be used to save the sender name
     */
    public $sender_name;

    /**
     * Create a new message instance.
     */
    public function __construct($data)
    {
        // set email data
        $this->order_data = $data;

        // Set sender name to be used on mail view
        $this->sender_name = auth()->user()->name;

        // Set Reply to address
        // Basically, the name and email from who's sending this email
        $this->replyto(auth()->user()->email, auth()->user()->name);

        // Set From
        $this->from(auth()->user()->email, auth()->user()->name);

        // set email subject
        $this->subject('subject...');
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('view...');
    }
}

【问题讨论】:

    标签: php laravel email queue


    【解决方案1】:

    当作业或电子邮件为queued 时,您将无法再访问会话(会话中的变量),因为在“异步”处理作业时没有 HTTP 访问。因此,您无法获得经过身份验证的用户(处理作业时没有特定用户)。

    您需要将认证的用户(作为对象或数组)发送给SendOrderConfirmation类的构造函数,然后才能在类内部使用。

    非队列将起作用,因为它将是 sync 并且仍然可以使用会话。

    【讨论】:

    • 很好解释,已经修复,测试和工作。将更新问题以反映更改。谢谢!
    【解决方案2】:

    您是否尝试过将名称作为数据添加到您的$order_data 并在邮件刀片中显示以查看是否可以解决问题。

    【讨论】:

    • 没试过这样。但是写了 public $sender; 并在构造函数中写了 $this->sender = auth()->user()->name 并没有运气。怀疑该队列不适用于身份验证。
    • 是的,通过将其添加到 $order_data 来尝试这种方式,因为您似乎无法在视图中访问它。因此,通过添加它,您可以简单地在视图中调用它。
    猜你喜欢
    • 2014-10-03
    • 2011-04-21
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    相关资源
    最近更新 更多