2016-11-18 26 views
0

我正在使用Laravel 5.3並自定義密碼重置電子郵件模板。我做了以下更改,以使用自定義Mailable類創建我自己的HTML電子郵件通知。這是我迄今取得的進展:自定義重置密碼電子郵件並在Laravel 5.3中傳遞用戶數據

ForgotPasswordController:

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類中查詢它?有人能告訴我我該怎麼做嗎?

+0

這不是很清楚。您何時或何時使用此電子郵件通知?我的意思是,你是否在用戶密碼重置之後使用它?或者你的意思是密碼重置電子郵件,其中包含重置密碼的鏈接?我說我可以幫你,但我根本不瞭解這種情況。 –

+0

感謝您的關注@JulianRodriguez我正在嘗試在包含重置密碼錶單鏈接的密碼重置電子郵件中執行此操作。我用'ForgotPasswordController'更新了我的帖子。基本上我試圖將'Notification'替換爲'Mailable'類,以便在重置鏈接郵寄時爲此設置不同的html佈局。 – Neel

回答

1

通常您會要求用戶電子郵件發送重置密碼電子郵件,該電子郵件應作爲請求參數發送給您的路由控制器。

默認情況下,L5.3使用post('密碼/電子郵件)路由來處理重置密碼請求。此路由執行sendResetLinkEmail方法,該方法在App \ Http \ Controllers \ Auth \ ForgotPasswordController使用的'SendsPasswordResetEmails'特徵中定義。

從這裏你可以採取2個選項之一:

1:你可以改寫路由調用同一個控制器的另一個功能(或任何其他控制器,在這種情況下,可能是你的postEmail功能),該搜索對於您收到的電子郵件的用戶模型,則可以將用戶模型作爲函數參數傳遞給執行隊列郵件操作的方法(這可能需要也可能不需要覆蓋SendsPasswordResetEmails,具體取決於您如何處理重置密碼方法)。

這個解決方案看起來是這樣的:

在路由/ web.php

post('password/email', 'Auth\[email protected]') 

在app /郵件/ 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 
         ]); 
    } 

的應用/ 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上創建了它。

我希望它有幫助!

+0

非常感謝你@ Julian那肯定給了我一個關於我現在如何解決這個問題的想法。非常感謝幫助我...我會嘗試使用您的建議更新我的代碼。 – Neel

+0

歡迎您先生。 –

相關問題