简体   繁体   中英

Laravel Mail queue Password Reset

I'am trying to queue the password reset mail on laravel.

I have tried cloning the PasswordBroker as follow:

<?php
namespace App;

use Closure;
use Illuminate\Auth\Passwords\PasswordBroker as IlluminatePasswordBroker;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class PasswordBroker extends IlluminatePasswordBroker
{

    public function emailResetLink(CanResetPasswordContract $user, $token, Closure $callback = null)
    {
    // We will use the reminder view that was given to the broker to display the
    // password reminder e-mail. We'll pass a "token" variable into the views
    // so that it may be displayed for an user to click for password reset.
        $view = $this->emailView;

        return $this->mailer->queue($view, compact('token', 'user'), function ($m) use ($user, $token, $callback) {
            $m->to($user->getEmailForPasswordReset());

            if (! is_null($callback)) {
                call_user_func($callback, $m, $user, $token);
            }
        });
    }

}

Then I get this error:

exception 'ErrorException' with message 'Serialization of closure failed: Serialization of closure failed: Serialization of 'Closure' is not allowed'

If I change mailer->queue to mailer->send it works fine. I can't figure ou what is happening.

When you put something into the Queue, Laravel serializes it into plain text, and stores it into the database - it can't do this with objects.

Instead: create a Job that captures all the info you need to send the email (user, token, etc), and then when the job is called, call the mail function as usual.

You need to declare the user email id to a variable and then pass it to the mail function

public function emailResetLink(CanResetPasswordContract $user, $token, Closure $callback = null)
    {
        $view = $this->emailView;
        $to = $user->getEmailForPasswordReset();


        return $this->mailer->queue($view, compact('token', 'user'), function ($m) use ($user,$to, $token, $callback) {
            $m->to($to);

            if (! is_null($callback)) {
                call_user_func($callback, $m, $user, $token);
            }
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM