swi*_*elp 1 laravel laravel-mail laravel-8 laravel-fortify
我正在将强化实施到我的应用程序中。当您点击密码重置/验证电子邮件路由时,我真的对自定义生成的默认电子邮件感到困惑?
我可以在供应商中编辑它们,但是每次更新都会给我带来问题。
必须有一个钩子来提供替代电子邮件模板。
不幸的是,我找不到任何解释它是如何完成的文档。
我是否需要添加:
public function sendEmailVerificationNotification()
{
}
Run Code Online (Sandbox Code Playgroud)
到我的用户模型?如果是这样,我如何生成返回验证 URL,因为它没有保存在数据库中?
任何帮助都会很棒。
谢谢!
小智 8
您可以通过在 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)
]);
});
Run Code Online (Sandbox Code Playgroud)
创建一个名为 的文件resources/views/emails/password_reset.blade.php,在该文件中您可以使用$user和$url
小智 6
这是 Laravel 8 中的解决方案。
1.) 通过命令创建通知
php artisan make:notification ResetPasswordNotification
Run Code Online (Sandbox Code Playgroud)
<?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 [
//
];
}
}
Run Code Online (Sandbox Code Playgroud)
2.) 在 app/Models/User.php 模型中。添加下面的方法。
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1259 次 |
| 最近记录: |