【问题标题】:replace password reset mail template with custom template laravel 5.3用自定义模板 laravel 5.3 替换密码重置邮件模板
【发布时间】:2020-06-25 16:49:25
【问题描述】:

我为身份验证系统执行了 laravel 命令,php artisan make:auth 它为我的应用程序创建了身份验证系统,几乎一切正常。

现在,当我使用忘记密码并向我发送一个令牌到我的邮件 ID 时,我看到模板包含 laravel 和一些我可能想要编辑或省略的其他内容,准确地说,我希望我的自定义模板在那里使用。

我查看了控制器及其源文件,但找不到在邮件中显示 html 的模板或代码。

我该怎么做?

如何改变它?

这是来自 laravel 到邮件的默认模板。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    请注意:除了上一个答案,如果您想修改 通知行,如 You are receiving this... 等,还有其他步骤。下面是分步指南。

    您需要在 User 模型上使用 override the default sendPasswordResetNotification 方法。

    为什么?因为这些线是从Illuminate\Auth\Notifications\ResetPassword.php 拉出来的。在核心中修改它意味着您的更改会在 Laravel 更新期间丢失。

    为此,请将以下内容添加到您的 User 模型中。

    use App\Notifications\PasswordReset; // Or the location that you store your notifications (this is default).
    
    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new PasswordReset($token));
    }
    

    最后,create that notification:

    php artisan make:notification PasswordReset
    

    以及此通知内容的示例:

    /**
     * The password reset token.
     *
     * @var string
     */
    public $token;
    
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($token)
    {
        $this->token = $token;
    }
    
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }
    
    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('You are receiving this email because we received a password reset request for your account.') // Here are the lines you can safely override
            ->action('Reset Password', url('password/reset', $this->token))
            ->line('If you did not request a password reset, no further action is required.');
    }
    

    【讨论】:

    • 记得翻译子副本或发送将因未定义变量而失败!
    • 在已发布的资产中,变量在哪里?!我需要完全重写模板,并且我没有关于用于 url、问候语等的变量的参考
    • 这就是我要找的。 view('some_blade_view',compact(,,,)) 可用于定义自定义模板并发送我们想要的任何参数。 laravel.com/docs/8.x/notifications#formatting-mail-messages
    • 通过写这个url('password/reset', $this->token),我只得到了带有令牌的重置url,没有写电子邮件。如何在重置网址中添加电子邮件?
    • @camelCase camelCase - 我需要你的帮助,请建议我如何更改 ResetPassword 电子邮件的 html 模板(外观),而不是 Laravel 8.x 版本中电子邮件的内容/文本?期待听到你的声音。谢谢。
    【解决方案2】:

    在终端中运行以下命令,两个电子邮件模板将被复制到您的 resources/vendor/notifications 文件夹中。然后就可以修改模板了。

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

    您可以在 Laravel Docs 中阅读有关 Notifications 的更多信息。

    【讨论】:

    • 电子邮件模板将位于 resources/views/vendor/notifications 文件夹中。
    • 我认为完整的答案是stackoverflow.com/a/41401524/2144424
    • 更改供应商目录中的某些内容似乎不是解决方案
    【解决方案3】:

    如果您想修改邮件模板,请查看 Laravel markdown,您可以在此处更改默认邮件模板:

    如果您确实想要获取 HTML 并能够对其进行编辑,请运行以下命令:

    php artisan vendor:publish --tag=laravel-mail
    

    这会发生:

    复制目录 [/vendor/laravel/framework/src/Illuminate/Mail/resources/views] 到 [/resources/views/vendor/mail]

    资源:https://laraveldaily.com/mail-notifications-customize-templates/

    【讨论】:

      【解决方案4】:

      我最终在User 模型中使用了Mail 外观..

      public function sendPasswordResetNotification($token){
          // $this->notify(new MyCustomResetPasswordNotification($token)); <--- remove this, use Mail instead like below
      
          $data = [
              $this->email
          ];
      
          Mail::send('email.reset-password', [
              'fullname'      => $this->fullname,
              'reset_url'     => route('user.password.reset', ['token' => $token, 'email' => $this->email]),
          ], function($message) use($data){
              $message->subject('Reset Password Request');
              $message->to($data[0]);
          });
      }
      

      【讨论】:

      • 这对我有帮助,但您忘记包含 Mailuse Illuminate\Support\Facades\Mail;。谢谢。
      【解决方案5】:

      在 .env 文件或 config/app.php 文件中,您必须更改您的应用名称,它工作正常。

      【讨论】:

        【解决方案6】:

        您也可以通过构建自己的邮件模板并使用 php mail() 或 Laravel Mail Facade 发送重置链接来实现此目的,但首先您需要创建重置令牌

        1) use Illuminate\Contracts\Auth\PasswordBroker;

          $user = User::where('email', $email)->first();
                         if ($user) {
                            //so we can have dependency 
                            $password_broker = app(PasswordBroker::class);
                            //create reset password token 
                            $token = $password_broker->createToken($user); 
        
                            DB::table('password_resets')->insert(['email' => $user->email, 'token' => $token, 'created_at' => new Carbon]); 
        
        //Send the reset token with your own template
        //It can be like $link = url('/').'/password/reset/'.$token;
        
                        }
        

        【讨论】:

          猜你喜欢
          • 2017-12-19
          • 2017-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-01
          • 1970-01-01
          • 2021-12-06
          • 2016-04-26
          相关资源
          最近更新 更多