laravel modify reset password template

LaravelHere we can use php artisan make:authit to generate a set of default login and registration reset mailboxes Authentication System, but how to modify the style and content of the reset password email sent by the system to users?

Default Password Reset View

Although the default email style is very beautiful, but it is all in English, we can at least add some Chinese prompts to facilitate users to view.

First we need to be clear:

  1. Laravel's default Notification Class is ResetPassword, located Illumintate/Auth/Notificationsin .
  2. We should not modify the code located in ResetPassword directly, because it may cause an overwrite if the package is updated.

Let's take a look first ResetPassword:

<?php
namespace Illuminate\Auth\Notifications; use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; class ResetPassword extends Notification { public $token; public function __construct($token) { $this->token = $token; } public function via($notifiable) { return ['mail']; } public function toMail($notifiable) { return (new MailMessage) ->line('You are receiving this email because we received a password reset request for your account') ->action('Reset Password', url(config('app.url').route('password.reset', $this->token, false))) ->line('If you did not request a password reset, no further action is required.'); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

As you can see, this class ResetPasswordhas been extended Notification, so all we need to do is to create a new Notificationclass to complete the modification of our custom email content:

$ php artisan make:notification ResetPasswordNotification
  • 1

Enter the above artisancommand, we will find a App\Notificationsfile named under ResetPasswordNotification.phpthe folder, open it, we can see that its content ResetPasswordis very similar. We only need to modify the key code:

<?php
namespace Illuminate\Auth\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; class ResetPasswordNotification extends Notification { use Queueable; public $token; public function __construct($token) { $this->token = $token; } public function via($notifiable) { return ['mail']; } public function toMail($notifiable) { return (new MailMessage) ->line('这里可以放我们需要添加的内容') ->line('You are receiving this email because we received a password reset request for your account') ->action('Reset Password', url(config('app.url').route('password.reset', $this->token, false))) ->line('这里可以放我们需要添加的内容') ->line('If you did not request a password reset, no further action is required.'); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

As you can see, we can use line as a unit to add the information we need.


So the message content is done, how to modify the email style? First we need a Bladetemplate to be able to modify the information:

$ php artisan vendor:publish --tag=laravel-notifications
  • 1

The above command publishes the template in the package to the resources/views/vendor/notificationsfolder, so we only need to modify resources/views/vendor/notifications/email.blade.phpit.

As a final step, we Useradd to the model:

/**
 * Send the password reset notification.
 *
 * @param  string  $token
 * @return void
 */
public function sendPasswordResetNotification($token) { $this->notify(new ResetPasswordNotification($token)); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

In this way, we can use ResetPasswordNotificationit to send emails.

The modification of the template is very simple, so I won't go into details here. After completion, we can see the new email content:
Reset Password Improved View

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324855254&siteId=291194637