DEV Community

Ariel Mejia
Ariel Mejia

Posted on

Customize Reset Password Mail in Laravel

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

Top comments (1)

Collapse
 
sourovpal profile image
Info Comment hidden by post author - thread only accessible via permalink
Sourov Pal

Hi,
This is Sourov Pal. I am a freelance web developer and Software Developer. I can do one of project for free. If you like my work you will pay me otherwise you don't need to pay. No upfront needed, no contract needed. If you want to outsource your work to me you may knock me.

My what's app no is: +8801919852044
Github Profile: github.com/sourovpal
Thanks

Some comments have been hidden by the post's author - find out more