How to change reset password email subject in lara

2019-01-22 00:51发布

问题:

I am beginner in Laravel. Currently I am learning this framework. My curent Laravel version is 5.3.

I am scaffolding my auth by using php artisan make:auth All are working fine. Also I configured gmail smtp in my .env file and mail.php in config directgory. All are perfectly working. But I saw by-default the forgot password email subject is going Reset Password. I want to change that.

I saw some blog. I found some blog. I have implement that in my site. But same output coming.

I followed these links -

https://laracasts.com/discuss/channels/general-discussion/laravel-5-password-reset-link-subject

https://laracasts.com/discuss/channels/general-discussion/reset-password-email-subject

https://laracasts.com/discuss/channels/laravel/how-to-override-message-in-sendresetlinkemail-in-forgotpasswordcontroller

回答1:

You can change your password reset email subject, but it will need some extra work. First, you need to create your own implementation of ResetPassword notification.

Create a new notification class insideapp\Notifications directory, let's named it ResetPassword.php:

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPassword extends Notification
{
    public $token;

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

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Your Reset Password Subject Here')
            ->line('You are receiving this email because we received a password reset request for your account.')
            ->action('Reset Password', url('password/reset', $this->token))
            ->line('If you did not request a password reset, no further action is required.');
    }
}

You can also generate the notification template using artisan command:

php artisan make:notification ResetPassword

Or you can simply copy-paste the above code. As you may notice this notification class is pretty similar with the default Illuminate\Auth\Notifications\ResetPassword. You can actually just extend it from the default ResetPassword class.

The only difference is here, you add a new method call to define the email's subject:

return (new MailMessage)
        ->subject('Your Reset Password Subject Here')

You may read more about Mail Notifications here.

Secondly, on your app\User.php file, you need to override the default sendPasswordResetNotification() method defined by Illuminate\Auth\Passwords\CanResetPassword trait. Now you should use your own ResetPassword implementation:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\ResetPassword as ResetPasswordNotification;

class User extends Authenticatable
{
    use Notifiable;

    ...

    public function sendPasswordResetNotification($token)
    {
        // Your your own implementation.
        $this->notify(new ResetPasswordNotification($token));
    }
}

And now your reset password email subject should be updated!

Hope this help!



回答2:

You may easily modify the notification class used to send the password reset link to the user. To get started, override the sendPasswordResetNotification method on your User model. Within this method, you may send the notification using any notification class you choose. The password reset $token is the first argument received by the method, See the Doc for Customization

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

Hope this helps!



回答3:

You can create a custom function that will create the reset password token like this.

 $user = User::where('email', 'example@name.com' )->first();
 $password_broker = app(PasswordBroker::class); //so we can have dependency injection
 $token = $password_broker->createToken($user); //create reset password token
 $password_broker->emailResetLink($user, $token, function (Message $message) {
         $message->subject('Custom Email title');
 });//send email.


回答4:

Just add the line:

->subject('New Subjetc')

in the the method toMail of the file Illuminate\Auth\Notifications\ResetPassword like this:

public function toMail($notifiable)
{
    return (new MailMessage)
        ->subject('New Subjetc')
        ->line('You are receiving this email because we received a password reset request for your account.')
        ->action('Restaurar Contraseña', url(config('app.url').route('password.reset', $this->token, false)))
        ->line('If you did not request a password reset, no further action is required.');
}