【问题标题】:How to customize the mail template on Laravel 5.5?如何在 Laravel 5.5 上自定义邮件模板?
【发布时间】:2018-01-05 20:22:40
【问题描述】:

在 Laravel 5.2 上,我可以通过将以下代码放入 config/auth.php 文件中来配置邮件模板。

'passwords' => [
    'users' => [
        'provider' => 'users',
        'email' => 'auth.emails.password',
        'table' => 'password_resets',
        'expire' => 1440,
    ],
],

这将使用我的 resources/views/auth/emails/password.blade.php 文件,我可以在其中自定义电子邮件的外观。但它在 Laravel 5.5 中不起作用。如何在 Laravel 5.5 上自定义邮件模板?

【问题讨论】:

    标签: php email templates laravel-5


    【解决方案1】:

    将供应商刀片文件发布到您的resources/views/vendor 目录后,您可以在resources/views/vendor/notifications 目录下编辑email.blade.php

    php artisan vendor:publish
    

    或者您可以通过覆盖用户模型中的sendPasswordResetNotification 方法来完全覆盖重置通知:

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

    【讨论】:

    • 我有一个错误“找不到类'ResetPasswordNotification'”,我确实在用户类的顶部放置了“使用ResetPasswordNotification”。
    • 您是否使用了正确的路径?这是默认通知,您在表单中实现此方法。
    • 我不确定您所说的“使用正确的路径”是什么意思。但我确实在我的用户模型中实现了 sendPasswordResetNotification($token) 函数。在请求密码重置链接时,已调用此方法并尝试创建 new ResetPasswordNotification($token) 的实例对象。但是它在我的 Laravel 5.5 项目中的任何地方都找不到这个 ResetPasswordNotification 类。
    • 所以你看起来不太好。 sendPasswordResetNotification 方法来自CanResetPassword 特征,其中ResetPasswordNotificationResetPassword 通知的别名。
    【解决方案2】:

    您将供应商刀片email.blade.php 文件发布到resources/views/vendor/notifications,您使用:
    $php artisan vendor:publish
    编辑文件以自定义布局电子邮件模板后。 继续,您在 User 模型中覆盖函数 sendPasswordResetNotification /** * Send the password reset notification. * * @param string $token * @return void */ public function sendPasswordResetNotification($token) { $this->notify(new ResetPasswordNotification($token)); } 使用ResetPasswordNotification,您可以在app\Notifications 中创建ResetPasswordNotification。内容与文件Illuminate\Auth\Notifications\ResetPassword的内容相同: /** * 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.') ->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.'); }

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 2020-12-09
      • 2018-06-19
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      相关资源
      最近更新 更多