2016-11-13 105 views
10

我是Laravel的初学者。目前我正在学习这个框架。我的Laravel版本是5.3。如何更改laravel中的重置密码电子邮件主题?

我脚手架使用我的身份验证php artisan make:auth所有工作正常。另外我在配置directgor​​y中配置了我的.env文件中的gmail smtp和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

https://laracasts.com/discuss/channels/laravel/how-to-override-message-in-sendresetlinkemail-in-forgotpasswordcontroller

回答

25

您可以更改您的密码重置电子邮件主题,但它需要一些额外的工作。首先,您需要创建自己的ResetPassword通知的实现。

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.'); 
    } 
} 

您也可以使用Artisan命令生成的通知模板:

php artisan make:notification ResetPassword 

或者你可以简单地复制粘贴上面的代码。您可能会注意到,此通知类与默认的Illuminate\Auth\Notifications\ResetPassword非常相似。实际上,您可以从默认的ResetPassword类中扩展它。

唯一的区别就在这里,你添加一个新的方法调用来定义邮件的主题:

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)); 
    } 
} 

现在你重设密码电子邮件主题应该更新!

Reset password email subject updated

希望这有助于!

+0

,我们如何才能改变Laravel和Laravel的问题。 – Steve

+1

@Steve转到config/app.php并更改应用程序名称 – kniteli

1

你可以很容易修改用于发送密码重置链接给用户的通知类。要开始使用,请在用户模型上覆盖sendPasswordResetNotification方法。在此方法中,您可以使用您选择的任何通知类发送通知。密码重置$token是该方法获得的第一个参数,请参阅Doc for Customization

/** 
* Send the password reset notification. 
* 
* @param string $token 
* @return void 
*/ 
public function sendPasswordResetNotification($token) 
{ 
    $this->notify(new ResetPasswordNotification($token)); 
} 

希望这有助于!

+0

然后接受一个更简单..! –

2

您可以创建一个自定义函数来创建像这样的重置密码令牌。

$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. 
0

只需添加一行:

- >主题( '新Subjetc')

在文件照亮\身份验证\通知的方法toMail \ 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.'); 
} 
相关问题