如何在Laravel 10 + Laravel Breeze中修改验证邮件内容
一、优先修改默认文本(无需自定义视图)
你找不到对应blade文件是因为Laravel默认的邮箱验证通知用的是MailMessage组件生成的邮件,文本内容来自语言文件,而非独立的blade视图。
复制框架自带的验证语言文件到项目目录:
如果你的项目还没有中文语言包,先复制vendor目录里的语言文件:cp -r vendor/laravel/framework/src/Illuminate/Auth/lang/zh-CN resources/lang/(英文环境对应路径为
vendor/laravel/framework/src/Illuminate/Auth/lang/en)修改语言文件内容:
打开resources/lang/zh-CN/auth.php,找到verify_email数组修改对应字段即可:'verify_email' => [ 'subject' => '验证邮箱地址', 'line' => '请点击下方按钮验证你的邮箱地址。', 'action' => '验证邮箱地址', 'line2' => '如果你没有创建账号,无需进行任何操作。', ],
二、自定义邮件视图(完全自定义内容)
如果需要更灵活的自定义(比如修改排版、添加额外内容),可以替换默认的通知类:
创建自定义的邮箱验证通知类:
在app/Notifications目录下新建CustomEmailVerificationNotification.php,内容如下:<?php namespace App\Notifications; use Illuminate\Auth\Notifications\VerifyEmail; use Illuminate\Notifications\Messages\MailMessage; class CustomEmailVerificationNotification extends VerifyEmail { public function toMail($notifiable) { $verificationUrl = $this->verificationUrl($notifiable); return (new MailMessage) ->subject('自定义邮箱验证标题') ->markdown('emails.verify-email', [ 'url' => $verificationUrl, 'name' => $notifiable->name ]); } }创建自定义的blade视图:
在resources/views/emails目录下新建verify-email.blade.php,编写自定义邮件内容,示例:@component('mail::message') # 欢迎注册,{{ $name }}! 请点击下方按钮验证你的邮箱: @component('mail::button', ['url' => $url]) 验证邮箱 @endcomponent 如果按钮无法点击,请复制以下链接到浏览器打开: {{ $url }} 感谢使用我们的服务! @endcomponent在User模型中替换默认的通知方法:
打开app/Models/User.php,重写sendEmailVerificationNotification方法:use App\Notifications\CustomEmailVerificationNotification; public function sendEmailVerificationNotification() { $this->notify(new CustomEmailVerificationNotification); }
三、补充说明
你之前执行的php artisan vendor:publish --tag=laravel-mail是发布Laravel的基础邮件组件模板(比如mail::message、mail::button的样式),仅需修改基础组件样式时才需要执行,发布后视图会存放在resources/views/vendor/mail目录下。
内容的提问来源于stack exchange,提问作者marcin2137

