简体   繁体   English

如何自定义 Laravel 的邮件模板

[英]How to customize email template of Laravel

how can I edit the email template that is being sent when resetting passwords or verifying the email addresses.如何编辑在重置密码或验证电子邮件地址时发送的电子邮件模板。 Image of the sample email示例电子邮件的图片

You should change the default Laravel sendPasswordResetNotification method on your User model.您应该更改 User 模型上的默认 Laravel sendPasswordResetNotification 方法。

Because the lines are come from Illuminate\\Auth\\Notifications\\ResetPassword.php.因为这些行来自 Illuminate\\Auth\\Notifications\\ResetPassword.php。 Never you must change this facade.永远不要改变这个外观。 If you do it you can lost changes during an update of Laravel.如果这样做,您可能会在 Laravel 更新期间丢失更改。

Instead of it do this, add the following to your your User model.而不是这样做,将以下内容添加到您的用户模型中。

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));
}

Create 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.');
}

Import email template using the following command使用以下命令导入电子邮件模板

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

Then you can edit your html/text template in this folder然后你可以在这个文件夹中编辑你的 html/text 模板

resources/views/vendor/mail

https://laravel.com/docs/8.x/mail#customizing-the-componentshttps://laravel.com/docs/8.x/mail#customizing-the-components

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM