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(workflows): attribute email sends to SES tenants behind an env flag (#74739)

Meikel Ratz675e9ef09452
diff --git a/nodejs/src/cdp/cdp-services.ts b/nodejs/src/cdp/cdp-services.ts
index cf4e95c702a8..f86a546a66f9 100644
--- a/nodejs/src/cdp/cdp-services.ts
+++ b/nodejs/src/cdp/cdp-services.ts
@@ -148,6 +148,7 @@ export type CdpCoreServicesConfig = Pick<
| 'SES_ENDPOINT'
| 'SES_TRACKED_CONFIGURATION_SET'
| 'SES_UNTRACKED_CONFIGURATION_SET'
+ | 'EMAIL_SES_TENANT_ATTRIBUTION_ENABLED'
| 'EMAIL_SUPPRESSION_TRANSIENT_BOUNCE_THRESHOLD'
| 'CDP_GOOGLE_ADWORDS_DEVELOPER_TOKEN'
| 'CDP_FETCH_RETRIES'
@@ -412,6 +413,7 @@ export function createCdpCoreServices(
sesEndpoint: config.SES_ENDPOINT,
sesTrackedConfigurationSet: config.SES_TRACKED_CONFIGURATION_SET,
sesUntrackedConfigurationSet: config.SES_UNTRACKED_CONFIGURATION_SET,
+ sesTenantAttributionEnabled: config.EMAIL_SES_TENANT_ATTRIBUTION_ENABLED,
},
deps.integrationManager,
teamWorkflowsConfigService,
diff --git a/nodejs/src/cdp/config.ts b/nodejs/src/cdp/config.ts
index 4a52343122fa..5f5cdd523105 100644
--- a/nodejs/src/cdp/config.ts
+++ b/nodejs/src/cdp/config.ts
@@ -139,6 +139,11 @@ export type CdpConfig = ClickhouseConfig & {
// Comma-separated allowlist of SNS Topic ARNs the SES webhook accepts events from. Empty string
// means no restriction (dev/test); production should set this to the workflow SES topic ARN(s).
SES_ALLOWED_SNS_TOPIC_ARNS: string
+ // When true, sends carry TenantName (team-<team_id>) so SES attributes reputation per team
+ // and its Standard reputation policy can pause a single tenant instead of the shared account.
+ // Off by default: a send naming a tenant whose identity association is missing fails, so this
+ // flips on only after tenant coverage is verified (migrate_ses_tenants --dry-run comes back empty).
+ EMAIL_SES_TENANT_ATTRIBUTION_ENABLED: boolean
// Consecutive soft bounces before an address is auto-suppressed. Tunable without a deploy.
EMAIL_SUPPRESSION_TRANSIENT_BOUNCE_THRESHOLD: number
@@ -305,6 +310,7 @@ export function getDefaultCdpConfig(): CdpConfig {
SES_TRACKED_CONFIGURATION_SET: 'posthog-messaging',
SES_UNTRACKED_CONFIGURATION_SET: '',
SES_ALLOWED_SNS_TOPIC_ARNS: '',
+ EMAIL_SES_TENANT_ATTRIBUTION_ENABLED: false,
EMAIL_SUPPRESSION_TRANSIENT_BOUNCE_THRESHOLD: 5,
// Destination migration diffing
diff --git a/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts b/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts
index bd5167f375ad..860b545f2f2d 100644
--- a/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts
+++ b/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts
@@ -522,6 +522,7 @@ export function createHogTransformerService(
sesEndpoint: config.SES_ENDPOINT,
sesTrackedConfigurationSet: config.SES_TRACKED_CONFIGURATION_SET,
sesUntrackedConfigurationSet: config.SES_UNTRACKED_CONFIGURATION_SET,
+ sesTenantAttributionEnabled: config.EMAIL_SES_TENANT_ATTRIBUTION_ENABLED,
},
deps.integrationManager,
teamWorkflowsConfigService,
diff --git a/nodejs/src/cdp/services/hog-executor.service.test.ts b/nodejs/src/cdp/services/hog-executor.service.test.ts
index 29703fca6fc7..5169d7a45d52 100644
--- a/nodejs/src/cdp/services/hog-executor.service.test.ts
+++ b/nodejs/src/cdp/services/hog-executor.service.test.ts
@@ -71,6 +71,7 @@ describe('Hog Executor', () => {
sesEndpoint: hub.SES_ENDPOINT,
sesTrackedConfigurationSet: hub.SES_TRACKED_CONFIGURATION_SET,
sesUntrackedConfigurationSet: hub.SES_UNTRACKED_CONFIGURATION_SET,
+ sesTenantAttributionEnabled: hub.EMAIL_SES_TENANT_ATTRIBUTION_ENABLED,
},
hub.integrationManager,
new TeamWorkflowsConfigService(hub.postgres),
diff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
index fa0756f7bb20..fc26611b8662 100644
--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
@@ -60,6 +60,7 @@ describe('HogFunctionHandler', () => {
sesEndpoint: hub.SES_ENDPOINT,
sesTrackedConfigurationSet: hub.SES_TRACKED_CONFIGURATION_SET,
sesUntrackedConfigurationSet: hub.SES_UNTRACKED_CONFIGURATION_SET,
+ sesTenantAttributionEnabled: false,
},
hub.integrationManager,
new TeamWorkflowsConfigService(hub.postgres),
diff --git a/nodejs/src/cdp/services/hogflows/hogflow-executor.service.test.ts b/nodejs/src/cdp/services/hogflows/hogflow-executor.service.test.ts
index 57ccb3c027fd..97ce3d0998d6 100644
--- a/nodejs/src/cdp/services/hogflows/hogflow-executor.service.test.ts
+++ b/nodejs/src/cdp/services/hogflows/hogflow-executor.service.test.ts
@@ -78,6 +78,7 @@ describe('Hogflow Executor', () => {
sesEndpoint: hub.SES_ENDPOINT,
sesTrackedConfigurationSet: hub.SES_TRACKED_CONFIGURATION_SET,
sesUntrackedConfigurationSet: hub.SES_UNTRACKED_CONFIGURATION_SET,
+ sesTenantAttributionEnabled: hub.EMAIL_SES_TENANT_ATTRIBUTION_ENABLED,
},
hub.integrationManager,
new TeamWorkflowsConfigService(hub.postgres),
diff --git a/nodejs/src/cdp/services/messaging/email.service.test.ts b/nodejs/src/cdp/services/messaging/email.service.test.ts
index 9c98f39cbca6..80d645c1880a 100644
--- a/nodejs/src/cdp/services/messaging/email.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/email.service.test.ts
@@ -93,6 +93,7 @@ describe('EmailService', () => {
sesEndpoint: hub.SES_ENDPOINT,
sesTrackedConfigurationSet: hub.SES_TRACKED_CONFIGURATION_SET,
sesUntrackedConfigurationSet: hub.SES_UNTRACKED_CONFIGURATION_SET,
+ sesTenantAttributionEnabled: hub.EMAIL_SES_TENANT_ATTRIBUTION_ENABLED,
},
hub.integrationManager,
new TeamWorkflowsConfigService(hub.postgres),
@@ -117,6 +118,7 @@ describe('EmailService', () => {
sesEndpoint: '',
sesTrackedConfigurationSet: 'posthog-messaging',
sesUntrackedConfigurationSet: '',
+ sesTenantAttributionEnabled: false,
},
hub.integrationManager,
new TeamWorkflowsConfigService(hub.postgres),
@@ -504,6 +506,40 @@ describe('EmailService', () => {
})
})
+ describe('SES tenant attribution', () => {
+ it('attributes the send to the team tenant when enabled', async () => {
+ service['sesConfig'].sesTenantAttributionEnabled = true
+ sendEmailSpy.mockResolvedValue({ MessageId: 'test-message-id' })
+
+ const result = await service.executeSendEmail(invocation)
+
+ expect(result.error).toBeUndefined()
+ const sentCommand = sendEmailSpy.mock.calls[0][0] as { input: any }
+ expect(sentCommand.input.TenantName).toEqual(`team-${team.id}`)
+ })
+
+ it('omits TenantName by default (flag off)', async () => {
+ sendEmailSpy.mockResolvedValue({ MessageId: 'test-message-id' })
+
+ const result = await service.executeSendEmail(invocation)
+
+ expect(result.error).toBeUndefined()
+ const sentCommand = sendEmailSpy.mock.calls[0][0] as { input: any }
+ expect(sentCommand.input.TenantName).toBeUndefined()
+ })
+
+ it('attributes test-panel sends too — they are real SES sends', async () => {
+ service['sesConfig'].sesTenantAttributionEnabled = true
+ sendEmailSpy.mockResolvedValue({ MessageId: 'test-message-id' })
+
+ const result = await service.executeSendEmail(invocation, true)
+
+ expect(result.error).toBeUndefined()
+ const sentCommand = sendEmailSpy.mock.calls[0][0] as { input: any }
+ expect(sentCommand.input.TenantName).toEqual(`team-${team.id}`)
+ })
+ })
+
it('should include cc addresses in SES destination', async () => {
sendEmailSpy.mockResolvedValue({ MessageId: 'test-message-id' })
invocation.queueParameters = createEmailParams({
diff --git a/nodejs/src/cdp/services/messaging/email.service.ts b/nodejs/src/cdp/services/messaging/email.service.ts
index 3fbc1ec5d44d..4c63ec1a00ef 100644
--- a/nodejs/src/cdp/services/messaging/email.service.ts
+++ b/nodejs/src/cdp/services/messaging/email.service.ts
@@ -86,6 +86,9 @@ export interface EmailServiceConfig {
// Configuration set without open/click tracking. Empty means not provisioned: tracking-off
// sends fall back to the tracked set (with a warning) rather than failing.
sesUntrackedConfigurationSet: string
+ // When true, sends carry TenantName so SES attributes reputation per team. Requires every
+ // sending identity to have a tenant resource association — see EMAIL_SES_TENANT_ATTRIBUTION_ENABLED.
+ sesTenantAttributionEnabled: boolean
}
/**
@@ -553,6 +556,20 @@ export class EmailService {
FeedbackForwardingEmailAddress: from.email,
}
+ if (this.sesConfig.sesTenantAttributionEnabled) {
+ // Attributes the send to the team's SES tenant so AWS tracks reputation per team and
+ // its reputation policy can pause one tenant instead of the shared account. `team-<id>`
+ // is the provisioning convention (products/workflows/backend/providers/ses.py and
+ // posthog/management/commands/migrate_ses_tenants.py). Deliberately NOT gated on
+ // isTest: test-panel sends are real over-the-wire SES sends, so leaving them
+ // unattributed would (a) push their bounces onto the shared account's reputation and
+ // (b) let a paused tenant keep sending via "Run test". The isTest skips elsewhere in
+ // this class only shield our internal metrics, a separate concern from SES-side
+ // attribution; test volume is far below the representative volume AWS needs for a
+ // reputation finding.
+ sendEmailParams.TenantName = `team-${result.invocation.teamId}`
+ }
+
// Authoritative tracking-code carrier: a custom MIME header. Header values aren't
// 256-char-bounded the way SES tag values are, so they safely carry the signed code
// (with distinct_id). The configuration set's event destination needs
diff --git a/nodejs/src/cdp/templates/test/test-helpers.ts b/nodejs/src/cdp/templates/test/test-helpers.ts
index bc9f3ea255a1..b34f4e3e61aa 100644
--- a/nodejs/src/cdp/templates/test/test-helpers.ts
+++ b/nodejs/src/cdp/templates/test/test-helpers.ts
@@ -204,6 +204,7 @@ export class TemplateTester {
sesEndpoint: config.SES_ENDPOINT,
sesTrackedConfigurationSet: config.SES_TRACKED_CONFIGURATION_SET,
sesUntrackedConfigurationSet: config.SES_UNTRACKED_CONFIGURATION_SET,
+ sesTenantAttributionEnabled: false,
},
undefined as any,
undefined as any,