简体   繁体   English

在Laravel 5.4中自定义忘记密码电子邮件

[英]Customize Forgotten Password Email in Laravel 5.4

I am trying to customize password reset email in Laravel. 我想在Laravel中自定义密码重置电子邮件。

I have to override this function: 我必须覆盖此功能:

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

}

This is my attempt: 这是我的尝试:

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

I get this error: 我收到此错误:

Declaration of Illuminate\\Foundation\\Auth\\User::sendPasswordResetNotification($token, Illuminate\\Http\\Request $request) must be compatible with Illuminate\\Contracts\\Auth\\CanResetPassword::sendPasswordResetNotification($token) Illuminate声明\\ Foundation \\ Auth \\ User :: sendPasswordResetNotification($ token,Illuminate \\ Http \\ Request $ request)必须与Illuminate \\ Contracts \\ Auth \\ CanResetPassword :: sendPasswordResetNotification($ token)兼容

If you read the error, it's telling you your class is not compatible with CanResetPassword . 如果你读错了,它会告诉你你的班级与CanResetPassword不兼容。 If you look at that.... 如果你看那......

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

You can see the function sendPasswordResetNotification should only take one parameter, $token . 您可以看到函数sendPasswordResetNotification只应该使用一个参数$token So you need to remove Request $request as a parameter from the method's signature. 因此,您需要从方法的签名中删除Request $request作为参数。

In order to get the request, you will want to use the function request() inside the sendPasswordResetNotification method. 为了获取请求,您需要在sendPasswordResetNotification方法中使用函数request()

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

I'm surprised you're going to that length to customize the email. 我很惊讶你要花那么长时间来定制电子邮件。

Try this instead: 试试这个:

php artisan vendor:publish

Then modify the file here 然后在这里修改文件

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

Works great for our usage. 适合我们的使用。

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.

Now if you NEED to change the copy and you want the fancy button that the original ResetPassword class uses, you can extend the mail class in your User.php class like the following example. 现在,如果您需要更改副本并且您想要原始ResetPassword类使用的花哨按钮,则可以在User.php类中扩展邮件类,如下例所示。

Here's a copy of ours that works great AS AN EXAMPLE ONLY: 这是我们的副本,仅作为示例:

<?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