[英]Fortify - How to customise verification / password reset emails?
我正在将 fortify 实施到我的应用程序中。 当您点击密码重置/验证电子邮件路由时,我真的对自定义生成的默认电子邮件感到困惑吗?
我可以在供应商中编辑它们,但这每次更新都会给我带来问题。
必须有一个钩子来提供替代的 email 模板。
不幸的是,我找不到任何解释其完成方式的文档。
我需要添加:
public function sendEmailVerificationNotification()
{
}
给我的用户 model? 如果是这样,我如何生成返回验证 URL 因为它没有保存在数据库中?
任何帮助都会很棒。
谢谢!
这是 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 Model 中。 添加以下方法。
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
3.) 现在在 views/emails/reset_password.blade.php 中创建一个 email 模板
Hi {{ $name }}, Please reset your password here. Click on the below link to reset the password.
<a href="{{ $url }}">RESET</a>
您可以通过在 FortifyServiceProvider 中添加以下内容来自定义密码重置 email
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
使用fortify时可以进入vendor\laravel\framework\src\Illuminate\ Notifications\resources\views\email.blade.php
的目录,覆盖email的内容和样式。
通知默认位于目录vendor\laravel\framework\src\Illuminate\Auth\Notifications\VerifyEmail.php
您可以在此处更改 email 文本行。 同样在ResetPassword.php
中重置密码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.