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(cdp): expire global hard bounces with an escalating hold (#130707)

Meikel Ratz76094ea7959d
diff --git a/nodejs/src/cdp/services/messaging/email-suppression.service.test.ts b/nodejs/src/cdp/services/messaging/email-suppression.service.test.ts
index 41dd48b9ae96..51ca2ac8152a 100644
--- a/nodejs/src/cdp/services/messaging/email-suppression.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/email-suppression.service.test.ts
@@ -418,6 +418,27 @@ describe('EmailSuppressionService', () => {
expect(await newService(true).isSuppressed(team.id, email)).toBe(false)
})
+ it.each([
+ ['bounced once, an hour ago', 1 / 24, 1 / 24, true],
+ ['bounced once, two days ago', 2, 2, false],
+ ['bounced for ten days, last five days ago', 15, 5, true],
+ ['bounced for two months, last fifteen days ago', 75, 15, false],
+ ] as const)(
+ 'holds an entry for as long as the address has been bouncing, up to 14 days: %s',
+ async (_label, firstDaysAgo, lastDaysAgo, blocked) => {
+ const email = `held-${firstDaysAgo}-${lastDaysAgo}-${team.id}@example.com`
+ await hub.postgres.query(
+ PostgresUse.COMMON_WRITE,
+ `INSERT INTO posthog_emailhardbounce (email, first_bounced_at, last_bounced_at)
+ VALUES ($1, NOW() - $2 * INTERVAL '1 day', NOW() - $3 * INTERVAL '1 day')`,
+ [email, firstDaysAgo, lastDaysAgo],
+ 'test-insert-global-hard-bounce'
+ )
+
+ expect(await newService(true).isSuppressed(team.id, email, { transactional: true })).toBe(blocked)
+ }
+ )
+
it("blocks transactional email when the team's own entry only blocks other email", async () => {
const email = `complained-${team.id}@example.com`
await bounceForAnotherTeam(email)
@@ -438,20 +459,33 @@ describe('EmailSuppressionService', () => {
expect(await newService(true).isSuppressed(team.id, email)).toBe(false)
})
+ const lapseGlobalEntry = async (email: string): Promise<void> => {
+ await hub.postgres.query(
+ PostgresUse.COMMON_WRITE,
+ `UPDATE posthog_emailhardbounce
+ SET first_bounced_at = NOW() - INTERVAL '3 days', last_bounced_at = NOW() - INTERVAL '2 days'
+ WHERE email = $1`,
+ [email],
+ 'test-lapse-global-hard-bounce'
+ )
+ }
+
it.each([
- ['no other event', false],
- ['a delayed soft bounce for an earlier send', true],
- ])(
- "releases a team's copied block once a delivery clears the global entry after %s",
- async (_label, softBounce) => {
- const email = `recovered-${softBounce ? 'soft' : 'plain'}-${team.id}@example.com`
- const otherTeamId = await bounceForAnotherTeam(email)
- expect(await newService(true).isSuppressed(team.id, email)).toBe(true)
- expect(await newService(true).isSuppressed(team.id, email)).toBe(true)
- if (softBounce) {
- await newService(true).recordTransientBounces(team.id, [email], 'smtp; 421 try again later')
- }
+ ['a delivery clears the global entry after no other event', 'delivery', false],
+ ['a delivery clears the global entry after a delayed soft bounce for an earlier send', 'delivery', true],
+ ['the global entry lapses', 'lapse', false],
+ ] as const)("releases a team's copied block once %s", async (_label, clearedBy, softBounce) => {
+ const email = `recovered-${clearedBy}-${softBounce ? 'soft' : 'plain'}-${team.id}@example.com`
+ const otherTeamId = await bounceForAnotherTeam(email)
+ expect(await newService(true).isSuppressed(team.id, email)).toBe(true)
+ expect(await newService(true).isSuppressed(team.id, email)).toBe(true)
+ if (softBounce) {
+ await newService(true).recordTransientBounces(team.id, [email], 'smtp; 421 try again later')
+ }
+ if (clearedBy === 'lapse') {
+ await lapseGlobalEntry(email)
+ } else {
await newService(true).recordDeliveries(
otherTeamId,
[email],
@@ -460,11 +494,21 @@ describe('EmailSuppressionService', () => {
clearGlobal: true,
}
)
-
- expect(await newService(true).isSuppressed(team.id, email, { transactional: true })).toBe(false)
- expect(await readRow(email)).toMatchObject({ suppressed: false, deleted: false })
}
- )
+
+ expect(await newService(true).isSuppressed(team.id, email, { transactional: true })).toBe(false)
+ expect(await readRow(email)).toMatchObject({ suppressed: false, deleted: false })
+ })
+
+ it("keeps the team's own hard bounce once the global entry lapses", async () => {
+ const email = `own-bounce-${team.id}@example.com`
+ await bounceForAnotherTeam(email)
+ await newService(true).recordHardBounces(team.id, [email], 'smtp; 550 5.1.1 user unknown')
+ await lapseGlobalEntry(email)
+
+ expect(await newService(true).isSuppressed(team.id, email, { transactional: true })).toBe(true)
+ expect(await readRow(email)).toMatchObject({ suppressed: true, deleted: false })
+ })
it('does not undo a removal that a lagging read missed', async () => {
const email = `removed-${team.id}@example.com`
diff --git a/nodejs/src/cdp/services/messaging/email-suppression.service.ts b/nodejs/src/cdp/services/messaging/email-suppression.service.ts
index 45ac2b3e90ed..7fca35b406e5 100644
--- a/nodejs/src/cdp/services/messaging/email-suppression.service.ts
+++ b/nodejs/src/cdp/services/messaging/email-suppression.service.ts
@@ -52,6 +52,10 @@ type GlobalCandidate = {
type SuppressionEntry = { blocksTransactional: boolean; global?: 'shadow' | 'enforce' }
const HARD_BOUNCE_REASON = 'Auto-suppressed after a hard bounce'
+
+// Like the SES global list, an entry expires so a recovered mailbox is retried; a team's own list stays permanent.
+const GLOBAL_HARD_BOUNCE_IS_HELD = `last_bounced_at + LEAST(
+ GREATEST(last_bounced_at - first_bounced_at, INTERVAL '1 day'), INTERVAL '14 days') > NOW()`
const GLOBAL_HARD_BOUNCE_DIAGNOSTIC = "This address doesn't exist. An earlier email to it hard-bounced."
// Reasons that mark a dead address on rows written before `hard_bounced_at` existed: our own hard-bounce
@@ -563,7 +567,7 @@ export class EmailSuppressionService {
try {
const result = await this.postgres.query<{ email: string }>(
PostgresUse.COMMON_READ,
- `SELECT email FROM posthog_emailhardbounce WHERE email = ANY($1)`,
+ `SELECT email FROM posthog_emailhardbounce WHERE email = ANY($1) AND ${GLOBAL_HARD_BOUNCE_IS_HELD}`,
[Array.from(new Set(candidates.map((c) => c.identifier)))],
'loadGlobalHardBounces'
)
@@ -579,7 +583,6 @@ export class EmailSuppressionService {
for (const { key, teamId, identifier, kind } of candidates) {
if (kind === 'copy') {
if (!bounced.has(identifier)) {
- // A delivery cleared the global entry, which proves the address exists.
entries.delete(key)
releasesByTeam.set(teamId, [...(releasesByTeam.get(teamId) ?? []), identifier])
} else if (!this.globalHardBounceEnforce) {