Laravel
Here we can use php artisan make:auth
it 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?
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:
- Laravel's default Notification Class is ResetPassword, located
Illumintate/Auth/Notifications
in .- 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 ResetPassword
has been extended Notification
, so all we need to do is to create a new Notification
class to complete the modification of our custom email content:
$ php artisan make:notification ResetPasswordNotification
- 1
Enter the above artisan
command, we will find a App\Notifications
file named under ResetPasswordNotification.php
the folder, open it, we can see that its content ResetPassword
is 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 Blade
template 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/notifications
folder, so we only need to modify resources/views/vendor/notifications/email.blade.php
it.
As a final step, we User
add 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 ResetPasswordNotification
it 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: