簡體   English   中英

郵件隊列發送功能異常

[英]Exception in mail queue send function

在我的應用程序中,我嘗試使用Mail::queue()發送電子郵件。

我收到一個例外,說閉包的序列化失敗。

SerializableClosure.php 93行中的ErrorException:關閉序列化失敗:不允許'Closure'的序列化

我有一個作為發送功能:

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,所以任何幫助都會很棒。

您試圖在Closure中使用$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是對類而不是在Closure中的字段的引用,因此要修復代碼,請使其成為局部變量並將其傳遞給閉包,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM