Skip to content

: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.

feat(messaging): set hard_bounced_at from the suppression writers (#125374)

Meikel Ratz15419e00f00e
diff --git a/products/messaging/backend/models/message_suppression.py b/products/messaging/backend/models/message_suppression.py
index 89cbe3f29ad7..74dfe450c0ef 100644
--- a/products/messaging/backend/models/message_suppression.py
+++ b/products/messaging/backend/models/message_suppression.py
@@ -30,10 +30,9 @@ class MessageSuppression(TeamScopedRootMixin, UUIDModel):
- Manually, when a user adds it in the Suppression list UI.
The pre-send path consults this list (see the Node `EmailSuppressionService`) and skips
- matching recipients. Unlike opt-outs, a suppression is a deliverability signal, not a
- messaging preference, so it applies regardless of message category (including transactional).
- The one exception is a COMPLAINT entry: the recipient objected to our mail, but the address
- still works, so transactional messages still go out to it.
+ matching recipients. Transactional messages skip only addresses that hard-bounced, because
+ only a hard bounce proves the address can't receive mail. A complaint, a manual entry or a
+ run of soft bounces (for example a full inbox) still lets transactional messages through.
"""
# db_constraint=False on these hot-table FKs so CreateModel takes no lock on
diff --git a/products/messaging/backend/services/customerio_import_service.py b/products/messaging/backend/services/customerio_import_service.py
index ff3b592bdd27..38512461b891 100644
--- a/products/messaging/backend/services/customerio_import_service.py
+++ b/products/messaging/backend/services/customerio_import_service.py
@@ -553,6 +553,9 @@ class CustomerIOImportService:
suppressed_at=details.suppressed_at or now,
last_bounce_at=details.suppressed_at if is_bounce else None,
last_bounce_diagnostic=details.diagnostic,
+ # Customer.io lists an address under bounces or invalid emails only when it can't receive
+ # mail, so these block transactional email the way our own hard bounces do.
+ hard_bounced_at=(details.suppressed_at or now) if is_bounce else None,
created_by_id=self.created_by_id,
)
for email, details in sorted(imported.items())
diff --git a/products/messaging/backend/services/suppression.py b/products/messaging/backend/services/suppression.py
index 99c98c85839d..58d43a28117a 100644
--- a/products/messaging/backend/services/suppression.py
+++ b/products/messaging/backend/services/suppression.py
@@ -44,8 +44,10 @@ def add_manual_suppression(team_id: int, identifier: str, created_by_id: int | N
def remove_suppression(team_id: int, identifier: str) -> bool:
- # Soft-delete and un-suppress. Reset the bounce counter so a previously-dead address that
- # a user deliberately re-enables starts from a clean slate. `source` is reset to BOUNCE so
+ # Soft-delete and un-suppress. Reset the bounce counter and the hard-bounce mark so a
+ # previously-dead address that a user deliberately re-enables starts from a clean slate. The reason is cleared too,
+ # because the node writes treat a BOUNCE row with a legacy hard-bounce reason as hard-bounced, and the next soft
+ # bounce or complaint would set the mark again. `source` is reset to BOUNCE so
# a future auto-suppression can re-suppress this row — the node upserts skip rows with
# source='MANUAL' (to protect user-managed entries), so a removed MANUAL row would otherwise
# be permanently invisible to the bounce-driven write path.
@@ -56,6 +58,8 @@ def remove_suppression(team_id: int, identifier: str) -> bool:
suppressed=False,
deleted=True,
transient_bounce_count=0,
+ hard_bounced_at=None,
+ reason=None,
source=SuppressionSource.BOUNCE,
updated_at=timezone.now(),
)
diff --git a/products/messaging/backend/tests/api/test_message_suppression.py b/products/messaging/backend/tests/api/test_message_suppression.py
index 4eccebff7756..6f191aa0d697 100644
--- a/products/messaging/backend/tests/api/test_message_suppression.py
+++ b/products/messaging/backend/tests/api/test_message_suppression.py
@@ -1,5 +1,7 @@
from posthog.test.base import APIBaseTest
+from django.utils import timezone
+
from parameterized import parameterized
from posthog.models.personal_api_key import PersonalAPIKey
@@ -98,6 +100,9 @@ class TestRemoveSuppressionResetsSource(APIBaseTest):
row = MessageSuppression.objects.for_team(self.team.id).get(identifier="[email protected]")
assert row.source == SuppressionSource.MANUAL
assert row.suppressed
+ MessageSuppression.objects.for_team(self.team.id).filter(pk=row.pk).update(
+ hard_bounced_at=timezone.now(), reason="Auto-suppressed after a hard bounce"
+ )
# Remove — the row should be un-suppressed AND its source reset so that the ON CONFLICT
# branches in the node write path (which skip MANUAL rows) can re-suppress it later.
@@ -105,10 +110,19 @@ class TestRemoveSuppressionResetsSource(APIBaseTest):
assert response.status_code == 204
row.refresh_from_db()
- assert (row.suppressed, row.deleted, row.transient_bounce_count, row.source) == (
+ assert (
+ row.suppressed,
+ row.deleted,
+ row.transient_bounce_count,
+ row.hard_bounced_at,
+ row.reason,
+ row.source,
+ ) == (
False,
True,
0,
+ None,
+ None,
SuppressionSource.BOUNCE,
), (
"remove_suppression must reset the row so the node write path can auto-suppress this address again if it later bounces"
diff --git a/products/messaging/backend/tests/test_provider_imports.py b/products/messaging/backend/tests/test_provider_imports.py
index 36044bf61c7c..0f5d6d23953a 100644
--- a/products/messaging/backend/tests/test_provider_imports.py
+++ b/products/messaging/backend/tests/test_provider_imports.py
@@ -86,6 +86,24 @@ class TestProviderImportRuns(BaseTest):
mock_client.get_suppressions.assert_not_called()
+ @parameterized.expand([("bounce", "[email protected]", True), ("spam_report", "[email protected]", False)])
+ def test_only_bounce_suppressions_are_imported_as_hard_bounces(
+ self,
+ mock_client_class: MagicMock,
+ _start_workflow: MagicMock,
+ _name: str,
+ identifier: str,
+ hard_bounced: bool,
+ ) -> None:
+ mock_client_class.return_value.get_suppressions.side_effect = lambda suppression_type, limit, offset: (
+ self.pages.get((suppression_type, offset), [])
+ )
+ run = self._start(["suppressions"])
+ runs.run_step(self.team.id, run.id, "suppressions", attempt=1)
+
+ row = MessageSuppression.objects.for_team(self.team.id).get(identifier=identifier)
+ assert (row.hard_bounced_at is not None) is hard_bounced
+
def test_an_older_attempt_stops_without_writing_once_a_newer_attempt_takes_over(
self, mock_client_class: MagicMock, _start_workflow: MagicMock
) -> None: