Customize Reset Password Mail in Laravel

Ariel Mejia - Mar 13 - - Dev Community

Overview

Laravel provides by default a forgot password feature for User model, you can find it in authorizable alias that extends to User model:

class User extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail;
}
Enter fullscreen mode Exit fullscreen mode

User model has a method that send the notification to reset password:

sendPasswordResetNotification($token)
Enter fullscreen mode Exit fullscreen mode

We can override it to handle or custom messages.

Customizing Reset Password notification

First, we are going to create or own notification that would extend from the default ResetPassword notification from Laravel:

In terminal create a new notification:

php artisan make:notification ResetPassword
Enter fullscreen mode Exit fullscreen mode

Then we are going to override sendPasswordResetNotification method in User model:

use App\Notifications\ResetPassword;

class User
{
    // ...

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }
}
Enter fullscreen mode Exit fullscreen mode

Just make sure that you import our custom notification.

Customize notification

In this example for the sake of simplicity I am going to extend from the default resetPassword from Laravel

Eg: replace a subject message:

    protected function buildMailMessage($url)
    {
        return (new MailMessage)->subject('Here a custom subject');
    }
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .