Changing the Laravel 5.3 password reset email text


passwordreset
Problem:  You use the out of the box authentication and password reset code.  The email that is sent to the user is in English, but you need it in another language.

You know that you should never edit code that is in the vendor folder, so what do you do?

Thankfully, Taylor included a hook where we can write our own mailable notification, and the password broker provides the required token.

Assuming you have working boilerplate auth functions but you need to change the text of the password reset email as easily as possible;

Create a notification

php artisan make:notification MailResetPasswordToken

edit this file which you find in a new folder App\Notifications

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class MailResetPasswordToken extends Notification
{
    use Queueable;

    public $token;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    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)
    {
        return (new MailMessage)
                    ->subject("Reset your password")
                    ->line("Hey, did you forget your password? Click the button to reset it.")
                    ->action('Reset Password', url('password/reset', $this->token))
                    ->line('Thankyou for being a friend');
    }

}

 

Override the send password reset trait with your local implementation in your User.php user model

    /**
     * Send a password reset email to the user
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new MailResetPasswordToken($token));
    }

remembering to import the class at the top of the user model

use App\Notifications\MailResetPasswordToken;

If you need to alter the layout of the message, including the text “If you’re having trouble clicking the “Reset Password” button,” then you need to run

php artisan vendor:publish

and then edit the files in

/resources/views/vendor/notifications

Leave a comment

Your email address will not be published. Required fields are marked *

6 thoughts on “Changing the Laravel 5.3 password reset email text