laravel 用戶發送郵件重置密碼

laravel5.3 重置密碼

因爲重置密碼須要發送郵件,而laravel默認使用國外的驅動,因此須要改爲國內的驅動,使用sendcloudphp

第一步:忘記密碼

首先看到的是App\Http\Controllers\Auth\ForgotPasswordController.php的代碼laravel

use SendsPasswordResetEmails;  //使用這個trait顯示發送郵件,和發送郵件

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

看到是如此乾淨,看來只有可能在SendsPasswordResetEmailsapp

第二部:發送郵件

再上一步咱們知道,用到了一個SendsPasswordResetEmails trait,裏面有兩個重要的方法:ui

//顯示視圖
     public function showLinkRequestForm()
        {
            return view('auth.passwords.email');
        }


  
      public function sendResetLinkEmail(Request $request)
        {
             //驗證郵箱地址是否有效
            $this->validate($request, ['email' => 'required|email']);
    
     
            //發送郵件  sendResetLink是PasswordBroker 裏面的一個方法
            $response = $this->broker()->sendResetLink(
                $request->only('email')
            );
            //根據發送郵件返回狀態 返回相應
            return $response == Password::RESET_LINK_SENT
                        ? $this->sendResetLinkResponse($response)
                        : $this->sendResetLinkFailedResponse($request, $response);
        }
        
         /**
         * Get the broker to be used during password reset.
         *
         * @return \Illuminate\Contracts\Auth\PasswordBroker 看這個返回值
         */
        public function broker()
        {
            return Password::broker();
        }

能夠看到\Illuminate\Contracts\Auth\PasswordBroker這個接口實例是Illuminate\Auth\PasswordsPasswordBroker.php這個文件,找到上面發送郵件使用的一個方法sendResetLink,以下this

public function sendResetLink(array $credentials)
    {
    
        //查找用戶信息
        $user = $this->getUser($credentials);

        if (is_null($user)) {
            return static::INVALID_USER;
        }

        //真正發送郵件的地方,發現調用的User.php 裏的方法
        $user->sendPasswordResetNotification(
            $this->tokens->create($user)
        );

        return static::RESET_LINK_SENT;
    }

第三步:用戶數據模型

上一步看到發送郵件使用的是User.php這個model 裏的 sendPasswordResetNotification方法
url

發現並無這個方法,看到繼承Authenticatable,進去看看:
spa

發現了重大新聞,這個地方用了個trait CanResetPassword,看下代碼:3d

//找到第二步中調用的方法了
 public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token));//notify 又是什麼鬼,暫時沒有注意到
    }

看下ResetPasswordNotification,發現有個發送郵件的方法:
code

public function toMail($notifiable)
    {
        return (new MailMessage)
            ->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.');
    }
   // url('password/reset', $this->token)) 這個重寫的時候用的上

這樣看來,咱們重現一下User.php中的這個方法就應該能夠改寫laravel默認發送郵件的方式了orm

public function sendPasswordResetNotification($token)
    {
        // 模板變量
        $data = [
            'url' => url('password/reset', $token), //這個地址是Illuminate\Auth\Notifications\ResetPassword 中方法toMail 裏的url,複製過來就能夠
            'name'=>$this->name,
        ];
        $template = new SendCloudTemplate( 'zhihu_app_register', $data );

        Mail::raw( $template, function ( $message ) {
            $message->from( 'xxxx@qq.com', 'xxxx'  );

            $message->to( $this->email );
        } );
    }
}

這樣咱們就改了默認重置密碼發送郵件的方法了

還有不少細節,等有空再去細摳,weaving love

相關文章
相關標籤/搜索