我为认证系统做了laravel命令,php artisan make:auth
它为我的应用程序制作了认证系统,几乎一切都正常。
现在,当我使用忘记密码并向我发送一个令牌到我的邮件ID时,我看到该模板包含laravel和其他一些我可能想编辑或省略的东西,确切地说,我希望我的自定义模板在那里使用。
我抬头看着控制器及其源文件,但我找不到模板或在邮件中显示html的代码。
我该怎么做 ?
我该如何改变它?
在终端中运行以下命令,两个电子邮件模板将复制到您的资源/供应商/通知文件夹中。然后,您可以修改模板。
php artisan vendor:publish --tag=laravel-notifications
您可以在Laravel Notifications
中阅读更多关于Docs的信息。
只是提醒:除了上一个答案,如果你想修改You are receiving this...
等通知行,还有其他步骤。下面是一个循序渐进的指南。
你需要在你的override the default模型上使用sendPasswordResetNotification
User
方法。
为什么?因为线条是从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));
}
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.');
}
您还可以通过构建自己的邮件模板并使用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;
}