我使用 Laravel 10 作为我的 Fortify API 后端。重置密码时,我想将 HTML 内容(从数据库检索)发送到电子邮件。电子邮件应该排队,可能通过作业表进行排队。
在
FortifyServiceProvider
,我尝试过toMailUsing
的方法。
ResetPassword::toMailUsing(function (User $user, string $token) {
return (new ResetEmail($user, $token))->onQueue('send-email');
});
但这封邮件需要排队直接发送。
仅供参考:我已经在运行
send-email
队列来发送其他电子邮件,并且它工作得很好(通过乔布斯)。是的,我确实在 .env 中设置了这个QUEUE_CONNECTION=database
在 laravel 源代码中我发现了这个:
if ($message instanceof Mailable) {
return $message->send($this->mailer);
}
这也应该处理排队的
Mailable
,对吧?还是我走错方向了?
来源:10.x/src/Illuminate/Notifications/Channels/MailChannel.php#L63
修复了这些更改。
php artisan notifications:table
php artisan migrate
<?php
namespace App\Notifications;
use App\Models\User;
use App\Mail\ResetEmail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
class ResetEmailNotificationQueued extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct(private string $token)
{
$this->onQueue('send-email');
}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
/**
*
* @param User $notifiable
*
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): Mailable
{
return new ResetEmail($notifiable, $this->token);
}
}
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetEmailNotificationQueued($token));
}
php artisan queue:listen --queue=default,send-email
干杯:)。