Laravel 5.3+ 重置密码邮件的样式及内容修改 (Notifications的使用)

Laravel里我们可以使用php artisan make:auth来生成一套默认的登陆注册重置邮箱的Authentication System,但是如何修改系统发送给用户的重置密码邮件的样式和内容呢?

Default Password Reset View

虽然默认的邮件样式很美观,但是不免全部是英文,我们至少可以添加进一些中文提示,方便用户查看。

首先我们需要明确的是:

  1. Laravel 默认的 Notification Class是ResetPassword,位于Illumintate/Auth/Notifications中。
  2. 我们不应该直接修改位于ResetPassword里的代码,因为如果更新package可能导致覆盖。

我们先来看一下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.');
    }
}

可以看到,ResetPassword拓展了Notification这个类,所以我们需要做的就是新建一个Notification类,来完成我们自定义邮件内容的修改:

$ php artisan make:notification ResetPasswordNotification

输入以上artisan命令,我们会发现在App\Notifications文件夹下多出了一个名为ResetPasswordNotification.php的文件,打开它,我们可以看到其内容跟ResetPassword很相似。我们只需要修改关键的代码即可:

<?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.');
    }
}

可以看到,我们可以以line作为单位来添加我们需要的信息。


那么信息内容搞定了,怎么样修改邮件样式呢?首先我们需要能够修改信息的Blade模板:

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

以上命令把包裹里的模板发布到resources/views/vendor/notifications文件夹中,这样我们只需要修改resources/views/vendor/notifications/email.blade.php就可以了。

最后一步,我们在User模型里添加:

/**
 * Send the password reset notification.
 *
 * @param  string  $token
 * @return void
 */
public function sendPasswordResetNotification($token)
{
    $this->notify(new ResetPasswordNotification($token));
}

这样,我们就可以使用ResetPasswordNotification来进行邮件的发送了。

模板的修改很简单,这里就不赘述了,完成后,我们可以看到新的邮件内容:
Reset Password Improved View

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章