Change email viewing path for reset password to Laravel

Using Laravel 5, I need two different views for the password reset. The default path to the email view is emails.password . But under some conditions, I want to send emails.password_alternative .

How can i do this? (using PasswordBroker from Laravel)

This is my current code:

public function __construct(Guard $auth, PasswordBroker $passwords) { $this->auth = $auth; $this->passwords = $passwords; } public function sendReset(PasswordResetRequest $request) { //HERE : If something, use another email view instead of the default one from the config file $response = $this->passwords->sendResetLink($request->only('email'), function($m) { $m->subject($this->getEmailSubject()); }); } 
+5
source share
2 answers

Using PasswordBroker and based on Illuminate/Auth/Passwords/PasswordBroker.php , $emailView is a protected variable, so you cannot change the value after instantiating the class.

However, you have several solutions:

  • You can create your own class that extends PasswordBroker and will use it.

     class MyPasswordBroker extends PasswordBroker { public function setEmailView($view) { $this->emailView = $view; } } // (...) public function __construct(Guard $auth, MyPasswordBroker $passwords) { $this->auth = $auth; $this->passwords = $passwords; } public function sendReset(PasswordResetRequest $request) { if ($someConditionHere) { $this->passwords->setEmailView('emails.password_alternative'); } $response = $this->passwords->sendResetLink($request->only('email'), function($m) { $m->subject($this->getEmailSubject()); }); } 
  • You can create a PasswordBroker in your method without using Injection Dependency.

     public function sendReset(PasswordResetRequest $request) { $emailView = 'emails.password'; if ($someConditionHere) { $emailView = 'emails.password_alternative'; } $passwords = new PasswordBroker( App::make('TokenRepositoryInterface'), App::make('UserProvider'), App::make('MailerContract'), $emailView ); $response = $passwords->sendResetLink($request->only('email'), function($m) { $m->subject($this->getEmailSubject()); }); } 

    This is an ugly solution, and if you have automated tests, it will be a pain to work with.

Disclaimer: I have not tested this code.

+4
source

For anyone interested in Laravel 5.2, you can set up a custom html and text email for the password reset by adding

 config(['auth.passwords.users.email' => ['auth.emails.password.html', 'auth.emails.password.text']]); 

in PasswordController.php in the constructor before invoking the middleware.

This overrides the app / config / auth.php setting for PasswordBroker.

The Blade template for the reset email password is then located at:

yourprojectname / resources / views / authentication / letters / password / html.blade.php yourprojectname / resources / views / authentication / letter / password / text.blade.php

Took me long enough.

Credits: http://ericlbarnes.com/2015/10/14/how-to-send-both-html-and-plain-text-password-reset-emails-in-laravel-5-1/ http: // academe.co.uk/2014/01/laravel-multipart-registration-and-reminder-emails/

+7
source

Source: https://habr.com/ru/post/1245831/


All Articles