簡體   English   中英

在Laravel 5.4中自定義忘記密碼電子郵件

[英]Customize Forgotten Password Email in Laravel 5.4

我想在Laravel中自定義密碼重置電子郵件。

我必須覆蓋此功能:

namespace Illuminate\Auth\Passwords;

use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
use Illuminate\Http\Request;


trait CanResetPassword
{
    /**
     * Get the e-mail address where password reset links are sent.
     *
     * @return string
     */
    public function getEmailForPasswordReset()
    {
        return $this->email;
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */

public function sendPasswordResetNotification($token)
{

    $this->notify(new ResetPasswordNotification($token));

}

這是我的嘗試:

 public function sendPasswordResetNotification($token, Requests $request)
{
Mail::to($request->email)->send(new newpassword($token));
}

我收到此錯誤:

Illuminate聲明\\ Foundation \\ Auth \\ User :: sendPasswordResetNotification($ token,Illuminate \\ Http \\ Request $ request)必須與Illuminate \\ Contracts \\ Auth \\ CanResetPassword :: sendPasswordResetNotification($ token)兼容

如果你讀錯了,它會告訴你你的班級與CanResetPassword不兼容。 如果你看那......

interface CanResetPassword
{
    /**
     * Get the e-mail address where password reset links are sent.
     *
     * @return string
     */
    public function getEmailForPasswordReset();
    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token);
}

您可以看到函數sendPasswordResetNotification只應該使用一個參數$token 因此,您需要從方法的簽名中刪除Request $request作為參數。

為了獲取請求,您需要在sendPasswordResetNotification方法中使用函數request()

public function sendPasswordResetNotification($token)
{
    Mail::to(request()->email)->send(new newpassword($token));
}

我很驚訝你要花那么長時間來定制電子郵件。

試試這個:

php artisan vendor:publish

然后在這里修改文件

/resources/views/vendor/notifications/email.blade.php

適合我們的使用。

user@default:~/laravel_5.4$ php artisan vendor:publish
Copied Directory [/vendor/laravel/framework/src/Illuminate/Pagination/resources/views] To [/resources/views/vendor/pagination]
Copied Directory [/vendor/laravel/framework/src/Illuminate/Notifications/resources/views] To [/resources/views/vendor/notifications]
Copied Directory [/vendor/laravel/framework/src/Illuminate/Mail/resources/views] To [/resources/views/vendor/mail]
Publishing complete.

現在,如果您需要更改副本並且您想要原始ResetPassword類使用的花哨按鈕,則可以在User.php類中擴展郵件類,如下例所示。

這是我們的副本,僅作為示例:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Notifications\Messages\MailMessage;

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'Users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'firstName',
        'lastName',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

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

class CustomPassword extends ResetPassword
{
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('We are sending this email because we recieved a forgot password request.')
            ->action('Reset Password', url(config('app.url') . route('password.reset', $this->token, false)))
            ->line('If you did not request a password reset, no further action is required. Please contact us if you did not submit this request.');
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM