邮件队列发送功能异常



在我的应用程序中,我尝试使用 Mail::queue() 发送电子邮件。

我收到一个异常,说关闭序列化失败。

ErrorException in SerializableClosure.php 第 93 行:序列化 关闭失败:不允许序列化"关闭">

我有一个这个作为发送函数:

public function send()
{
    $view = view('emails.welcome');
    $data = [
        'user' => Auth::user()
    ];
    return $this->mailer->queue($view, $data, function($message){
        $message->to($this->to)->subject($this->subject);
    });
}

我最近才开始使用Laravel,所以任何帮助都会很棒。

您尝试使用的问题$this在关闭中。

请使用use关键字提供$to和$subject参数,如以下示例所示:

return $this->mailer->queue($view, $data, function($message) use ($to, $subject) {
    $message->to($to)->subject($subject);
});

问题是在闭包中使用$this

$this->to$this->subject 是对类上的字段的引用,而不是对闭包中的字段的引用,因此要修复代码,请使它们成为局部变量并将它们传递给闭包,如下所示:

public function send()
{
    $to = $this->getTo();
    $subject = $this->getSubject();
    return $this->mailer->queue( $this->getView(), $this->getData(), $this->getData(), 
    function($message) use($to, $subject) {
        $message->to($to)->subject($subject);
    });
}

最新更新