: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
fix(workflows): only suppress addresses an email was sent to (#130784)
Meikel Ratz37cac3f0d804
diff --git a/nodejs/src/cdp/services/messaging/helpers/ses.test.ts b/nodejs/src/cdp/services/messaging/helpers/ses.test.tsindex 735a4693f45e..c6d156b835f1 100644--- a/nodejs/src/cdp/services/messaging/helpers/ses.test.ts+++ b/nodejs/src/cdp/services/messaging/helpers/ses.test.ts@@ -639,6 +639,62 @@ describe('SesWebhookHandler', () => {})+ it.each([+ [+ 'Permanent bounce',+ 'hardBounceRecipients' as const,+ (emailAddresses: string[]) => ({+ eventType: 'Bounce',+ bounce: {+ bounceType: 'Permanent',+ bouncedRecipients: emailAddresses.map((emailAddress) => ({ emailAddress, status: '5.1.1' })),+ timestamp: '2025-10-03T12:04:00Z',+ },+ }),+ ],+ [+ 'Transient bounce',+ 'transientBounceRecipients' as const,+ (emailAddresses: string[]) => ({+ eventType: 'Bounce',+ bounce: {+ bounceType: 'Transient',+ bouncedRecipients: emailAddresses.map((emailAddress) => ({ emailAddress, status: '4.2.2' })),+ timestamp: '2025-10-03T12:04:00Z',+ },+ }),+ ],+ [+ 'Complaint',+ 'complainedRecipients' as const,+ (emailAddresses: string[]) => ({+ eventType: 'Complaint',+ complaint: {+ complainedRecipients: emailAddresses.map((emailAddress) => ({ emailAddress })),+ timestamp: '2025-10-03T12:05:00Z',+ },+ }),+ ],+ ])('keeps only sent-to recipients of a %s for suppression', async (_label, arrayKey, buildEvent) => {+ // The send passes a display name with the address, so the destination entry can carry it too.+ const mail = { ...baseMail, destination: ['"Recipient Name" <[email protected]>'] }+ const body = [+ { mail, ...buildEvent(['[email protected]', '[email protected]']) },+ { mail, ...buildEvent(['[email protected]']) },+ ]+ // Other tests in the process add to the counter, so read it as a delta.+ const notSentToCount = async (): Promise<number> => {+ const metric = await register.getSingleMetric('email_tracking_recipients_not_sent_to_total')!.get()+ return metric.values.find((v) => v.labels.event_type === body[0].eventType)?.value ?? 0+ }+ const countBefore = await notSentToCount()++ const result = await handler.handleWebhook({ body, headers: {} })++ expect(result[arrayKey]).toEqual([expect.objectContaining({ teamId: '1', emailAddresses: ['[email protected]'] })])+ expect((await notSentToCount()) - countBefore).toBe(2)+ })+it('returns 200, no metrics and no logged payload if tracking code is missing from both carriers', async () => {const logSpies = (['info', 'warn', 'error'] as const).map((level) => jest.spyOn(logger, level))const body = [@@ -1259,14 +1315,14 @@ describe('SesWebhookHandler', () => {mail: baseMail,bounce: {bounceType: 'Permanent',- bouncedRecipients: [{ emailAddress: '[email protected]', diagnosticCode: 'bad' }],+ bouncedRecipients: [{ emailAddress: '[email protected]', diagnosticCode: 'bad' }],timestamp: '2025-10-03T12:04:00Z',},})const result = await restricted.handleWebhook({ body: envelope, headers: {}, verifySignature: false })expect(result.status).toBe(200)expect(result.hardBounceRecipients).toEqual([- { teamId: '1', emailAddresses: ['[email protected]'], diagnostic: 'bad' },+ { teamId: '1', emailAddresses: ['[email protected]'], diagnostic: 'bad' },])})diff --git a/nodejs/src/cdp/services/messaging/helpers/ses.ts b/nodejs/src/cdp/services/messaging/helpers/ses.tsindex df850381cb18..5d2c5472573b 100644--- a/nodejs/src/cdp/services/messaging/helpers/ses.ts+++ b/nodejs/src/cdp/services/messaging/helpers/ses.ts@@ -270,14 +270,33 @@ export type SesEmailStatusUpdate = {const normalizeAddress = (address: string): string => unquoteMailbox(address).trim().toLowerCase()-// The addresses in the To header. SES reports each entry as written, so a display name can wrap the address.-const toHeaderAddresses = (rec: SesEventRecord): Set<string> =>+// SES reports each entry as written at send time, so a display name can wrap the address.+const mailboxAddresses = (entries: string[]): Set<string> =>new Set(- (rec.mail.commonHeaders?.to ?? []).flatMap((entry) =>+ entries.flatMap((entry) =>addressparser(entry, { flatten: true }).map((parsed) => normalizeAddress(parsed.address))))+const toHeaderAddresses = (rec: SesEventRecord): Set<string> => mailboxAddresses(rec.mail.commonHeaders?.to ?? [])++const sesRecipientsNotSentToCounter = new Counter({+ name: 'email_tracking_recipients_not_sent_to_total',+ help: 'Bounce and complaint recipients left out of suppression because the email was not sent to them',+ labelNames: ['event_type'],+})++// SES reports a bounce or complaint recipient from the receiving server's report, which can name any address.+// Only an address this send went to may change suppression state.+const sentToRecipients = <T extends { emailAddress: string }>(rec: SesEventRecord, recipients: T[]): T[] => {+ const sentTo = mailboxAddresses(rec.mail.destination)+ const kept = recipients.filter((r) => sentTo.has(normalizeAddress(r.emailAddress)))+ if (kept.length < recipients.length) {+ sesRecipientsNotSentToCounter.inc({ event_type: rec.eventType }, recipients.length - kept.length)+ }+ return kept+}+/*** The status change a Delivery, Bounce, Complaint or Reject record makes to the email it reports on, or the* open or click an Open or Click record adds to it, or null.@@ -1014,31 +1033,36 @@ export class SesWebhookHandler {// Record permanent bounces in the suppression list so future sends to the address are// blocked. The SMTP diagnostic is threaded through for operator visibility on the row.if (suppressionAllowed && rec.eventType === 'Bounce' && rec.bounce.bounceType === 'Permanent') {- const emails = rec.bounce.bouncedRecipients.map((r) => r.emailAddress)- const diagnostic = rec.bounce.bouncedRecipients.find((r) => r.diagnosticCode)?.diagnosticCode- // SES reports the DSN's final recipient, which the receiving server chooses. Only an address- // this send went to can go on the global list.- const sentTo = new Set(rec.mail.destination.map(normalizeAddress))- const nonexistentAddresses = fromTrustedTopic- ? nonexistentRecipients(rec.bounce).filter((email) => sentTo.has(normalizeAddress(email)))- : []- hardBounceRecipients.push({- teamId,- emailAddresses: emails,- diagnostic,- ...(nonexistentAddresses.length > 0- ? { nonexistentAddresses, bounceTimestamp: rec.bounce.timestamp }- : {}),- })+ const bounced = sentToRecipients(rec, rec.bounce.bouncedRecipients)+ if (bounced.length > 0) {+ const diagnostic = bounced.find((r) => r.diagnosticCode)?.diagnosticCode+ const nonexistentAddresses = fromTrustedTopic+ ? nonexistentRecipients({ ...rec.bounce, bouncedRecipients: bounced })+ : []+ hardBounceRecipients.push({+ teamId,+ emailAddresses: bounced.map((r) => r.emailAddress),+ diagnostic,+ ...(nonexistentAddresses.length > 0+ ? { nonexistentAddresses, bounceTimestamp: rec.bounce.timestamp }+ : {}),+ })+ }}// Count soft (Transient) bounces toward suppression. These are recipient-side failures// (server unreachable, mailbox full, greylisting); one is harmless but a persistent run// of them means the address can't receive mail.if (suppressionAllowed && rec.eventType === 'Bounce' && rec.bounce.bounceType === 'Transient') {- const emails = rec.bounce.bouncedRecipients.map((r) => r.emailAddress)- const diagnostic = rec.bounce.bouncedRecipients.find((r) => r.diagnosticCode)?.diagnosticCode- transientBounceRecipients.push({ teamId, emailAddresses: emails, diagnostic })+ const bounced = sentToRecipients(rec, rec.bounce.bouncedRecipients)+ if (bounced.length > 0) {+ const diagnostic = bounced.find((r) => r.diagnosticCode)?.diagnosticCode+ transientBounceRecipients.push({+ teamId,+ emailAddresses: bounced.map((r) => r.emailAddress),+ diagnostic,+ })+ }}// Record complaints in the suppression list so future sends to the address are blocked.@@ -1052,12 +1076,14 @@ export class SesWebhookHandler {rec.eventType === 'Complaint' &&rec.complaint.complaintFeedbackType?.toLowerCase() !== 'not-spam') {- const emails = rec.complaint.complainedRecipients.map((r) => r.emailAddress)- complainedRecipients.push({- teamId,- emailAddresses: emails,- feedbackType: rec.complaint.complaintFeedbackType,- })+ const complained = sentToRecipients(rec, rec.complaint.complainedRecipients)+ if (complained.length > 0) {+ complainedRecipients.push({+ teamId,+ emailAddresses: complained.map((r) => r.emailAddress),+ feedbackType: rec.complaint.complaintFeedbackType,+ })+ }}// Successful delivery resets an address's soft-bounce counter — but only if newer than the