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

Posted

技术标签:

【中文标题】在 Laravel 5.4 中自定义忘记密码的电子邮件【英文标题】:Customize Forgotten Password Email in Laravel 5.4 【发布时间】:2017-07-23 17:43:48 【问题描述】:

我正在尝试在 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)

【问题讨论】:

看看这个答案***.com/questions/40574001/… 【参考方案1】:

如果您阅读了该错误,则说明您的课程与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));

【讨论】:

【参考方案2】:

我很惊讶你会用那么长的时间来自定义电子邮件。

试试这个:

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

【讨论】:

你也确实解决了这个问题***.com/q/40532296/4650866,干得好 感谢您的解决方案。为了改进你的答案,你可以使用php artisan make:notification CustomPassword 命令来创建这个类的脚手架。

以上是关于在 Laravel 5.4 中自定义忘记密码的电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

Laravel - 如何在忘记密码中添加自定义字段

laravel 5.4 密码重置问题——发送邮件

在 Laravel 中的令牌旁边添加电子邮件忘记密码模板电子邮件链接(已解决)

Laravel 5.5 自定义重置密码抛出令牌不匹配

5.4 Go语言中自定义类型与结构体(Struct)

5.4 Go语言中自定义类型与结构体(Struct)