我做了认证系统的laravel命令,php artisan make:auth
它为我的应用制作了认证系统,几乎所有的东西都在工作。用自定义模板laravel替换密码重置邮件模板5.3
现在,当我使用忘记的密码,它向我的邮件ID发送一个令牌时,我看到该模板包含laravel和其他一些我可能想要编辑或省略的事情,具体来说,我希望自定义模板在那里使用。
我抬头看着控制器及其源文件,但我找不到在邮件中显示html的模板或代码。
我该怎么做?
我该如何改变它?
我做了认证系统的laravel命令,php artisan make:auth
它为我的应用制作了认证系统,几乎所有的东西都在工作。用自定义模板laravel替换密码重置邮件模板5.3
现在,当我使用忘记的密码,它向我的邮件ID发送一个令牌时,我看到该模板包含laravel和其他一些我可能想要编辑或省略的事情,具体来说,我希望自定义模板在那里使用。
我抬头看着控制器及其源文件,但我找不到在邮件中显示html的模板或代码。
我该怎么做?
我该如何改变它?
在终端中运行以下命令,这两个电子邮件模板将被复制到您的资源/供应商/通知文件夹。然后你可以修改模板。
php artisan vendor:publish --tag=laravel-notifications
您可以在Laravel Docs阅读更多关于Notifications
。
只是抬起头来:除了以前的答案,还有额外的步骤,如果你想修改通知行像You are receiving this...
等下面是一步一步的指导。
您需要在User
型号上使用override the defaultsendPasswordResetNotification
方法。
为什么?因为这些线是从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.');
}
谢谢。这是完整的答案。我需要添加以下行 - >主题('您的自定义主题')来更改电子邮件的主题。非常感谢。 – jpussacq
您还可以通过建立自己的邮件模板和发送复位使用链接自己实现这一目标php mail()
或or 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;
}
电子邮件模板可将内部资源/视图/供应商/通知文件夹中。 – KCP
我认为完整的答案是https://stackoverflow.com/a/41401524/2144424 – jpussacq