20

How to customize the path of the reset email blade template in Laravel 5.3?

The template used is: vendor/laravel/framework/src/Illuminate/Notifications/resources/views/email.blade.php

I'd like to build my own.

Also, how to change the text of this email predefined in: vendor/laravel/framework/src/Illuminate/Auth/Notifications/ResetPassword.php

public function toMail()
{
    return (new MailMessage)
        ->line([
            'You are receiving this email because we received a password reset request for your account.',
            'Click the button below to reset your password:',
        ])
        ->action('Reset Password', url('password/reset', $this->token))
        ->line('If you did not request a password reset, no further action is required.');
}

3 Answers 3

41

To change template you should use artisan command php artisan vendor:publish it will create blade templates in your resources/views/vendor directory. To change text of email you should override the sendPasswordResetNotification method on your User model. This is described here https://laravel.com/docs/5.3/passwords in Reset Email Customization section.

You must add new method to your User model.

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

and use your own class for notification instead ResetPasswordNotification.

UPDATED: for @lewis4u request

Step by step instruction:

  1. To create a new Notification class, you must use this command line php artisan make:notification MyResetPassword . It will make a new Notification Class 'MyResetPassword' at app/Notifications directory.

  2. add use App\Notifications\MyResetPassword; to your User model

  3. add new method to your User model.

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new MyResetPassword($token));
    }
    
  4. run php artisan command php artisan vendor:publish --tag=laravel-notifications After running this command, the mail notification templates will be located in the resources/views/vendor/notifications directory.

  5. Edit your MyResetPassword class method toMail() if you want to. It's described here https://laravel.com/docs/5.3/notifications

  6. Edit your email blade template if you want to. It's resources/views/vendor/notifications/email.blade.php

Bonus: Laracast video: https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/9

PS: Thanks @Garric15 for suggestion about php artisan make:notification

1
  • 1
    Also, to create a new Notification class, you must use this command line php artisan make:notification MyOwnResetPassword More here
    – Garric
    Commented Sep 21, 2016 at 14:35
14

I wanted to elaborate on a very helpful Eugen’s answer, but didn’t have enough reputation to leave a comment.

In case you like to have your own directory structure, you don’t have to use Blade templates published to views/vendor/notifications/... When you create a new Notification class and start building your MailMessage class, it has a view() method that you can use to override default views:

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
        ->view('emails.password_reset');
        // resources/views/emails/password_reset.blade.php will be used instead.
}
0
0

In Addition to the above answer for Laravel 5.6, here it is easier to pass variables in an array to your custom email template.

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
$url = url('/invoice/'.$this->invoice->id);

return (new MailMessage)
            ->subject('Invoice Paid')
            ->markdown('emails.password_reset', ['url' => $url]);

}

https://laravel.com/docs/5.6/notifications

Not the answer you're looking for? Browse other questions tagged or ask your own question.