Laravel Waitlist:郵件
當使用 taldres/laravel-waitlist 的應用程式需要向等待清單上的人員發送任何郵件,或為其設定郵件提供者時,使用此技能。
主要目標
- 流程所需的每封郵件都從一個已排隊、加密的監聽器發出,攜帶符合其用途的連結,並且僅送達已取得同意的地址
工作流程
1. 了解何時該發送何種郵件
此套件本身不發送郵件,也不呼叫任何提供者。它觸發事件;應用程式的監聽器透過任何 Laravel 郵件驅動程式(Resend、Postmark、SES、SMTP 等)發送。
| 郵件 | 觸發時機 | 必須攜帶 |
|---|---|---|
| 確認信(雙重 opt-in) | 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(payload 持有令牌;EntryForgotten持有地址)。 - Laravel 透過
app/Listeners中的handle()型別提示自動發現監聽器;如果再使用Event::listen()註冊它們,會導致每封郵件被發送兩次。 - 事件在資料庫提交後分派;已分派的事件並不代表郵件已成功發送,因此請監控失敗的任務。
- 郵件驅動程式設定:https://laravel.com/docs/mail;請從設有 SPF、DKIM 和 DMARC 的網域發送。
- 完整指南:https://github.com/Taldres/laravel-waitlist/blob/main/docs/mail.md
範例
- "使用 Resend 發送雙重 opt-in 郵件":執行
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的佇列監聽器。