Fortify-如何自定义验证/密码重置电子邮件?

我正在将强化实施到我的应用程序中。当您点击密码重置/验证电子邮件路由时,我真的对自定义生成的默认电子邮件感到困惑?

我可以在供应商中编辑它们,但是每次更新都会给我带来问题。

必须有一个钩子来提供替代电子邮件模板。

不幸的是,我找不到任何解释它是如何完成的文档。

我是否需要添加:

public function sendEmailVerificationNotification()
{


}

到我的用户模型?如果是这样,我如何生成返回验证 URL,因为它没有保存在数据库中?

任何帮助都会很棒。

谢谢!

回答

这是 Laravel 8 中的解决方案。

1.) 通过命令创建通知

php artisan make:notification ResetPasswordNotification
<?php

namespace AppNotifications;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
use IlluminateAuthNotificationsResetPassword;
use IlluminateNotificationsNotification;
use IlluminateSupportFacadesLang;

class ResetPasswordNotification extends ResetPassword
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public $token;

    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return IlluminateNotificationsMessagesMailMessage
     */
    public function toMail($notifiable)
    {
        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $this->token);
        }

        $url = url(config('app.url') . route('password.reset', [
            'token' => $this->token,
            'email' => $notifiable->getEmailForPasswordReset(),
        ], false));

        return (new MailMessage)
            ->view(
                'emails.reset_password', ['name'=>$notifiable->name,'url' => $url]
            )
            ->subject(Lang::get('Reset Password'));
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

2.) 在 app/Models/User.php 模型中。添加下面的方法。

   public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token));
    }

3.) 现在在 views/emails/reset_password.blade.php 中创建一个电子邮件模板

Hi {{ $name }}, Please reset your password here. Click on the below link to reset the password.
 <a href="{{ $url }}">RESET</a>


以上是Fortify-如何自定义验证/密码重置电子邮件?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>