Laravel 5.3重新定义重置电子邮件刀片模板

问题描述:

如何在Laravel 5.3中自定义重置电子邮件模板的路径?Laravel 5.3重新定义重置电子邮件刀片模板

使用的模板是:vendor/laravel/framework/src/Illuminate/Notifications/resources/views/email.blade.php

我想建立自己的。

此外,如何改变这种电子邮件的预定义的文本:vendor/laravel/framework/src/Illuminate/Auth/Notifications/ResetPassword.php

public function toMail() 
{ 
    return (new MailMessage) 
     ->line([ 
      'You are receiving this email because we received a password reset request for your account.', 
      'Click the button below to reset your password:', 
     ]) 
     ->action('Reset Password', url('password/reset', $this->token)) 
     ->line('If you did not request a password reset, no further action is required.'); 
} 

要更改模板,你应该使用工匠命令php artisan vendor:publish它会在你resources/views/vendor目录中创建刀模板。要更改电子邮件的文本,您应该覆盖用户模型上的sendPasswordResetNotification方法。这里描述https://laravel.com/docs/5.3/passwords重置电子邮件定制部分。

您必须将新方法添加到您的用户模型中。

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

并使用自己的类来通知,而不是ResetPasswordNotification。

修订:对@ lewis4u要求

一步一步的指示:

  1. 要创建新的Notification类,则必须使用此命令行php artisan make:notification MyResetPassword。它会在app/Notifications目录下创建一个新的Notification Class'MyResetPassword'。

  2. 附加use App\Notifications\MyResetPassword;到您的用户模型

  3. 添加新的方法,以您的用户模型。

    public function sendPasswordResetNotification($token) 
    { 
        $this->notify(new MyResetPassword($token)); 
    } 
    
  4. 运行PHP的工匠命令php artisan vendor:publish --tag=laravel-notifications运行此命令后,邮件通知模板将位于资源/视图/供应商/通知目录。

  5. 如果需要,请编辑您的MyResetPassword类方法toMail()。这里描述的是https://laravel.com/docs/5.3/notifications

  6. 如果需要编辑电子邮件刀片模板。这是resources/views/vendor/notifications/email.blade.php

奖励: Laracast视频:https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/9

PS:感谢@ Garric15的建议有关php artisan make:notification

+1

另外,创建一个新的Notification类,则必须使用此命令行' php artisan make:通知MyOwnResetPassword' [更多信息](https://laracasts.com/discuss/channels/laravel/how-to-override-message-in-sendresetlinkemail-in-forgotpasswordcontroller#reply-183598) – Garric15

我想阐述一个非常有益的Eugen’s answer,但没有足够的声望发表评论。

如果您想拥有自己的目录结构,则不必使用发布到views/vendor/notifications/..的刀片模板。当你创建一个新的Notification类,并开始建立你的MailMessage类,它有一个view()方法,你可以用它来替代默认的观点:

/** 
* Get the mail representation of the notification. 
* 
* @param mixed $notifiable 
* @return \Illuminate\Notifications\Messages\MailMessage 
*/ 
public function toMail($notifiable) 
{ 
    return (new MailMessage) 
     ->view('emails.password_reset'); 
     // resources/views/emails/password_reset.blade.php will be used instead. 
} 
+0

谢谢!浪费整晚试图弄清楚这一点,这是很容易.. pff .... –