Laravel Waitlist:與電子郵件提供者同步
當使用 taldres/laravel-waitlist 的應用程式透過外部電子報工具發送行銷活動,且其聯絡人清單必須遵循等候清單的同意狀態時,使用此技能。
主要目標
- 提供者僅持有其目的仍有效力的聯絡人,且任一方發生的撤回動作都能傳遞到另一方。
工作流程
1. 判斷是否需要同步
Laravel 應用程式自行發送的郵件(透過任何郵件程式)不需要同步:只需將它們發送給 Waitlist::recipients($purpose)。僅當行銷活動是從外部工具發送時,才需要同步。
2. 將事件對應到提供者呼叫
| 事件 | 提供者動作 |
|---|---|
EntryConfirmed | 新增聯絡人,根據 $event->entry->purposes 中的每個目的,加入到對應的提供者清單 |
ConsentGranted | 一旦 $event->subscription->isConfirmed() 成立,將聯絡人加入到該目的的清單 |
ConsentWithdrawn | 從 $event->consent->purpose 的清單中移除,除非專案中仍有其他清單持有該目的 |
EntryUnsubscribed | 從該生命週期所持有的每個目的的清單中移除,除非其他清單仍持有該目的 |
EntryForgotten | 僅在該電子郵件地址完全沒有任何條目時,才刪除該聯絡人;否則,僅移除剩餘條目未持有的目的 |
- 離開一個清單會終止其與清單上每個目的的關聯:在該清單上同意的電子報也會終止,且不會為此觸發
ConsentWithdrawn事件。 EntryForgotten會為每個被刪除的條目觸發,包括forgetAll()和對單一清單的保留期限清理,而該電子郵件地址可能仍在其他清單上。- 不要在
EntrySubscribed事件上進行同步:採用雙重 opt-in 時,該地址尚未確認。
3. 一個佇列化、加密的監聽器類別
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Taldres\Waitlist\Events\ConsentGranted;
use Taldres\Waitlist\Events\ConsentWithdrawn;
use Taldres\Waitlist\Events\EntryConfirmed;
use Taldres\Waitlist\Events\EntryForgotten;
use Taldres\Waitlist\Events\EntryUnsubscribed;
use Taldres\Waitlist\Models\WaitlistEntry;
class SyncWaitlistToProvider implements ShouldQueue, ShouldBeEncrypted
{
public function onConfirmed(EntryConfirmed $event): void
{
// 將 $event->entry->email 加入到 $event->entry->purposes 中每個目的對應的提供者清單
}
public function onGranted(ConsentGranted $event): void
{
if ($event->subscription->isConfirmed()) {
// 將 $event->entry->email 加入到 $event->consent->purpose 對應的清單
}
}
public function onWithdrawn(ConsentWithdrawn $event): void
{
$this->removeUnlessHeld($event->entry->project, $event->entry->email, $event->consent->purpose);
}
public function onUnsubscribed(EntryUnsubscribed $event): void
{
$event->subscription->consents()->pluck('purpose')->unique()->each(
fn (string $purpose) => $this->removeUnlessHeld($event->entry->project, $event->entry->email, $purpose),
);
}
public function onForgotten(EntryForgotten $event): void
{
if ($event->email === null) { // 無法解密
return;
}
if (! WaitlistEntry::query()->forEmail($event->email)->exists()) {
// 刪除聯絡人 $event->email
return;
}
foreach (['waitlist', 'newsletter'] as $purpose) { // 你要同步的目的
$this->removeUnlessHeld($event->project, $event->email, $purpose);
}
}
private function removeUnlessHeld(string $project, string $email, string $purpose): void
{
// 事件在提交後觸發:只有當專案中其他清單仍持有該目的時,該目的才仍然有效。
$held = WaitlistEntry::query()->inProject($project)->forEmail($email)->whereConsentedTo($purpose)->exists();
if (! $held) {
// 從 $purpose 的提供者清單中移除 $email
}
}
}
請手動註冊這些方法。它們故意不以 handle 開頭,以避免 Laravel 的事件發現機制再次註冊它們:
// AppServiceProvider::boot()
Event::listen(EntryConfirmed::class, [SyncWaitlistToProvider::class, 'onConfirmed']);
Event::listen(ConsentGranted::class, [SyncWaitlistToProvider::class, 'onGranted']);
Event::listen(ConsentWithdrawn::class, [SyncWaitlistToProvider::class, 'onWithdrawn']);
Event::listen(EntryUnsubscribed::class, [SyncWaitlistToProvider::class, 'onUnsubscribed']);
Event::listen(EntryForgotten::class, [SyncWaitlistToProvider::class, 'onForgotten']);
使用 Laravel 的 Http 用戶端來呼叫提供者 API;將憑證和每個目的對應的清單 ID 存放在 config/services.php 中。
4. 將提供者的取消訂閱帶回
從工具發送的行銷活動會包含其自身的取消訂閱連結。接收提供者的 webhook 並從該地址所有清單中撤回目的;重複執行不會有任何影響:
// routes/api.php,或 routes/web.php(將該路由排除在 CSRF 保護之外)
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Taldres\Waitlist\Facades\Waitlist;
use Taldres\Waitlist\Models\WaitlistEntry;
Route::post('/webhooks/newsletter', function (Request $request) {
// 首先按照提供者的文件驗證 webhook
$email = (string) $request->input('email'); // 欄位名稱取決於提供者
Waitlist::findByEmail($email)->each(
fn (WaitlistEntry $entry) => Waitlist::for($entry->list)->withdraw($email, 'newsletter'),
);
return response()->noContent();
});
多個專案:將提供者清單對應到其專案,並使用 Waitlist::project($project)->findByEmail() 和 Waitlist::project($project)->for($entry->list)->withdraw()。
規則、參考資料與範本
- 每個專案和目的有一個提供者清單;切勿為可選目的使用每條等候清單一個清單的模式,因為可選目的是每個專案一個同意。
- 共用主要目的的多個清單是獨立的等候清單:離開一個清單絕不會在另一個清單仍持有該目的時移除聯絡人。
php artisan waitlist:privacy會將監聽器列為接收者;提供者是處理者,需要簽署協議。- 包含完整 Brevo 監聽器的完整指南:https://github.com/Taldres/laravel-waitlist/blob/main/docs/email-provider-sync.md
範例
- "將已確認的電子報註冊同步到 Brevo":步驟 2 和 3,使用
config('services.brevo.lists.newsletter')作為清單 ID。 - "在 Mailchimp 中取消訂閱的人仍然收到我們的啟動郵件":步驟 4。
反模式
- 在
EntrySubscribed上同步,這發生在雙重 opt-in 確認之前。 - 在
EntryUnsubscribed時移除聯絡人,而未檢查專案中的其他清單,或僅移除清單的主要目的。 - 在每次
EntryForgotten時都刪除聯絡人:保留期限清理和forgetAll()會刪除一個清單的條目,而該地址可能仍在其他清單上。 - 將監聽器方法命名為
handle*並同時使用Event::listen()註冊它們:每次同步會執行兩次。 - 僅從一個條目撤回的 Webhook,或未驗證請求就信任它。
- 將已派送的
EntryForgotten視為提供者已刪除聯絡人的證明;請監控失敗的任務。