Modify password reset notification

Published on December 12th, 2019

When you want to customize the standard Laravel password reset notification the official documentation recommends you to overwrite the sendPasswordResetNotification() method on your User model and just reference a different notification.

public function sendPasswordResetNotification($token)
{
    $this->notify(new CustomResetNotification($token));
}

There is a much easier way to modify the standard notification without creating a completely new notification. Turns out the Illuminate\Auth\Notifications\ResetPassword notification does have a toMailUsing() method which can be used to alter the email subject, text, action etc.

/**
  * Set a callback that should be used when building the notification mail message.
  *
  * @param  \Closure  $callback
  * @return void
  */
public static function toMailUsing($callback)
{
    static::$toMailCallback = $callback;
}

To use your customized password reset notification you can call toMailUsing() and pass in a callback with the parameters $notifiable and $token.

// e.g. AppServiceProvider
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Notifications\Messages\MailMessage;

public function boot()
{
  ResetPassword::toMailUsing(function ($notifiable, $token) {
    return (new MailMessage)
      ->subject('custom subject')
      ->line('custom line')
      ->action(Lang::get('Reset Password'), url(config('app.url').route('password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
      ->line('another line');
  });
}

How it works

By calling the toMailUsing() method your callback is being saved to the ResetPassword::$toMailUsing variable. When the toMail() method is called it is checked whether a callback exists or not. If it exists your callback is called otherwise the standard notification will be build and returned.

/**
  * Build the mail representation of the notification.
  *
  * @param  mixed  $notifiable
  * @return \Illuminate\Notifications\Messages\MailMessage
  */
public function toMail($notifiable)
{
    if (static::$toMailCallback) {
        return call_user_func(static::$toMailCallback, $notifiable, $this->token);
    }

    return (new MailMessage)
        ->subject(Lang::get('Reset Password Notification'))
        ->line(Lang::get('You are receiving this email because we received a password reset request for your account.'))
        ->action(Lang::get('Reset Password'), url(config('app.url').route('password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
        ->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')]))
        ->line(Lang::get('If you did not request a password reset, no further action is required.'));
}

Thanks for making it so far! I appreciate you taking the time to read through the article. To provide you with higher quality articles I need your feedback on how to write better articles. Did you enjoy the read? Do you have tips on what I could make better? Please tag me on twitter @krishankoenig or write me a mail to krishan.koenig@gmail.com.

Sign up for my newsletter

If you enjoyed this article, sign up for my newsletter to be the first to know when a new article is published.