我想从控制器中手动向特定用户(不是当前登录的用户)发送密码重置请求。我在Laravel代码中进行了一些挖掘,似乎我应该在postEmail(Request $request)
中调用ResetsPasswords
,但我似乎无法弄清楚如何访问正确的PasswordController
实例称之为。
答案 0 :(得分:13)
为什么不为你的控制器做这样的事情:
<?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)]);
}
}
}
您没有真正解释如何发送此内容的背景,因此请进行相应调整。
答案 1 :(得分:7)
完全控制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);
});
答案 2 :(得分:5)
感谢Mariusz Kurman,我只在他的回答中添加了令牌。效果很好:
$user = User::where('email', request()->input('email'))->first();
$token = Password::getRepository()->create($user);
$user->sendPasswordResetNotification($token);
答案 3 :(得分:0)
最简单的方法:
$user = User::where('email', request()->input('email'))->first();
$user->sendPasswordResetNotification($token);
如果要手动编辑电子邮件,请执行以下操作:
php artisan vendor:publish
选择“ 11” 给你:
/resources/views/vendor/notifications/email.blade.php
答案 4 :(得分:0)
使用此代码发送密码重置链接。这是我的 API路由。
$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);
对于Web视图:
$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)]);
}