如何通过数据库队列系统发送laravel 10 fortify重置密码电子邮件?

问题描述 投票:0回答:1

我使用 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

laravel fortify laravel-queue laravel-notification laravel-fortify
1个回答
0
投票

修复了这些更改。

  1. 创建通知表
php artisan notifications:table
 
php artisan migrate
  1. 创建自定义通知类
<?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);
    }
}

  1. 更新模型\User.php
public function sendPasswordResetNotification($token)
{
    $this->notify(new ResetEmailNotificationQueued($token));
}
  1. 运行队列
php artisan queue:listen --queue=default,send-email

干杯:)。

© www.soinside.com 2019 - 2024. All rights reserved.