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

Deadly 提交于 2020-08-22 04:22:08

问题


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.


回答1:


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.




回答2:


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



回答3:


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



回答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


来源:https://stackoverflow.com/questions/38905761/how-do-i-manually-send-a-password-reset-request-in-laravel-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!