Laravel Waitlist:邮件发送
当使用 taldres/laravel-waitlist 的应用需要向等待列表中的用户发送邮件,或为其配置邮件提供商时,使用此技能。
主要目标
- 流程中的每封邮件都通过队列化、加密的监听器发送,携带符合其用途的链接,且只发送给已同意并保持同意的地址。
工作流程
1. 了解何时应发送哪封邮件
该包本身不发送邮件,也不调用任何邮件提供商。它触发事件;应用中的监听器通过任意 Laravel 邮件驱动(Resend、Postmark、SES、SMTP 等)发送邮件。
| 邮件 | 触发时机 | 必须携带 |
|---|---|---|
| 确认邮件(双重确认) | 当 EntrySubscribed 事件中的 $event->requiresConfirmation 为 true | $event->confirmUrl、$event->unsubscribeUrl |
| 偏好设置页链接 | ManageLinkRequested 事件 | $event->manageUrl,仅发送至 $event->entry->email |
| 欢迎邮件(可选) | EntryConfirmed 事件 | $event->unsubscribeUrl、Waitlist::listUnsubscribeHeaders($event->entry) |
| 发布或关于某个列表的更新 | 应用自行决定 | 每个接收者的专属链接,用于退出该列表 |
| 新闻通讯(可选用途) | 应用自行决定 | 每个接收者的专属链接,仅用于退订新闻通讯 |
2. 确认邮件监听器
namespace App\Listeners;
use App\Mail\ConfirmWaitlistMail;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
use LogicException;
use Taldres\Waitlist\Events\EntrySubscribed;
use Taldres\Waitlist\Facades\Waitlist;
class SendWaitlistConfirmationMail implements ShouldQueue, ShouldBeEncrypted
{
public function handle(EntrySubscribed $event): void
{
if (! $event->requiresConfirmation) {
return;
}
$confirmUrl = $event->confirmUrl ?? throw new LogicException('在 urls() 中设置确认页面,或启用包的默认路由。');
Mail::to($event->entry->email)->send(new ConfirmWaitlistMail(
$confirmUrl,
$event->unsubscribeUrl,
$event->subscription->consents->pluck('text')->all(), // 正在被确认的内容
));
Waitlist::confirmationMailed($event->subscription, 'confirm-mail@2026-10');
}
}
- 同一事件也可能携带重新发送的情况(
$event->isNewCycle === false)。不要在监听器中限制频率;包的冷却和上限机制已经处理过了。 confirmationMailed()会在同意记录旁边记录邮件的引用标识;应使用模板名称和版本号,并保存每个版本的文本。- 当项目的
urls()未设置confirm页面且包路由关闭时,$event->confirmUrl为null。如果无法构建链接(路由已开启但未注册,例如路由缓存过期后),则会抛出InvalidConfigurationException并撤销注册或确认:此时不会有任何提交,因此不会触发事件,也不会有邮件需要发送。
3. 偏好设置页链接监听器
use Taldres\Waitlist\Events\ManageLinkRequested;
class SendManageLinkMail implements ShouldQueue, ShouldBeEncrypted
{
public function handle(ManageLinkRequested $event): void
{
Mail::to($event->entry->email)->send(new ManageLinkMail($event->manageUrl, $event->expiresAt));
}
}
该链接在 waitlist.manage.token_ttl 分钟(默认 60)后过期,请在邮件中说明。
4. 发布邮件和新闻通讯:使用 recipients()
use Taldres\Waitlist\Support\Recipient;
// 一个列表,用于其主要用途:链接退出该列表
Waitlist::for('beta')->recipients()->each(
fn (Recipient $recipient) => Mail::to($recipient->email)->locale($recipient->locale)->queue(new LaunchMail($recipient)),
);
// 一个可选的用途,项目范围,每个地址只发送一次:链接仅退订该用途
Waitlist::recipients('newsletter')->each(
fn (Recipient $recipient) => Mail::to($recipient->email)->locale($recipient->locale)->queue(new NewsletterMail($recipient)),
);
在 Mailable 中使用接收者的链接:
use Illuminate\Mail\Mailables\Headers;
public function __construct(public Recipient $recipient) {}
public function headers(): Headers
{
return new Headers(text: $this->recipient->listUnsubscribeHeaders()); // RFC 8058 一键点击
}
// 邮件内容:$this->recipient->unsubscribeUrl()
Recipient 包含 entry、email、purpose、locale(同意记录的语言,可能为 null)和 leavesList。队列化的 Mailable 携带地址和令牌链接:也让它们实现 ShouldBeEncrypted。一键点击头部需要包路由和 https 的 APP_URL;没有路由时这些头部为空。
5. 事件外的链接生成
Waitlist::unsubscribeUrl($entry); // 退出整个列表
Waitlist::unsubscribeUrl($entry, 'newsletter'); // 仅退订新闻通讯
Waitlist::listUnsubscribeHeaders($entry, 'newsletter');
6. 多项目管理
项目标识为 $event->entry->project(或 $recipient->entry->project)。从中选择发件人和邮件驱动:
// config/products.php(应用自有配置):['rocket' => ['name' => 'Rocket', 'from' => '[email protected]', 'mailer' => 'resend'], ...]
$product = config("products.{$event->entry->project}");
$mail = (new ConfirmWaitlistMail(...))->from($product['from'], $product['name']);
Mail::mailer($product['mailer'])->to($event->entry->email)->send($mail);
邮件中的链接已自动指向各项目自己的页面(由 urls() 决定)。
规则、参考与模板
- 每个发送邮件的监听器都需要实现
ShouldQueue(新地址和已知地址响应时间一致)和ShouldBeEncrypted(负载包含令牌;EntryForgotten事件包含地址)。 - Laravel 通过
handle()方法的类型提示自动发现app/Listeners目录下的监听器;如果再用Event::listen()注册一次,每封邮件会被发送两次。 - 事件在数据库提交后触发;事件被触发并不保证邮件已成功发送,请监控失败的任务。
- 邮件驱动设置:https://laravel.com/docs/mail;从配置了 SPF、DKIM 和 DMARC 的域名发送。
- 完整指南:https://github.com/Taldres/laravel-waitlist/blob/main/docs/mail.md
示例
- "用 Resend 发送双重确认邮件":执行
composer require resend/resend-php,设置MAIL_MAILER=resend,RESEND_KEY=...,然后使用步骤 2 中的监听器。 - "给所有 beta 列表的用户发送产品发布通知":使用步骤 4 和
Waitlist::for('beta')->recipients()。 - "发送月度新闻通讯":使用
Waitlist::recipients('newsletter'),并为每个接收者生成一键点击头部。
反模式
- 在未检查用户同意的情况下,直接通过
WaitlistEntry::all()或Waitlist::for($list)->entries()发送邮件;应使用recipients()或whereConsentedTo($purpose)。 - 将
Waitlist::manageLink($entry)或管理 URL 放入普通邮件中;这会暴露用户的个人数据,且邮件可能会被转发。 - 将管理链接发送到
$event->entry->email以外的任何地址,或直接用该链接回复请求。 - 新闻通讯链接直接退订整个列表:应使用
unsubscribeUrl($entry, 'newsletter')或Recipient自带的链接。 - 使用同步邮件监听器,或队列化但未实现
ShouldBeEncrypted的监听器。