Modify Password Reset Email text in Laravel

Last Updated at : February 11, 2018 By - Usama Muneer | 4995

    If you’re using Laravel‘s default authentication for your user management, but wanted to change URI’s and text in the default Password Reset email. Well There’s a way out we can achieve that in Laravel.

    Default Layout & Text

    default-email

    Create a new Notification

    Let’s start by creating a new Notification for our new custom emailer with laravel artisan CLI.

    php artisan make:notification MailResetPasswordNotification

    You can now find this file under app\Notifications. Right into the toMail method, we can make our modifications.

    public function toMail( $notifiable ) {
       $link = url( "/password/reset/?token=" . $this->token );
    
       return ( new MailMessage )
          ->view('reset.emailer')
          ->from('info@example.com')
          ->subject( 'Reset your password' )
          ->line( "Hey, We've successfully changed the text " )
          ->action( 'Reset Password', $link )
          ->attach('reset.attachment')
          ->line( 'Thank you!' );
    }

    Here we’ve modified the action URI, added the view to be loaded for our email, added a couple of lines and an attachment for our Reset email to look more cool.

    Override the sendPasswordResetNotification method

    By default, User model uses the canResetPassword trait for resetting the passwords, you can find this file under vendor/laravel/framework/src/illuminate/Auth/Passwords/CanResetPassword.php

    Now Its time to tell our User.php to use our custom notification instead of the default one. Let’s overrideĀ sendPasswordResetNotification.

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new App\Notifications\MailResetPasswordNotification($token));
    }

    Repeat the password recovery steps again & these changes should reflect now in the email.

     

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Previously Localization in Laravel was done by creating separate directories for each locales and place in under resources/lang/ and add key value pairs to every single keyword in a number of files as per their modules.

    Another #LaraconUS in the books & It was the 6th instalment of #LaraconUS happened at Chicago with full swing & we got a lot to learn this time with exceptional line of speakers and never-ending community support by fellow Artisans

    Laravel is one of a famous and widely used PHP framework which is growing with immense pace since its version 4.2. Learning Laravel & Setting up your application locally is not much of a difficult task but when it comes to deployment, developers often find it a bit tricky to handle.

    There are several PHP frameworks for a web development project. Among them Laravel is regarded as the best open-source PHP web framework.