使用以下命令导入电子邮件模板
php artisan vendor:publish --tag=laravel-mail
然后您可以在此文件夹中编辑您的html/文本模板
resources/views/vendor/mail
https://laravel.com/docs/8.x/mail#customizing-the-components
您应该更改 User 模型上的默认 Laravel sendPasswordResetNotification 方法。
因为这些行来自 Illuminate\Auth\Notifications\ResetPassword.php。你永远不必改变这个外观。如果您这样做,您可能会在 Laravel 更新期间丢失更改。
不要这样做,而是将以下内容添加到您的用户模型中。
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));
}
创建创建该通知:
php artisan make:notification PasswordReset
此通知内容示例:
/**
* 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.');
}
Laravel 有电子邮件和通知的内部模板。 通知模板已集成到电子邮件模板中。因此,通知模板充当邮件的消息组件。请务必注意,
message
模板是 mail
模板的一部分。
要管理、编辑或自定义 Laravel 电子邮件模板,必须首先发布它们。这可以通过运行以下 Artisan 命令来完成:
php artisan vendor:publish --tag=laravel-mail
php artisan vendor:publish --tag=laravel-notifications
Laravel 会将这些文件放置在以下位置:
/resources/views/vendor/mail
/resources/views/vendor/notifications
此时,可以将自定义电子邮件 HTML 模板集成到
/resources/views/vendor/mail/html
中,其中包含以下文件:
layout.blade.php
- 基础布局header.blade.php
- 标题footer.blade.php
- 页脚button.blade.php
- 按钮/操作message.blade.php
- 电子邮件正文(通知文本)/resources/views/vendor/notifications
目录包含一个文件email.blade.php
,用于通知。大多数情况下,不需要修改。
需要注意的是,不应在这些 Blade 文件中添加缩进或制表符,因为它会将 HTML 转换为纯文本。
Laravel 提供了通过使用用户 UUID 而不是内部 ID 的自定义 URL 进行用户电子邮件验证的功能。此功能记录在 Laravel 官方文档中。
通过此命令创建自己的通知类:
php artisan make:notification VerifyEmail
该命令将在
VerifyEmail.php
文件夹中创建 app/Notifications
。更新一下吧:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;
class VerifyEmail extends \Illuminate\Auth\Notifications\VerifyEmail
{
protected function buildMailMessage($url)
{
return (new MailMessage)
->subject('my subject')
->line('text line 1')
->action('please verify!', $url)
->line('text line 2');
}
//$notifiable = User model object
protected function verificationUrl($notifiable)
{
if (static::$createUrlCallback) {
return call_user_func(static::$createUrlCallback, $notifiable);
}
return URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
[
'uuid' => $notifiable->uuid,//custom User model field
'hash' => sha1($notifiable->getEmailForVerification())
]
);
}
}
编辑用户模型
/app/Models/User.php
:
\Illuminate\Contracts\Auth\MustVerifyEmail
接口Illuminate\Notifications\Notifiable
sendEmailVerificationNotification()
方法用户模型类代码:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
/*some code*/
public function sendEmailVerificationNotification()
{
$this->notify(new \App\Notifications\VerifyEmail);
}
}
您还必须在
verification.verify
修改 /routes/web.php
路线。就这样吧。
use Illuminate\Foundation\Auth\EmailVerificationRequest;
// {uuid} instead of {id}
Route::get('/email/verify/{uuid}/{hash}', function (EmailVerificationRequest $request) {
$request->fulfill();
return redirect('/home');
})->middleware(['auth', 'signed'])->name('verification.verify');