Laravel Waitlist: 与邮件提供商同步
当使用 taldres/laravel-waitlist 的应用程序通过外部邮件工具发送营销活动,且该工具中的联系人列表必须遵循候补名单的同意状态时,使用此技能。
主要目标
- 提供商仅保留目的仍在生效的联系人,任何一方的撤销操作都将同步到另一方。
工作流程
1. 确定是否需要同步
Laravel 应用自身(通过任何邮件器)发送的邮件无需同步:直接使用 Waitlist::recipients($purpose) 发送即可。仅当活动是从邮件工具发送时,才需要进行同步。
2. 将事件映射到提供商操作
| 事件 | 提供商操作 |
|---|---|
EntryConfirmed | 添加联系人,每个 $event->entry->purposes 对应一个提供商列表 |
ConsentGranted | 当 $event->subscription->isConfirmed() 为 true 时,添加到该目的对应的列表 |
ConsentWithdrawn | 从 $event->consent->purpose 对应的列表中移除,除非项目的其他列表仍持有该目的 |
EntryUnsubscribed | 从该周期持有的每个目的对应的列表中移除,除非其他列表仍持有该目的 |
EntryForgotten | 仅当该邮箱地址没有任何条目剩余时才删除联系人;否则,移除剩余条目不持有的目的 |
- 离开一个列表将结束其所有目的对应的周期:在该列表中同意的新闻通讯也将终止,且不会触发
ConsentWithdrawn事件。 EntryForgotten为每个被清除的条目触发,包括forgetAll()和单个列表的保留策略修剪,此时该邮箱地址可能仍在其他列表中。- 不要在
EntrySubscribed事件上执行同步:双向确认模式下,地址尚未确认。
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上同步,即在双向确认之前同步。 - 在
EntryUnsubscribed上移除联系人时,未检查项目的其他列表,或仅移除列表的主要目的。 - 在每次
EntryForgotten时删除联系人:保留策略和forgetAll()会清除一个列表,但该地址可能仍在其他列表中。 - 将监听器方法命名为
handle*并同时通过Event::listen()注册:每次同步都会执行两次。 - 仅从一个条目中撤销的 webhook,或者未验证请求就信任它。
- 认为调度一个
EntryForgotten事件就证明提供商已删除联系人;请监控失败的任务。