0

我正在使用 Laravel 5.3 并自定义密码重置电子邮件模板。我已经完成了以下更改,以使用自定义 Mailable 类为通知创建自己的 html 电子邮件。这是我到目前为止的进展:

忘记密码控制器:

public function postEmail(Request $request)
    {
        $this->validate($request, ['email' => 'required|email']);

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

        switch ($response) {
            case Password::RESET_LINK_SENT:
                return Response::json(['status' => trans($response)], 200);

            case Password::INVALID_USER:
                return Response::json(['email' => trans($response)], 400);
        }
    }

用户模型:

public function sendPasswordResetNotification($token)
{
    Mail::queue(new ResetPassword($token));
}

ResetPassword 可邮寄类:

protected $token;

public function __construct($token)
{
    $this->token = $token;
}

public function build()
{
    $userEmail = 'something'; // How to add User Email??
    $userName = 'Donald Trump'; // How to find out User's Name??
    $subject = 'Password Reset';

    return $this->view('emails.password')
                ->to($userEmail)
                ->subject($subject)
                ->with([
                        'token'   => $this->token
                        'userEmail' => $userEmail,
                        'userName'  => $userName
                      ]);
    }   

如果您在上面注意到,我不确定如何传递用户名并找出用户的电子邮件地址。我需要从用户模型发送这些数据还是从 Mailable 类中查询它?有人可以告诉我我该怎么做吗?

4

1 回答 1

1

通常您要求用户电子邮件以发送重置密码电子邮件,该电子邮件应作为请求参数提供给您的路由控制器。

默认情况下,L5.3 使用 post('password/email) 路由来处理重置密码请求。此路由执行在 App\Http\Controllers\Auth\ForgotPasswordController 使用的“SendsPasswordResetEmails”特征中定义的 sendResetLinkEmail 方法。

从这里您可以选择 2 个选项之一:

第一:您可以覆盖路由以调用同一控制器(或任何其他控制器,在这种情况下可能是您的 postEmail 函数)中的另一个函数,该函数通过您收到的电子邮件搜索用户模型,然后您可以将用户模型传递为执行队列邮件操作的方法的函数参数(这可能需要也可能不需要覆盖 SendsPasswordResetEmails,取决于您如何处理重置密码方法)。

这个解决方案看起来像这样:

在路线/web.php

post('password/email', 'Auth\ForgotPasswordController@postEmail')

在 app/Mail/passwordNotification.php 中(例如)

protected $token;
protected $userModel;

public function __construct($token, User $userModel)
{
    $this->token = $token;
    $this->userModel = $userModel;
}

public function build()
{
    $userEmail = $this->userModel->email;
    $userName = $this->userModel->email
    $subject = 'Password Reset';

    return $this->view('emails.password')
                ->to($userEmail)
                ->subject($subject)
                ->with([
                        'token'   => $this->token
                        'userEmail' => $userEmail,
                        'userName'  => $userName
                      ]);
    }  

在 app/Http/Controllers/Auth/ForgotPasswordController

public function postEmail(Request $request)
    {
        $this->validate($request, ['email' => 'required|email']);
        $userModel = User::where('email', $request->only('email'))->first();
        Mail::queue(new ResetPassword($token));
        //Manage here your response
    }

第二:您可以重写特征 SendsPasswordResetEmails 以通过电子邮件搜索用户模型,并在 sendResetLinkEmail 函数中使用您自定义的函数。在那里您可以使用您的函数,但请注意您仍然必须以某种方式处理状态以创建响应,因为您已经在 ForgotPasswordController 上拥有它。

我希望它有帮助!

于 2016-11-18T20:55:36.230 回答