如何更改 laravel 中的重设密码电子邮件主题?
How to change reset password email subject in laravel?
我是 Laravel 的初学者。目前我正在学习这个框架。我当前的 Laravel 版本是 5.3。
我正在使用 php artisan make:auth
构建我的身份验证,一切正常。此外,我在我的 .env 文件中配置了 gmail smtp,并在 config directgory 中配置了 mail.php。一切都很好。但我看到默认情况下忘记密码的电子邮件主题是 Reset Password
。我想改变它。
我看了一些博客。我找到了一些博客。我已经在我的网站上实现了。但同样的输出即将到来。
我点击了这些链接 -
https://laracasts.com/discuss/channels/general-discussion/laravel-5-password-reset-link-subject
https://laracasts.com/discuss/channels/general-discussion/reset-password-email-subject
您可以轻松修改用于向用户发送密码重置 link 的通知 class。首先,覆盖 User 模型上的 sendPasswordResetNotification
方法。在此方法中,您可以使用您选择的任何通知 class 发送通知。密码重置 $token
是该方法收到的第一个参数,请参阅 Doc for Customization
/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
希望对您有所帮助!
您可以创建一个自定义函数来像这样创建重置密码令牌。
$user = User::where('email', 'example@name.com' )->first();
$password_broker = app(PasswordBroker::class); //so we can have dependency injection
$token = $password_broker->createToken($user); //create reset password token
$password_broker->emailResetLink($user, $token, function (Message $message) {
$message->subject('Custom Email title');
});//send email.
您可以更改密码重设电子邮件的主题,但这需要一些额外的工作。首先,您需要创建自己的 ResetPassword
通知实现。
在app\Notifications
目录中创建一个新通知class,我们将其命名为ResetPassword.php
:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class ResetPassword extends Notification
{
public $token;
public function __construct($token)
{
$this->token = $token;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Your Reset Password Subject Here')
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', url('password/reset', $this->token))
->line('If you did not request a password reset, no further action is required.');
}
}
您还可以使用 artisan 命令生成通知模板:
php artisan make:notification ResetPassword
或者您可以简单地复制粘贴上面的代码。您可能会注意到此通知 class 与默认 Illuminate\Auth\Notifications\ResetPassword
非常相似。您实际上可以从默认 ResetPassword
class 扩展它。
唯一的区别在于,您添加了一个新的方法调用来定义电子邮件的主题:
return (new MailMessage)
->subject('Your Reset Password Subject Here')
您可以阅读更多关于 Mail Notifications here 的内容。
其次,在您的 app\User.php
文件中,您需要覆盖 Illuminate\Auth\Passwords\CanResetPassword
特征定义的默认 sendPasswordResetNotification()
方法。现在您应该使用自己的 ResetPassword
实现:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\ResetPassword as ResetPasswordNotification;
class User extends Authenticatable
{
use Notifiable;
...
public function sendPasswordResetNotification($token)
{
// Your your own implementation.
$this->notify(new ResetPasswordNotification($token));
}
}
现在您的重设密码电子邮件主题应该已更新!
希望对您有所帮助!
只需添加以下行:
->主题('New Subjetc')
在文件的toMail方法中Illuminate\Auth\Notifications\ResetPassword
像这样:
public function toMail($notifiable)
{
return (new MailMessage)
->subject('New Subjetc')
->line('You are receiving this email because we received a password reset request for your account.')
->action('Restaurar Contraseña', url(config('app.url').route('password.reset', $this->token, false)))
->line('If you did not request a password reset, no further action is required.');
}
在Laravel 5.7
中,默认实现类似于:
return (new MailMessage)
->subject(Lang::getFromJson('Reset Password Notification'))
->line(Lang::getFromJson('You are receiving this email because we received a password reset request for your account.'))
->action(Lang::getFromJson('Reset Password'), url(config('app.url').route('password.reset', $this->token, false)))
->line(Lang::getFromJson('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.users.expire')]))
->line(Lang::getFromJson('If you did not request a password reset, no further action is required.'));
您只需将 locale
从 config/app.php
更改为 ro
,然后在 resources/lang
中创建一个文件 ro.json
类似这样:
{
"Reset Password Notification": "Viața Medicală CMS :: Resetare parolă",
"Hello!": "Salut,",
"You are receiving this email because we received a password reset request for your account.": "Primești acest email deoarece am primit o solicitare de resetare a parolei pentru contul tău.",
"Reset Password": "Reseteză parola",
"This password reset link will expire in :count minutes.": "Acest link va expira în :count de minute.",
"If you did not request a password reset, no further action is required.": "Dacă nu ai solicitat resetarea parolei, nu este necesară nicio altă acțiune.",
"Regards": "Toate cele bune",
"Oh no": "O, nu",
"Whoops!": "Hopa!",
"If you’re having trouble clicking the \":actionText\" button, copy and paste the URL below\ninto your web browser: [:actionURL](:actionURL)": "Dacă nu reușești să dai click pe butonul de \":actionText\", dă copy-paste la URL-ul de mai jos în browser:\n [:actionURL](:actionURL)"
}
它将翻译主题(第一个键)和邮件正文。
更新 Laravel 6。*
这也可以用于 VerifyEmail.php
通知。
致所有询问如何更新您好、问候和子副本文本的人:
php artisan vendor:publish
(选项 11)
然后在 views/vendor/notifications/email.blade.php
在这个文件中会有像 Hello 这样的文本,您可以通过更改来更改:
例如:
第 9 行# @lang('Hallo!, Hei!, Bonjour!, Guten Tag!, Geia!')
Laravel 8
在AuthServiceProvider.php
添加这些代码。
ResetPassword::toMailUsing(function ($notifiable, $url) {
return (new MailMessage)
->subject(Lang::get('Reset Password Notification'))
->line(Lang::get('You are receiving this email because we received a password reset request for your account.'))
->action(Lang::get('Reset Password'), $url)
->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.' . config('auth.defaults.passwords') . '.expire')]))
->line(Lang::get('If you did not request a password reset, no further action is required.'));
});
我是 Laravel 的初学者。目前我正在学习这个框架。我当前的 Laravel 版本是 5.3。
我正在使用 php artisan make:auth
构建我的身份验证,一切正常。此外,我在我的 .env 文件中配置了 gmail smtp,并在 config directgory 中配置了 mail.php。一切都很好。但我看到默认情况下忘记密码的电子邮件主题是 Reset Password
。我想改变它。
我看了一些博客。我找到了一些博客。我已经在我的网站上实现了。但同样的输出即将到来。
我点击了这些链接 -
https://laracasts.com/discuss/channels/general-discussion/laravel-5-password-reset-link-subject
https://laracasts.com/discuss/channels/general-discussion/reset-password-email-subject
您可以轻松修改用于向用户发送密码重置 link 的通知 class。首先,覆盖 User 模型上的 sendPasswordResetNotification
方法。在此方法中,您可以使用您选择的任何通知 class 发送通知。密码重置 $token
是该方法收到的第一个参数,请参阅 Doc for Customization
/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
希望对您有所帮助!
您可以创建一个自定义函数来像这样创建重置密码令牌。
$user = User::where('email', 'example@name.com' )->first();
$password_broker = app(PasswordBroker::class); //so we can have dependency injection
$token = $password_broker->createToken($user); //create reset password token
$password_broker->emailResetLink($user, $token, function (Message $message) {
$message->subject('Custom Email title');
});//send email.
您可以更改密码重设电子邮件的主题,但这需要一些额外的工作。首先,您需要创建自己的 ResetPassword
通知实现。
在app\Notifications
目录中创建一个新通知class,我们将其命名为ResetPassword.php
:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class ResetPassword extends Notification
{
public $token;
public function __construct($token)
{
$this->token = $token;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Your Reset Password Subject Here')
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', url('password/reset', $this->token))
->line('If you did not request a password reset, no further action is required.');
}
}
您还可以使用 artisan 命令生成通知模板:
php artisan make:notification ResetPassword
或者您可以简单地复制粘贴上面的代码。您可能会注意到此通知 class 与默认 Illuminate\Auth\Notifications\ResetPassword
非常相似。您实际上可以从默认 ResetPassword
class 扩展它。
唯一的区别在于,您添加了一个新的方法调用来定义电子邮件的主题:
return (new MailMessage)
->subject('Your Reset Password Subject Here')
您可以阅读更多关于 Mail Notifications here 的内容。
其次,在您的 app\User.php
文件中,您需要覆盖 Illuminate\Auth\Passwords\CanResetPassword
特征定义的默认 sendPasswordResetNotification()
方法。现在您应该使用自己的 ResetPassword
实现:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\ResetPassword as ResetPasswordNotification;
class User extends Authenticatable
{
use Notifiable;
...
public function sendPasswordResetNotification($token)
{
// Your your own implementation.
$this->notify(new ResetPasswordNotification($token));
}
}
现在您的重设密码电子邮件主题应该已更新!
希望对您有所帮助!
只需添加以下行:
->主题('New Subjetc')
在文件的toMail方法中Illuminate\Auth\Notifications\ResetPassword 像这样:
public function toMail($notifiable)
{
return (new MailMessage)
->subject('New Subjetc')
->line('You are receiving this email because we received a password reset request for your account.')
->action('Restaurar Contraseña', url(config('app.url').route('password.reset', $this->token, false)))
->line('If you did not request a password reset, no further action is required.');
}
在Laravel 5.7
中,默认实现类似于:
return (new MailMessage)
->subject(Lang::getFromJson('Reset Password Notification'))
->line(Lang::getFromJson('You are receiving this email because we received a password reset request for your account.'))
->action(Lang::getFromJson('Reset Password'), url(config('app.url').route('password.reset', $this->token, false)))
->line(Lang::getFromJson('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.users.expire')]))
->line(Lang::getFromJson('If you did not request a password reset, no further action is required.'));
您只需将 locale
从 config/app.php
更改为 ro
,然后在 resources/lang
中创建一个文件 ro.json
类似这样:
{
"Reset Password Notification": "Viața Medicală CMS :: Resetare parolă",
"Hello!": "Salut,",
"You are receiving this email because we received a password reset request for your account.": "Primești acest email deoarece am primit o solicitare de resetare a parolei pentru contul tău.",
"Reset Password": "Reseteză parola",
"This password reset link will expire in :count minutes.": "Acest link va expira în :count de minute.",
"If you did not request a password reset, no further action is required.": "Dacă nu ai solicitat resetarea parolei, nu este necesară nicio altă acțiune.",
"Regards": "Toate cele bune",
"Oh no": "O, nu",
"Whoops!": "Hopa!",
"If you’re having trouble clicking the \":actionText\" button, copy and paste the URL below\ninto your web browser: [:actionURL](:actionURL)": "Dacă nu reușești să dai click pe butonul de \":actionText\", dă copy-paste la URL-ul de mai jos în browser:\n [:actionURL](:actionURL)"
}
它将翻译主题(第一个键)和邮件正文。
更新 Laravel 6。*
这也可以用于 VerifyEmail.php
通知。
致所有询问如何更新您好、问候和子副本文本的人:
php artisan vendor:publish
(选项 11)
然后在 views/vendor/notifications/email.blade.php
在这个文件中会有像 Hello 这样的文本,您可以通过更改来更改:
例如:
第 9 行# @lang('Hallo!, Hei!, Bonjour!, Guten Tag!, Geia!')
Laravel 8
在AuthServiceProvider.php
添加这些代码。
ResetPassword::toMailUsing(function ($notifiable, $url) {
return (new MailMessage)
->subject(Lang::get('Reset Password Notification'))
->line(Lang::get('You are receiving this email because we received a password reset request for your account.'))
->action(Lang::get('Reset Password'), $url)
->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.' . config('auth.defaults.passwords') . '.expire')]))
->line(Lang::get('If you did not request a password reset, no further action is required.'));
});