[英]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 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]);
}
}
我真的很感激任何帮助。
我在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 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]);
}
}
我真的很感谢您的帮助。
我使用以下代码在 Laravel 8.x 中发送带有队列的 ResetPassword 邮件
通过php artisan make:notification ResetPasswordNotification
创建新的 ResetPasswordNotification 类并将其代码替换为:
<?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 类中添加方法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
and test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.