【问题标题】:Fortify - How to customise verification / password reset emails?Fortify - 如何自定义验证/密码重置电子邮件?
【发布时间】:2021-05-15 15:24:00
【问题描述】:

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

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

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

不幸的是,我找不到任何解释其完成方式的文档。

是否需要补充:

public function sendEmailVerificationNotification()
{


}

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

任何帮助都会很棒。

谢谢!

【问题讨论】:

    标签: laravel laravel-8 laravel-mail laravel-fortify


    【解决方案1】:

    使用fortifyvendor\laravel\framework\src\Illuminate\ Notifications\resources\views\email.blade.php时可以进入目录,覆盖邮件的内容和样式。

    通知默认在vendor\laravel\framework\src\Illuminate\Auth\Notifications\VerifyEmail.php目录中

    您可以在此处更改电子邮件文本行。同样在ResetPassword.php重置密码

    【讨论】:

    • 这是一个非常糟糕的答案。您永远不应该更改供应商文件夹中的文件。更新可能会覆盖更改,所有工作都是徒劳的!提示:如果只是关于文本,请看这里:github.com/Laravel-Lang/lang
    • 为什么这被标记为选择的答案?哇...任何说“在供应商目录中编辑 x”的答案都应该在他们发布的第二秒被 SO 自动删除! - 永远不要听任何告诉您在供应商目录中编辑某些内容的答案。
    【解决方案2】:

    这是 Laravel 8 中的解决方案。

    1.) 通过命令创建通知

    php artisan make:notification ResetPasswordNotification
    
    <?php
    
    namespace App\Notifications;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Notifications\Messages\MailMessage;
    use Illuminate\Auth\Notifications\ResetPassword;
    use Illuminate\Notifications\Notification;
    use Illuminate\Support\Facades\Lang;
    
    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 \Illuminate\Notifications\Messages\MailMessage
         */
        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>
    

    【讨论】:

      【解决方案3】:

      您可以通过在 FortifyServiceProvider 中添加以下内容来自定义密码重置电子邮件

      ResetPassword::toMailUsing(function($user, string $token) {
          return (new MailMessage)
              ->subject('Reset Password')
              ->view('emails.password_reset', [
                  'user' => $user,
                  'url' => sprintf('%s/users/password_reset/%s', config('app.url'), $token)
          ]);
      });
      

      创建一个名为resources/views/emails/password_reset.blade.php的文件,在这个文件中你可以使用$user$url

      【讨论】:

        猜你喜欢
        • 2022-07-06
        • 1970-01-01
        • 1970-01-01
        • 2018-04-08
        • 2020-06-26
        • 2020-11-01
        • 1970-01-01
        • 2020-01-04
        • 2021-11-27
        相关资源
        最近更新 更多