用自定义模板 Laravel 5.3 替换密码重置邮件模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41400395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:35:18  来源:igfitidea点击:

replace password reset mail template with custom template laravel 5.3

laravel

提问by BOTJr.

I did the laravel command for authentication system , php artisan make:authit made the authentication system for my app and almost everything is working.

我为身份验证系统执行了 laravel 命令,php artisan make:auth它为我的应用程序创建了身份验证系统,并且几乎一切正常。

Now when i use the forgot password and it sends me a token to my mail id , i see that the template contains laravel and some other things that i might wanna edit or ommit, to be precise , i want my custom template to be used there.

现在,当我使用忘记的密码并向我发送一个令牌到我的邮件 ID 时,我看到模板包含 laravel 和一些我可能想要编辑或省略的其他内容,准确地说,我希望在那里使用我的自定义模板.

I looked up at the controllers and their source files but i can't find the template or the code that is displaying the html in the mail.

我查看了控制器及其源文件,但找不到模板或在邮件中显示 html 的代码。

How do i do it ?

我该怎么做 ?

How do i change it?

我该如何更改?

This is the default template that comes from laravel to the mail. enter image description here

这是从 laravel 到邮件的默认模板。 在此处输入图片说明

回答by camelCase

Just a heads up: In addition to the previous answer, there are additional steps if you want to modify the notification lineslike You are receiving this..., etc. Below is a step-by-step guide.

提醒一下:除了前面的答案,如果您想修改通知行,You are receiving this...等,还有其他步骤。以下是分步指南。

You'll need to override the defaultsendPasswordResetNotificationmethod on your Usermodel.

您需要覆盖模型sendPasswordResetNotification的默认方法User

Why? Because the lines are pulled from Illuminate\Auth\Notifications\ResetPassword.php. Modifying it in the core will mean your changes are lost during an update of Laravel.

为什么?因为这些线是从Illuminate\Auth\Notifications\ResetPassword.php. 在核心中修改它意味着您的更改在 Laravel 更新期间丢失。

To do this, add the following to your your Usermodel.

为此,请将以下内容添加到您的User模型中。

use App\Notifications\PasswordReset; // Or the location that you store your notifications (this is default).

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

Lastly, create that notification:

最后,创建该通知

php artisan make:notification PasswordReset

And example of this notification's content:

以及此通知内容的示例:

/**
 * The password reset token.
 *
 * @var string
 */
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'];
}

/**
 * Build the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
        ->line('You are receiving this email because we received a password reset request for your account.') // Here are the lines you can safely override
        ->action('Reset Password', url('password/reset', $this->token))
        ->line('If you did not request a password reset, no further action is required.');
}

回答by Farzin Farzanehnia

Run the following command in the terminal and the two email templates will be copied to your resources/vendor/notifications folder. Then you can modify the templates.

在终端中运行以下命令,两个电子邮件模板将被复制到您的资源/供应商/通知文件夹。然后您可以修改模板。

php artisan vendor:publish --tag=laravel-notifications

You can read more about Notificationsin Laravel Docs.

您可以Notifications在 Laravel Docs 中阅读更多相关信息。

回答by F.E Noel Nfebe

You can also achieve this by building your own mail template and sending the Reset link yourself using the php mail()or or Laravel Mail Facadebut first you will need to create the reset token

您也可以通过构建自己的邮件模板并使用 phpmail()或 Laravel自己发送重置链接来实现此目的,Mail Facade但首先您需要创建重置令牌

1) use Illuminate\Contracts\Auth\PasswordBroker;

1) use Illuminate\Contracts\Auth\PasswordBroker;

  $user = User::where('email', $email)->first();
                 if ($user) {
                    //so we can have dependency 
                    $password_broker = app(PasswordBroker::class);
                    //create reset password token 
                    $token = $password_broker->createToken($user); 

                    DB::table('password_resets')->insert(['email' => $user->email, 'token' => $token, 'created_at' => new Carbon]); 

//Send the reset token with your own template
//It can be like $link = url('/').'/password/reset/'.$token;

                }

回答by Syamsoul Azrien

I ended up using Mailfacade in Usermodel..

我最终MailUser模型中使用了外观..

public function sendPasswordResetNotification($token){
    // $this->notify(new MyCustomResetPasswordNotification($token)); <--- remove this, use Mail instead like below

    $data = [
        $this->email
    ];

    Mail::send('email.reset-password', [
        'fullname'      => $this->fullname,
        'reset_url'     => route('user.password.reset', ['token' => $token, 'email' => $this->email]),
    ], function($message) use($data){
        $message->subject('Reset Password Request');
        $message->to($data[0]);
    });
}

回答by Hemant Kumar

If you wish to modify the mail template then check Laravel markdown, here you can change the default mail template:

如果您想修改邮件模板,请检查 Laravel markdown,您可以在此处更改默认邮件模板:

if you do want to get HTML and be able to edit it, run this:

如果您确实想获取 HTML 并能够对其进行编辑,请运行以下命令:

php artisan vendor:publish --tag=laravel-mail

This will happen:

这会发生:

Copied Directory [/vendor/laravel/framework/src/Illuminate/Mail/resources/views] To [/resources/views/vendor/mail]

复制目录 [/vendor/laravel/framework/src/Illuminate/Mail/resources/views] 到 [/resources/views/vendor/mail]

Resource: https://laraveldaily.com/mail-notifications-customize-templates/

资源:https: //laraveldaily.com/mail-notifications-customize-templates/