Replace password reset mail template with laravel 5.3 custom template

I ran the laravel command for the authentication system, php artisan make:auth made the authentication system for my application and almost everything works.

Now, when I use a forgotten password, and it sends me a token to my mail id, I see that the template contains laravel and some other things that I can change or omit, to be precise, I want my custom template to be there be used.

I looked at the controllers and their source files, but I can not find the template or code that displays the html in the letter.

How should I do it?

How do I change it?

This is the default template that comes from laravel to mail. enter image description here

+12
source share
4 answers

Run the following command in the terminal and two email templates will be copied to your resources / vendor / notifications folder. Then you can change the templates.

 php artisan vendor:publish --tag=laravel-notifications 

You can read more about Notifications in Laravel Docs .

+16
source

Just a head: in addition to the previous answer, there are additional steps if you want to change the notification lines, for example You are receiving this... etc. The following is a step-by-step guide.

You will need to override the default sendPasswordResetNotification default sendPasswordResetNotification of User .

Why? Because strings are pulled from Illuminate\Auth\Notifications\ResetPassword.php . Changing it in the kernel means that your changes will be lost during the Laravel update.

To do this, add the following to your User model.

 use App\Notifications\PasswordReset; // Or the location that you store your notifications (this is default). /** * Send the password reset notification. * * @param string $token * @return void */ public function sendPasswordResetNotification($token) { $this->notify(new PasswordReset($token)); } 

Finally, create this notification :

 php artisan make:notification PasswordReset 

And an example of this notification content:

 /** * The password reset token. * * @var string */ public $token; /** * Create a new notification instance. * * @return void */ public function __construct($token) { $this->token = $token; } /** * Get the notification delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return ['mail']; } /** * Build the mail representation of the notification. * * @param mixed $notifiable * @return \Illuminate\Notifications\Messages\MailMessage */ public function toMail($notifiable) { return (new MailMessage) ->line('You are receiving this email because we received a password reset request for your account.') // Here are the lines you can safely override ->action('Reset Password', url('password/reset', $this->token)) ->line('If you did not request a password reset, no further action is required.'); } 
+28
source

You can also achieve this by creating your own mail template and sending the Reset link yourself using php mail() or or Laravel Mail Facade , but first you will need to create a Reset token

1) use Illuminate\Contracts\Auth\PasswordBroker;

  $user = User::where('email', $email)->first(); if ($user) { //so we can have dependency $password_broker = app(PasswordBroker::class); //create reset password token $token = $password_broker->createToken($user); DB::table('password_resets')->insert(['email' => $user->email, 'token' => $token, 'created_at' => new Carbon]); //Send the reset token with your own template //It can be like $link = url('/').'/password/reset/'.$token; } 
0
source

I ended up using the Mail Facade in the User model ..

 public function sendPasswordResetNotification($token){ // $this->notify(new MyCustomResetPasswordNotification($token)); <--- remove this, use Mail instead like below $data = [ $this->email ]; Mail::send('email.reset-password', [ 'fullname' => $this->fullname, 'reset_url' => route('user.password.reset', ['token' => $token, 'email' => $this->email]), ], function($message) use($data){ $message->subject('Reset Password Request'); $message->to($data[0]); }); } 
0
source

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


All Articles