简体   繁体   中英

Changing Laravel password reset mail to queued

I'm struggling with queues in Laravel as I never used them before. I'm overriding the default reset password email with help of the toMailUsing method and a dedicated service provider:

class MailServiceProvider extends ServiceProvider
{
    public function boot()
    {
        ResetPassword::toMailUsing(function ($notifiable, $token) {
            $url = url(route('password.reset', ['token' => $token, 'email' => $notifiable->getEmailForPasswordReset()]));
            dispatch(new SendEmail($url, $notifiable));
        });
    }
}

Here is my SendEmail job class:

class SendEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;
    protected $url;

    public function __construct($url, $user)
    {
        $this->user = $user;
        $this->url = $url;
    }

    
    public function handle()
    {
        $email = new ResetPassword($this->url, $this->user);
        Mail::to($this->user->email)->send($email);
    }
}

And the mailable itself:

class ResetPassword extends Mailable
{
    use Queueable, SerializesModels;
    protected $url;
    protected $user;


    public function __construct($url, $user)
    {
        $this->url = $url;
        $this->user = $user;
    }


    public function build()
    {
        return $this->markdown('emails.password_reset', ['url' => $this->url, 'user' => $this->user]);
    }
}

Where is the problem? I successfully queue the job and receive email, but get an error:

Trying to get property 'view' of non-object

Stack trace: https://flareapp.io/share/87nOGYM5#F59

Here is my previous, working code:

//Provider
ResetPassword::toMailUsing(function ($notifiable, $token) {
    $url = url(route('password.reset', ['token' => $token, 'email' => $notifiable->getEmailForPasswordReset()]));
    return new ResetPasswordMail($url, $notifiable);
});

//Mailable
class ResetPassword extends Mailable
{
    use Queueable, SerializesModels;
    protected $url;
    protected $user;


    public function __construct($url, $user)
    {
        $this->url = $url;
        $this->user = $user;
    }


    public function build()
    {
        $address = 'noreply@' . config('app.domain');
        $name = 'Lorem ipsum';
        $subject = config('app.name') . ' - Próba zresetowania hasła';

        $this->to($this->user)->subject($subject)->from($address, $name)->markdown('emails.password_reset', ['url' => $this->url, 'user' => $this->user]);
    }
}

I would really appreciate any help.

I'm struggling with queues in Laravel as I never used them before. I'm overriding the default reset password email with help of the toMailUsing method and a dedicated service provider:

class MailServiceProvider extends ServiceProvider
{
    public function boot()
    {
        ResetPassword::toMailUsing(function ($notifiable, $token) {
            $url = url(route('password.reset', ['token' => $token, 'email' => $notifiable->getEmailForPasswordReset()]));
            dispatch(new SendEmail($url, $notifiable));
        });
    }
}

Here is my SendEmail job class:

class SendEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;
    protected $url;

    public function __construct($url, $user)
    {
        $this->user = $user;
        $this->url = $url;
    }

    
    public function handle()
    {
        $email = new ResetPassword($this->url, $this->user);
        Mail::to($this->user->email)->send($email);
    }
}

And the mailable itself:

class ResetPassword extends Mailable
{
    use Queueable, SerializesModels;
    protected $url;
    protected $user;


    public function __construct($url, $user)
    {
        $this->url = $url;
        $this->user = $user;
    }


    public function build()
    {
        return $this->markdown('emails.password_reset', ['url' => $this->url, 'user' => $this->user]);
    }
}

Where is the problem? I successfully queue the job and receive email, but get an error:

Trying to get property 'view' of non-object

Stack trace: https://flareapp.io/share/87nOGYM5#F59

Here is my previous, working code:

//Provider
ResetPassword::toMailUsing(function ($notifiable, $token) {
    $url = url(route('password.reset', ['token' => $token, 'email' => $notifiable->getEmailForPasswordReset()]));
    return new ResetPasswordMail($url, $notifiable);
});

//Mailable
class ResetPassword extends Mailable
{
    use Queueable, SerializesModels;
    protected $url;
    protected $user;


    public function __construct($url, $user)
    {
        $this->url = $url;
        $this->user = $user;
    }


    public function build()
    {
        $address = 'noreply@' . config('app.domain');
        $name = 'Lorem ipsum';
        $subject = config('app.name') . ' - Próba zresetowania hasła';

        $this->to($this->user)->subject($subject)->from($address, $name)->markdown('emails.password_reset', ['url' => $this->url, 'user' => $this->user]);
    }
}

I would really appreciate any help.

I used below code for sending ResetPassword mail with queue in Laravel 8.x

create new ResetPasswordNotification class via php artisan make:notification ResetPasswordNotification and replace it's code with this:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Auth\Notifications\ResetPassword;

class ResetPasswordNotification extends ResetPassword implements ShouldQueue
{
    use Queueable;
}

And add method sendPasswordResetNotification method in your App\\Models\\User class:

<?php

...
use App\Notifications\ResetPasswordNotification;
...    

/**
 * Send the password reset notification.
 *
 * @param  string  $token
 * @return void
 */
public function sendPasswordResetNotification($token)
{
    $this->notify(new ResetPasswordNotification($token));
}

then run php artisan queue:work and test

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