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에서는 동기화하지 마십시오: 더블 옵트인의 경우 주소가 확인되지 않았습니다.
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']);
제공업체 API에는 Laravel의 Http 클라이언트를 사용하십시오. 자격 증명과 목적별 목록 ID는 config/services.php에 보관하십시오.
4. 제공업체의 구독 취소를 다시 가져오기
도구의 캠페인에는 자체 구독 취소 링크가 포함됩니다. 해당 웹훅을 수신하고 주소의 모든 목록에서 목적을 철회하십시오. 반복해도 변경 사항은 없습니다:
// routes/api.php, 또는 CSRF 보호에서 제외된 라우트를 사용하는 routes/web.php
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) {
// 먼저 제공업체 문서대로 웹훅을 검증합니다
$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단계, 목록 ID로
config('services.brevo.lists.newsletter')사용. - "Mailchimp에서 구독 취소한 사람들이 여전히 출시 메일을 받는 경우": 4단계.
안티 패턴
EntrySubscribed에서 더블 옵트인 전에 동기화.EntryUnsubscribed에서 프로젝트의 다른 목록을 확인하지 않고 연락처를 제거하거나 목록의 기본 목적만 제거.- 모든
EntryForgotten에서 연락처를 삭제: 보존 정리 및forgetAll()은 주소가 다른 목록에 여전히 있을 수 있는 동안 하나의 목록을 삭제합니다. - 리스너 메서드 이름을
handle*로 지정하고Event::listen()으로도 등록: 모든 동기화가 두 번 실행됩니다. - 하나의 항목에서만 철회하거나 요청을 검증하지 않고 신뢰하는 웹훅.
- 전달된
EntryForgotten을 제공업체가 연락처를 삭제했다는 증거로 간주; 실패한 작업을 모니터링하십시오.