How do I manually send a password reset request in Laravel 5.2?
Asked Answered
Z

6

21

I would like to manually send a password reset request to a specific user (not the one currently logged in) from within a controller. I did some digging around in the Laravel code and it seems like I should be calling postEmail(Request $request) in ResetsPasswords, but I can't seem to figure out how to get access to the right PasswordController instance to call it.

Zinfandel answered 11/8, 2016 at 20:42 Comment(3)
Have you tried including the trait by doing use ResetsPasswords; inside your controller and then calling the relevant method from the trait?Phrenic
@Phrenic Yes. I've used use ResetsPasswords; and $this->postEmail($request);. There is no output; neither error nor success but I never receive an email.Zinfandel
update your auth.php configuration file driverParabasis
Y
27

Why not just something like this for your controller:

<?php

namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Password;

class YourController extends Controller
{
    public function sendEmail()
    {
        $credentials = ['email' => $email_address];
        $response = Password::sendResetLink($credentials, function (Message $message) {
            $message->subject($this->getEmailSubject());
        });

        switch ($response) {
            case Password::RESET_LINK_SENT:
                return redirect()->back()->with('status', trans($response));
            case Password::INVALID_USER:
                return redirect()->back()->withErrors(['email' => trans($response)]);
        }
    }
}

You don't really explain the context of how you want to send this, so adjust accordingly.

Yellowthroat answered 11/8, 2016 at 22:11 Comment(5)
Thanks - this got me on the right track! I just edited the post to make it clear that the first argument to sendResetLink is supposed to be an array of the form ['email' => $email_address]Zinfandel
Awesome, glad to hear!Yellowthroat
you saved my day, mate!Keyes
I got some error. "Route [password.reset] not defined" .Antihelix
Works very well.Chiang
F
22

Thanks to Mariusz Kurman, I only added token to his answer. this works just fine:

$user = User::where('email', request()->input('email'))->first();
$token = Password::getRepository()->create($user);
$user->sendPasswordResetNotification($token);
Fiftyfifty answered 18/8, 2019 at 5:41 Comment(0)
B
18

Complete control for Laravel 5.5:

    $user = User::where('email', request()->input('email'))->first();
    $token = Password::getRepository()->create($user);

    Mail::send(['text' => 'emails.password'], ['token' => $token], function (Message $message) use ($user) {
        $message->subject(config('app.name') . ' Password Reset Link');
        $message->to($user->email);
    });
Beamer answered 5/9, 2017 at 19:42 Comment(4)
Cheers for the Password::getRepository()->create(); Also, this worked on 5.4 as well.Characharabanc
Same as @SuperNOVA: thanks, and it also works on 5.8.Floatation
And here’s the link href to put in the mail: {{ route('nova.password.reset', ['token' => $token ]) }}Floatation
@Beamer why Password::getRepository()->create($user) and not just Password::createToken($user) - see src github.com/laravel/framework/blob/5.4/src/Illuminate/Auth/…Disepalous
M
4

The easiest way:

    $token = Str::random(60);
    $user = User::where('email', request()->input('email'))->first();
    $user->sendPasswordResetNotification($token);

@Doc's bottom

And if you want to edit your e-mail manually:

    php artisan vendor:publish

select "11" gives you:

/resources/views/vendor/notifications/email.blade.php
Martres answered 22/11, 2018 at 8:57 Comment(1)
You're using a '$token' variable that's not defined anywhere.Eyespot
P
1

Use this code for send password reset link. here i am API Route for this.

  $this->validate($request, [
        'user_id'    => 'required|int',
    ]);

    $user =  User::find($request->user_id);
    if( $user )
    {
        $credentials = ['email' => $user->email];
        $response = Password::sendResetLink($credentials, function (Message $message) {
            $message->subject($this->getEmailSubject());
        });

        switch ($response) {
            case Password::RESET_LINK_SENT:
                return response()->json([
                    'status'        => 'success',
                    'message' => 'Password reset link send into mail.',
                    'data' =>''], 201);
            case Password::INVALID_USER:
                return response()->json([
                    'status'        => 'failed',
                    'message' =>   'Unable to send password reset link.'
                ], 401);
        }  
    }
    return response()->json([
        'status'        => 'failed',
        'message' =>   'user detail not found!'
    ], 401);

for Web View :

$response = Password::sendResetLink($credentials, function (Message $message) {
            $message->subject($this->getEmailSubject());
        });

       switch ($response) {
            case Password::RESET_LINK_SENT:
                return redirect()->back()->with('status', trans($response));
            case Password::INVALID_USER:
                return redirect()->back()->withErrors(['email' => trans($response)]);
        }
Pectase answered 5/9, 2020 at 10:52 Comment(0)
H
0

At least since Laravel 8 (and still valid on Laravel 11):

use Illuminate\Support\Facades\Password;

Password::sendResetLink(['email' => $email_of_the_user]);
Handmaiden answered 24/6, 2024 at 15:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.