I am using the ResetsPasswords trait by laravel to implement password reset. What I would like to achieve is to send the email using queue. Digging through the code I found the line below in function postEmail():
$response = Password::sendResetLink($request->only('email'), function (Message $message) {
$message->subject($this->getEmailSubject());
});
Digging further I notice that sendResetLink() function is implemented in a PasswordBroker class which in turn calls the function emailResetLink(). emailResetLink function returns the following:
return $this->mailer->send($view, compact('token', 'user'), function ($m) use ($user, $token, $callback) {
$m->to($user->getEmailForPasswordReset());
which I can simply change mailer->send
to mailer->queue
. Is they some better way to do it without modifying this non-project file?
In case you get "Call to a member function onQueue() on null" after trying to specify queue fakemeta's solution just specify the the queue you are targeting in the constructor of your class.
then use the notification facade to send your mail in the overriding method. The notify method also works
This is where the Laravel container comes to the rescue. If you don't like the functionality of a core component then you can go ahead and override it fairly painlessly.
First things first you will need to create your own PasswordBroker:
Change your namespace to whatever you want if you want to place it elsewhere in your app.
Since the service provider registering the service is a deferred service provider you will need to create your own provider to replace it. Probably the easiest way to do this is extend
Illuminate\Auth\Passwords\PasswordResetServiceProvider
with something like the following:Finally in your
config/app.php
file removeIlluminate\Auth\Passwords\PasswordResetServiceProvider::class
and addApp\Providers\PasswordResetServiceProvider::class
to your'providers'
array.Laravel will now use your implementation of the PasswordBroker rather than the stock framework one and you don't have to worry about modifying framework code.
I know this has been answered but I found another way to queue password reset notification which I found much simpler. I've tested it on Laravel 5.3.
By default, password reset notification is implemented by
Illuminate\Auth\Notifications\ResetPassword
class. This class is instantiated in yourUser
model insendPasswordResetNotification
method and passed tonotify
method ofIlluminate\Notifications\Notifiable
trait.So, to queue password reset notification you can simply create new
ResetPassword
notification class viaartisan make:notification ResetPassword
and replace it's code with this:And now just override
sendPasswordResetNotification
method in yourApp\User
class: