1 頁 (共 1 頁)

Changing the Laravel 5.3 password reset email text

發表於 : 2017-10-18 10:42:57
yehlu
http://novate.co.uk/changing-the-larave ... mail-text/

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
[9 ] Tag: laravel-notifications
and then edit the files in

代碼: 選擇全部

/resources/views/vendor/notifications