Community编程与开发github.com

Taldres/laravel-waitlist: 与邮件提供商同步

保持外部邮件工具(如 Brevo、Mailchimp 或 Mailcoach)与 taldres/laravel-waitlist 候补名单同步:按目的添加已确认联系人,在撤销和删除时移除,并通过 webhook 将提供商处执行的退订操作回传到候补名单。

laravel-waitlist: 与邮件提供商同步 是什么?

该技能旨在帮助开发者将 Laravel 应用中的 `taldres/laravel-waitlist` 包与外部邮件营销工具(如 Brevo、Mailchimp、Mailcoach 等)同步。其核心目标是确保邮件提供商中的联系人列表始终反映 Laravel 应用中候补名单的同意状态:当用户确认加入、授予同意、撤销同意、退订或数据被遗忘时,自动在提供商侧执行添加或删除操作。技能提供了完整的工作流指南,包括应同步的事件映射(如 `EntryConfirmed`、`ConsentGranted`、`ConsentWithdrawn`、`EntryUnsubscribed`、`EntryForgotten`)以及不应同步的事件(如未确认的 `EntrySubscribed`)。此外,技能还涵盖了如何处理来自提供商 webhook 的退订回传、一个完整的加密队列监听器类示例、注册事件监听器的注意事项,以及常见反模式提示(如重复同步、未检查其他列表即删除联系人等)。此技能适用于 Laravel 框架和任何支持 webhook 的邮件服务提供商。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Taldres/laravel-waitlist/tree/HEAD/resources/boost/skills/laravel-waitlist-provider-sync

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

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 事件就证明提供商已删除联系人;请监控失败的任务。

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

Taldres/laravel-waitlist

Use this skill when reviewing Laravel package compatibility across composer constraints, PHP versions, Laravel versions, Testbench versions, dependency stability lanes, Windows CI, or matrix-sensitive code and workflow changes.

Taldres/laravel-waitlist

Use this skill when creating or updating the bundled Laravel Boost skill under resources/boost/skills from the package implementation and package documentation. Trigger after public APIs, commands, config, routes, views, publish tags, README content, or examples change.

Taldres/laravel-waitlist

Use this skill when preparing Laravel package releases: CHANGELOG.md updates, generated release notes, GitHub release workflows, version checks, tags, release validation, or release automation changes. Never publish autonomously.

Taldres/laravel-waitlist

Use this skill when adding Laravel package capabilities or wiring them through the service provider: commands, migrations, routes, config merges, views, translations, assets, middleware, publish tags, workbench files, or console-only behavior.

Taldres/laravel-waitlist

Use this skill when writing, editing, fixing, or reviewing package tests with Pest 4/5 and Orchestra Testbench, including TDD, feature tests, unit tests, type coverage, arch tests, workbench behavior, commands, routes, config, migrations, and publishable resources.

Taldres/laravel-waitlist

Integrate taldres/laravel-waitlist in a Laravel application: define purposes, lists and fields, build the signup form, send the confirmation mail from events, add a preference page, and keep consent, retention and encryption intact.

Taldres/laravel-waitlist

Build the pages of a taldres/laravel-waitlist integration: the signup form with the registered wording, and the confirm, unsubscribe and preference pages, either in Laravel controllers (Blade, Livewire, Inertia) or in an SPA or static site over the package's JSON API, including CORS and bot protection.

Taldres/laravel-waitlist

Turn a taldres/laravel-waitlist waitlist into a launch: invite people in batches in signup order, let invited addresses register, announce the launch to everyone who agreed, and erase the list once its purpose is fulfilled.

Taldres/laravel-waitlist

Send the mails of a taldres/laravel-waitlist integration: the double opt-in confirmation, the preference page link, a welcome mail, launch mails and newsletters to the people who agreed, with the right unsubscribe links, one-click headers and a sender per project.

Taldres/laravel-waitlist

Operate a taldres/laravel-waitlist installation: answer access and erasure requests, withdraw a purpose on request, apply retention, erase a list once its purpose is fulfilled, rotate APP_KEY with waitlist:rekey, export a list, and check the setup before going live.

Taldres/laravel-waitlist

Run the waitlists of several products in one Laravel app with taldres/laravel-waitlist projects: define projects, scope facade calls with project(), resolve the project of HTTP signups, send mail per project, handle requests across all projects, or keep projects in a database for a central waitlist API.

Taldres/laravel-waitlist

Build waitlist statistics with taldres/laravel-waitlist: signups and confirmations per day for charts, totals and confirmation rates for a period, the confirmed count on any past day, and how many people are on a list right now, for dashboards, admin pages and API endpoints.

Taldres/laravel-waitlist

Write feature tests for an application's taldres/laravel-waitlist integration: get the plain tokens from events, assert the confirmation and other mails, test the signup, confirm and unsubscribe flows in PHP and over the package's HTTP routes, and bypass rate limits and bot checks in tests.

相关技能