Laravel Jetstream配合AWS SES时如何修改Fortify密码重置邮件发件人地址
解决方案
以下两种方案均适配Laravel 8 + Fortify运行环境,可强制指定密码重置邮件的发件人地址:
方式一:自定义重置密码通知(推荐,灵活度最高)
- 生成自定义通知类,执行命令:
php artisan make:notification ResetPasswordCustom - 修改自定义通知内容
打开app/Notifications/ResetPasswordCustom.php,替换为以下代码:
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; class ResetPasswordCustom extends Notification { use Queueable; public $token; public function __construct($token) { $this->token = $token; } public function via($notifiable) { return ['mail']; } public function toMail($notifiable) { $resetUrl = url(route('password.reset', [ 'token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset(), ], false)); return (new MailMessage) // 此处强制指定已通过SES验证的发件人 ->from('info@openlabvr.com', '你的站点名称') ->subject('重置账户密码') ->line('你收到这封邮件是因为我们收到了你的账户密码重置请求。') ->action('重置密码', $resetUrl) ->line('该重置链接有效期为60分钟,如果你没有发起密码重置请求,无需进行任何操作。'); } public function toArray($notifiable) { return []; } }
- 覆盖用户模型的密码重置通知调用逻辑
打开app/Models/User.php,添加以下方法:
use App\Notifications\ResetPasswordCustom; // 原有代码保持不变 public function sendPasswordResetNotification($token) { $this->notify(new ResetPasswordCustom($token)); }
方式二:通过服务提供者全局重置默认逻辑(无需修改用户模型)
打开app/Providers/AppServiceProvider.php,在boot方法中添加以下代码:
use Illuminate\Auth\Notifications\ResetPassword; use Illuminate\Notifications\Messages\MailMessage; // 原有代码保持不变 public function boot() { ResetPassword::toMailUsing(function ($notifiable, $token) { $resetUrl = url(route('password.reset', [ 'token' => $token, 'email' => $notifiable->getEmailForPasswordReset(), ])); return (new MailMessage) ->from('info@openlabvr.com', '你的站点名称') ->subject('重置账户密码') ->line('你收到这封邮件是因为我们收到了你的账户密码重置请求。') ->action('重置密码', $resetUrl) ->line('该重置链接有效期为60分钟,如果你没有发起密码重置请求,无需进行任何操作。'); }); }
生效操作
修改完成后执行以下命令清空缓存即可:php artisan config:clearphp artisan view:clear
内容的提问来源于stack exchange,提问作者pperuzzo
相关产品推荐
相关产品推荐

