php 如何在laravel中更改重置密码电子邮件主题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40574001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change reset password email subject in laravel?
提问by Chinmay235
I am beginner in Laravel. Currently I am learning this framework. My curent Laravel version is 5.3.
我是 Laravel 的初学者。目前我正在学习这个框架。我当前的 Laravel 版本是 5.3。
I am scaffolding my auth by using php artisan make:auth
All are working fine. Also I configured gmail smtp in my .env file and mail.php in config directgory. All are perfectly working. But I saw by-default the forgot password email subject is going Reset Password
. I want to change that.
我通过使用php artisan make:auth
All 工作正常来搭建我的身份验证。我还在我的 .env 文件中配置了 gmail smtp,在配置目录中配置了 mail.php。一切都完美无缺。但是我看到默认情况下忘记密码的电子邮件主题是 go Reset Password
。我想改变这一点。
I saw some blog. I found some blog. I have implement that in my site. But same output coming.
我看到了一些博客。我找到了一些博客。我已经在我的网站中实现了它。但同样的输出来了。
I followed these links -
我按照这些链接 -
https://laracasts.com/discuss/channels/general-discussion/laravel-5-password-reset-link-subject
https://laracasts.com/discuss/channels/general-discussion/laravel-5-password-reset-link-subject
https://laracasts.com/discuss/channels/general-discussion/reset-password-email-subject
https://laracasts.com/discuss/channels/general-discussion/reset-password-email-subject
回答by Risan Bagja Pradana
You can change your password reset email subject, but it will need some extra work. First, you need to create your own implementation of ResetPassword
notification.
您可以更改密码重置电子邮件主题,但这需要一些额外的工作。首先,您需要创建自己的ResetPassword
通知实现。
Create a new notification class insideapp\Notifications
directory, let's named it ResetPassword.php
:
在app\Notifications
目录中创建一个新的通知类,让我们将其命名为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.');
}
}
You can also generate the notification template using artisan command:
您还可以使用 artisan 命令生成通知模板:
php artisan make:notification ResetPassword
Or you can simply copy-paste the above code. As you may notice this notification class is pretty similar with the default Illuminate\Auth\Notifications\ResetPassword
. You can actually just extend it from the default ResetPassword
class.
或者你可以简单地复制粘贴上面的代码。正如您可能注意到的,这个通知类与默认的Illuminate\Auth\Notifications\ResetPassword
. 您实际上可以从默认ResetPassword
类扩展它。
The only difference is here, you add a new method call to define the email's subject:
唯一的区别在这里,您添加一个新的方法调用来定义电子邮件的主题:
return (new MailMessage)
->subject('Your Reset Password Subject Here')
You may read more about Mail Notifications here.
Secondly, on your app\User.php
file, you need to override the default sendPasswordResetNotification()
method defined by Illuminate\Auth\Passwords\CanResetPassword
trait. Now you should use your own ResetPassword
implementation:
其次,在您的app\User.php
文件中,您需要覆盖traitsendPasswordResetNotification()
定义的默认方法Illuminate\Auth\Passwords\CanResetPassword
。现在你应该使用你自己的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));
}
}
And now your reset password email subject should be updated!
现在您的重置密码电子邮件主题应该更新!
Hope this help!
希望这有帮助!
回答by Saumya Rastogi
You may easily modify the notification class used to send the password reset link to the user. To get started, override the sendPasswordResetNotification
method on your User model. Within this method, you may send the notification using any notification class you choose. The password reset $token
is the first argument received by the method, See the Doc for Customization
您可以轻松修改用于向用户发送密码重置链接的通知类。首先,覆盖sendPasswordResetNotification
用户模型上的方法。在此方法中,您可以使用您选择的任何通知类发送通知。密码重置$token
是该方法收到的第一个参数,请参阅自定义文档
/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
Hope this helps!
希望这可以帮助!
回答by Valdrinium
In Laravel 5.7
the default implementation is similar to this:
在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.'));
All you have to do is change your locale
from config/app.php
for example to ro
, then in your resources/lang
, create a file ro.json
similar to this:
您所要做的就是将您的locale
fromconfig/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)"
}
It will translate both the subject (first key) and the mail body.
它将翻译主题(第一个键)和邮件正文。
UPDATE for Laravel 6.*
This can be also used for VerifyEmail.php
notification.
Laravel 6.* 更新
这也可用于VerifyEmail.php
通知。
回答by Above The Law
To everyone asking how to update the Hello, Regards, and subcopy text:
对于询问如何更新 Hello、Regards 和 subcopy 文本的每个人:
php artisan vendor:publish
(option 11)
php artisan vendor:publish
(选项 11)
then in views/vendor/notifications/email.blade.php
然后在 views/vendor/notifications/email.blade.php
In this file there will be the text like Hello, wich you can change by changing:
for example:
line 9# @lang('Hallo!, Hei!, Bonjour!, Guten Tag!, Geia!')
在这个文件中会有像你好这样的文本,你可以通过改变来改变:例如:第 9 行# @lang('Hallo!, Hei!, Bonjour!, Guten Tag!, Geia!')
回答by jycr753
You can create a custom function that will create the reset password token like this.
您可以创建一个自定义函数来创建这样的重置密码令牌。
$user = User::where('email', '[email protected]' )->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.
回答by Alfredo Vazquez
Just add the line:
只需添加以下行:
->subject('New Subjetc')
->subject('New Subjetc')
in the the method toMail of the file Illuminate\Auth\Notifications\ResetPassword like this:
在文件 Illuminate\Auth\Notifications\ResetPassword 的 toMail 方法中,像这样:
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.');
}