Fortify - How to customise verification / password reset emails?

Viewed 3779

I'm in the process of implementing fortify into my app. I'm really confused about customising the default emails that are produced when you hit the password-reset / verifty-email routes?

I could edit them in vendor but that going to cause me an issue every time I update.

There must be a hook to provide an alternative email template.

Unfortnatly I cant find any documentation that explains how its done.

Do I need to add:

public function sendEmailVerificationNotification()
{


}

To my user model? If so how to I generate the return verificaiton URL as its not being held in the database?

Any help would be great.

Thanks!

3 Answers

Here is the solution in Laravel 8.

1.) Create a Notification by command

php artisan make:notification ResetPasswordNotification
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;

class ResetPasswordNotification extends ResetPassword
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public $token;

    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get 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);
        }

        $url = url(config('app.url') . route('password.reset', [
            'token' => $this->token,
            'email' => $notifiable->getEmailForPasswordReset(),
        ], false));

        return (new MailMessage)
            ->view(
                'emails.reset_password', ['name'=>$notifiable->name,'url' => $url]
            )
            ->subject(Lang::get('Reset Password'));
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

2.) In the app/Models/User.php Model. Add below method.

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

3.) Now create an email template in views/emails/reset_password.blade.php

Hi {{ $name }}, Please reset your password here. Click on the below link to reset the password.
 <a href="{{ $url }}">RESET</a>

You can customize the password reset email by adding the following in FortifyServiceProvider

ResetPassword::toMailUsing(function($user, string $token) {
    return (new MailMessage)
        ->subject('Reset Password')
        ->view('emails.password_reset', [
            'user' => $user,
            'url' => sprintf('%s/users/password_reset/%s', config('app.url'), $token)
    ]);
});

Create a file named resources/views/emails/password_reset.blade.php, in this file you can use $user and $url

You can enter the directory when you use fortify vendor\laravel\framework\src\Illuminate\ Notifications\resources\views\email.blade.php and overwrite the content and style of the email.

Notifications are by default in the directory vendor\laravel\framework\src\Illuminate\Auth\Notifications\VerifyEmail.php

You can change the email text lines there. Similarly reset password in ResetPassword.php

Related