:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(workflows): import ses suppressed bounces into the global list (#128090)
Harley Alexander03e0825621b2
diff --git a/products/messaging/backend/admin.py b/products/messaging/backend/admin.pynew file mode 100644index 000000000000..cd7ad99a0389--- /dev/null+++ b/products/messaging/backend/admin.py@@ -0,0 +1,18 @@+from django.contrib import admin+from django.http import HttpRequest++from products.messaging.backend.models.email_hard_bounce import EmailHardBounce++[email protected](EmailHardBounce)+class EmailHardBounceAdmin(admin.ModelAdmin):+ list_display = ("email", "first_bounced_at", "last_bounced_at")+ ordering = ("-last_bounced_at",)+ search_fields = ("email",)+ readonly_fields = ("email", "first_bounced_at", "last_bounced_at")++ def has_add_permission(self, request: HttpRequest) -> bool:+ return False++ def has_change_permission(self, request: HttpRequest, obj: EmailHardBounce | None = None) -> bool:+ return Falsediff --git a/products/messaging/backend/management/__init__.py b/products/messaging/backend/management/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/products/messaging/backend/management/commands/__init__.py b/products/messaging/backend/management/commands/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/products/messaging/backend/management/commands/import_ses_hard_bounces.py b/products/messaging/backend/management/commands/import_ses_hard_bounces.pynew file mode 100644index 000000000000..ac9d584819b6--- /dev/null+++ b/products/messaging/backend/management/commands/import_ses_hard_bounces.py@@ -0,0 +1,26 @@+from typing import Any++from django.core.management.base import BaseCommand++from products.messaging.backend.services.suppression import add_global_hard_bounces+from products.workflows.backend.facade.api import list_ses_bounce_suppressions+++class Command(BaseCommand):+ help = (+ "Copy the bounce entries of the SES account-level suppression list to the global hard-bounce list. "+ "Run this before turning off SES suppression, so no known bad address loses its block. Idempotent."+ )++ def add_arguments(self, parser: Any) -> None:+ parser.add_argument("--dry-run", action="store_true", help="Count the addresses without writing them")++ def handle(self, *args: Any, **options: Any) -> None:+ dry_run = options.get("dry_run", False)+ total = 0+ for page in list_ses_bounce_suppressions():+ total += len(page)+ if not dry_run:+ add_global_hard_bounces(page)+ prefix = "[DRY-RUN] Would import" if dry_run else "Imported"+ self.stdout.write(f"{prefix} {total} address(es) from the SES suppression list")diff --git a/products/messaging/backend/services/suppression.py b/products/messaging/backend/services/suppression.pyindex 58d43a28117a..38da2fdab431 100644--- a/products/messaging/backend/services/suppression.py+++ b/products/messaging/backend/services/suppression.py@@ -2,6 +2,7 @@ from django.db.models import F, QuerySetfrom django.db.models.functions import Coalesce, Nowfrom django.utils import timezone+from products.messaging.backend.models.email_hard_bounce import EmailHardBouncefrom products.messaging.backend.models.message_suppression import MessageSuppression, SuppressionSource@@ -65,3 +66,14 @@ def remove_suppression(team_id: int, identifier: str) -> bool:))return bool(updated)+++def add_global_hard_bounces(emails: list[str]) -> None:+ """Add addresses to the global hard-bounce list that every team's sends skip. Keeps existing entries as they are."""+ identifiers = sorted({email.strip().lower() for email in emails if email.strip()})+ now = timezone.now()+ # An existing row came from a real bounce and carries better times, so it wins.+ EmailHardBounce.objects.bulk_create(+ [EmailHardBounce(email=email, first_bounced_at=now, last_bounced_at=now) for email in identifiers],+ ignore_conflicts=True,+ )diff --git a/products/messaging/backend/tests/test_import_ses_hard_bounces.py b/products/messaging/backend/tests/test_import_ses_hard_bounces.pynew file mode 100644index 000000000000..d13287cdd1bb--- /dev/null+++ b/products/messaging/backend/tests/test_import_ses_hard_bounces.py@@ -0,0 +1,39 @@+from datetime import timedelta++from posthog.test.base import BaseTest+from unittest.mock import MagicMock, patch++from django.core.management import call_command+from django.utils import timezone++from products.messaging.backend.models.email_hard_bounce import EmailHardBounce+++class TestImportSesHardBounces(BaseTest):+ @patch("products.workflows.backend.providers.ses.boto3.client")+ def test_imports_every_page_and_keeps_existing_entries(self, client_factory: MagicMock) -> None:+ bounced_at = timezone.now() - timedelta(days=3)+ EmailHardBounce.objects.create(+ email="[email protected]", first_bounced_at=bounced_at, last_bounced_at=bounced_at+ )+ client_factory.return_value.list_suppressed_destinations.side_effect = [+ {+ "SuppressedDestinationSummaries": [+ {"EmailAddress": "[email protected]"},+ {"EmailAddress": "[email protected]"},+ ],+ "NextToken": "page-2",+ },+ {"SuppressedDestinationSummaries": [{"EmailAddress": "[email protected]"}]},+ ]++ call_command("import_ses_hard_bounces")++ rows = {row.email: row.first_bounced_at for row in EmailHardBounce.objects.all()}+ assert rows["[email protected]"] == bounced_at+ calls = client_factory.return_value.list_suppressed_destinations.call_args_list+ assert [call.kwargs for call in calls] == [+ {"Reasons": ["BOUNCE"], "PageSize": 1000},+ {"Reasons": ["BOUNCE"], "PageSize": 1000, "NextToken": "page-2"},+ ]diff --git a/products/workflows/backend/facade/api.py b/products/workflows/backend/facade/api.pyindex 060614e43bb8..65750373a36d 100644--- a/products/workflows/backend/facade/api.py+++ b/products/workflows/backend/facade/api.py@@ -1,4 +1,4 @@-from collections.abc import Collection, Iterable+from collections.abc import Collection, Iterable, Iteratorfrom typing import Anyfrom uuid import UUID@@ -364,6 +364,13 @@ def delete_ses_identity(identity: str) -> None:providers.SESProvider().delete_identity(identity)+def list_ses_bounce_suppressions() -> Iterator[list[str]]:+ """Yield the addresses on the SES account-level suppression list for a bounce, one page at a time."""+ from products.workflows.backend import providers # noqa: PLC0415++ yield from providers.SESProvider().list_bounce_suppressions()++def get_maildev_mock_dns_records() -> list[EmailDomainDnsRecord]:from products.workflows.backend import providers # noqa: PLC0415diff --git a/products/workflows/backend/providers/ses.py b/products/workflows/backend/providers/ses.pyindex 4ecb6490fda4..6f212561320e 100644--- a/products/workflows/backend/providers/ses.py+++ b/products/workflows/backend/providers/ses.py@@ -911,6 +911,18 @@ class SESProvider:logger.exception(f"SES API error deleting identity: {e}")raise+ def list_bounce_suppressions(self) -> Iterator[list[str]]:+ """Yield the email addresses on the account-level suppression list for a bounce, one page at a time."""+ next_token: str | None = None+ while True:+ response = self.ses_v2_client.list_suppressed_destinations(+ Reasons=["BOUNCE"], PageSize=1000, **({"NextToken": next_token} if next_token else {})+ )+ yield [entry["EmailAddress"] for entry in response.get("SuppressedDestinationSummaries", [])]+ next_token = response.get("NextToken")+ if not next_token:+ return+def _busiest_domains(self, domains: Sequence[str], *, start: datetime, end: datetime, limit: int, deadline: float) -> list[str]: