2016-12-30 28 views
5

我做了認證系統的laravel命令,php artisan make:auth它爲我的應用製作了認證系統,幾乎所有的東西都在工作。用自定義模板laravel替換密碼重置郵件模板5.3

現在,當我使用忘記的密碼,它向我的郵件ID發送一個令牌時,我看到該模板包含laravel和其他一些我可能想要編輯或省略的事情,具體來說,我希望自定義模板在那裏使用。

我擡頭看着控制器及其源文件,但我找不到在郵件中顯示html的模板或代碼。

我該怎麼做?

我該如何改變它?

這是從laravel到郵件的默認模板。 enter image description here

回答

4

在終端中運行以下命令,這兩個電子郵件模板將被複制到您的資源/供應商/通知文件夾。然後你可以修改模板。

php artisan vendor:publish --tag=laravel-notifications 

您可以在Laravel Docs閱讀更多關於Notifications

+0

電子郵件模板可將內部資源/視圖/供應商/通知文件夾中。 – KCP

+0

我認爲完整的答案是https://stackoverflow.com/a/41401524/2144424 – jpussacq

7

只是擡起頭來:除了以前的答案,還有額外的步驟,如果你想修改通知行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)); 
} 

最後,create that notification

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

謝謝。這是完整的答案。我需要添加以下行 - >主題('您的自定義主題')來更改電子郵件的主題。非常感謝。 – jpussacq

0

您還可以通過建立自己的郵件模板和發送復位使用鏈接自己實現這一目標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; 

       }