将 Laravel 密码重设邮件更改为排队

Changing Laravel password reset mail to queued

我在 Laravel 中遇到队列问题,因为我以前从未使用过它们。我正在 toMailUsing 方法和专门的服务提供商的帮助下覆盖默认的重置密码电子邮件:

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

这是我的 SendEmail 工作 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);
    }
}

以及邮件本身:

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

问题出在哪里?我已成功排队作业并收到电子邮件,但收到错误消息:

Trying to get property 'view' of non-object

堆栈跟踪:https://flareapp.io/share/87nOGYM5#F59

这是我以前的工作代码:

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

非常感谢任何帮助。

这看起来很复杂,所以我不确定你的问题出在哪里。但如果有帮助,我会按照以下步骤发送排队的自定义密码重设电子邮件。

  1. Illuminate\Auth\Passwords\CanResetPassword::sendPasswordResetNotification()User模型继承;覆盖它以使用您的自定义通知。

    <?php
    
    namespace App;
    
    use App\Notifications\UserPasswordReset;
    
    class User extends Authenticatable
    {
        public function sendPasswordResetNotification(string $token): void
        {
            $url = route('password.reset', [
                'token' => $token,
                'email' => $this->getEmailForPasswordReset()
            ]);
            $this->notify(new UserPasswordReset($url));
        }
    }
    
  2. 构建自定义通知并确保它是可排队的

    <?php
    
    namespace App\Notifications;
    
    use App\Mail\UserPasswordReset as UserPasswordResetMailable;
    use App\User;
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    
    class UserPasswordReset extends Notification implements ShouldQueue
    {
        use Queueable;
    
        protected $url;
    
        public function __construct(string $url)
        {
            $this->url = $url;
        }
    
        public function via(User $notifiable)
        {
            return ["mail"];
        }
    
        public function toMail(User $notifiable)
        {
            return new UserPasswordResetMailable($this->url, $notifiable);
        }
    }
    
  3. 您现有的可邮寄邮件应该可以正常工作,但这是我的样子:

    <?php
    
    namespace App\Mail;
    
    use App\User;
    use Illuminate\Bus\Queueable;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;
    
    class UserPasswordReset extends Mailable
    {
        use Queueable;
        use SerializesModels;
    
        public $user;
        public $url;
    
        public function __construct(string $url, User $user)
        {
            $this->user = $user;
            $this->url = $url;
        }
    
        public function build()
        {
            return $this->
                ->with(["message" => $this])
                ->to($this->user->email)
                ->from(config("mail.from.address"))
                ->subject(__("Password Reset"))
                ->text("email.reset_plaintext")
                ->view("email.reset_html");
        }
    }
    

不需要服务提供商,不需要工作class。

我使用下面的代码在 Laravel 8.x

中发送带有队列的 ResetPassword 邮件

通过 php artisan make:notification ResetPasswordNotification 创建新的 ResetPasswordNotification class 并将其代码替换为:

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

并在你的 App\Models\User class 中添加方法 sendPasswordResetNotification 方法:

<?php

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

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

然后 运行 php artisan queue:work 和测试