: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): skip email sends with no recipient address (#120078)
Meikel Ratzd93d313a7bcb
diff --git a/nodejs/src/cdp/services/messaging/email-validation.service.ts b/nodejs/src/cdp/services/messaging/email-validation.service.tsindex fd1eafde3e3c..29bb984b6968 100644--- a/nodejs/src/cdp/services/messaging/email-validation.service.ts+++ b/nodejs/src/cdp/services/messaging/email-validation.service.ts@@ -100,7 +100,7 @@ export class EmailValidationService {const rawEmail = invocation.state.globals.inputs?.email?.to?.emailif (typeof rawEmail !== 'string' || rawEmail.trim().length === 0) {- // Missing recipient is handled (and surfaced) by the existing opt-out check.+ // EmailService skips a missing recipient at send time and records email_missing_recipient.return null}const email = rawEmail.trim()diff --git a/nodejs/src/cdp/services/messaging/email.service.test.ts b/nodejs/src/cdp/services/messaging/email.service.test.tsindex cc3d8dee6d85..cf75681706d2 100644--- a/nodejs/src/cdp/services/messaging/email.service.test.ts+++ b/nodejs/src/cdp/services/messaging/email.service.test.ts@@ -1085,6 +1085,28 @@ describe('EmailService', () => {})})+ // A person without an email property renders an empty `to`. Counting that as email_failed+ // lets one project's data trip the platform send failure rate alert.+ it.each([+ ['an empty string', ''],+ ['only whitespace', ' '],+ ['no value', undefined],+ ])('skips the send without SES or email_failed when the recipient is %s', async (_label, email) => {+ invocation.queueParameters = createEmailParams({+ from: { integrationId: 1 },+ to: { email: email as string, name: 'Test User' },+ })+ sendEmailSpy.mockResolvedValue({ MessageId: 'test-message-id' })++ const result = await service.executeSendEmail(invocation)++ expect(sendEmailSpy).not.toHaveBeenCalled()+ expect(result.error).toBeUndefined()+ expect(result.metrics.map((m) => m.metric_name)).toEqual(['email_missing_recipient'])+ expect(result.skipped).toBe(true)+ expect(invocation.state.vmState?.stack).toEqual([{ success: false }])+ })+describe('SES tenant attribution', () => {it('attributes the send to the team tenant', async () => {sendEmailSpy.mockResolvedValue({ MessageId: 'test-message-id' })diff --git a/nodejs/src/cdp/services/messaging/email.service.ts b/nodejs/src/cdp/services/messaging/email.service.tsindex 0fda911ecf0e..c739a9693862 100644--- a/nodejs/src/cdp/services/messaging/email.service.ts+++ b/nodejs/src/cdp/services/messaging/email.service.ts@@ -479,6 +479,30 @@ export class EmailService {const from = this.resolveFromSender(integration, params.from, addLog)+ // An empty `to` comes from the customer's data (a person without an email property, or no+ // person at all), not from a fault in the send path. SES rejects it as an invalid address,+ // so record a skip with its own metric instead of an `email_failed`. Otherwise one project+ // with bad data inflates the platform send failure rate.+ if (!params.to?.email?.trim()) {+ addLog(+ 'warn',+ 'Skipping send: the recipient has no email address. Add a filter so only people with an email address reach this step.'+ )+ if (!isTest) {+ result.metrics.push({+ team_id: invocation.teamId,+ app_source_id: invocation.parentRunId ?? invocation.functionId,+ instance_id: invocation.state.actionId || invocation.id,+ metric_kind: 'email',+ metric_name: 'email_missing_recipient',+ count: 1,+ })+ }+ result.skipped = true+ result.invocation.state.vmState?.stack.push({ success: false })+ return result+ }+// Single choke point for the suppression check — every send path lands here regardless// of whether the invocation came from a workflow action or an email destination hog// function. Checking here means callers can't bypass it by taking a different upstreamdiff --git a/nodejs/src/cdp/services/messaging/recipient-preferences.service.test.ts b/nodejs/src/cdp/services/messaging/recipient-preferences.service.test.tsindex 07a804a34db1..09324e04647b 100644--- a/nodejs/src/cdp/services/messaging/recipient-preferences.service.test.ts+++ b/nodejs/src/cdp/services/messaging/recipient-preferences.service.test.ts@@ -232,13 +232,12 @@ describe('RecipientPreferencesService', () => {loggerSpy.mockRestore()})- it('should throw error if no email identifier is found', async () => {+ it('should not skip when no email identifier is found, so the send records the missing recipient', async () => {const action = createEmailAction('', '123e4567-e89b-12d3-a456-426614174000')const invocation = createFunctionStepInvocation(action)- await expect(service.shouldSkipAction(invocation, action)).rejects.toThrow(- `No recipient identifier found for message action [Action:${action.id}]. Check that the message 'to' field is set correctly for this person.`- )+ await expect(service.shouldSkipAction(invocation, action)).resolves.toBeNull()+ expect(mockRecipientsManagerGet).not.toHaveBeenCalled()})it('should return true if recipient is opted out of all marketing messaging', async () => {diff --git a/nodejs/src/cdp/services/messaging/recipient-preferences.service.ts b/nodejs/src/cdp/services/messaging/recipient-preferences.service.tsindex 36aa833071bf..3be0c81408d2 100644--- a/nodejs/src/cdp/services/messaging/recipient-preferences.service.ts+++ b/nodejs/src/cdp/services/messaging/recipient-preferences.service.ts@@ -177,6 +177,10 @@ export class RecipientPreferencesService {}if (!identifier) {+ // EmailService skips a send with no `to` and records email_missing_recipient, so let it through.+ if (action.type === 'function_email') {+ return false+ }throw new Error(`No recipient identifier found for message action [Action:${action.id}]. Check that the message 'to' field is set correctly for this person.`)diff --git a/nodejs/src/cdp/types.ts b/nodejs/src/cdp/types.tsindex 701531552087..858ef2adb310 100644--- a/nodejs/src/cdp/types.ts+++ b/nodejs/src/cdp/types.ts@@ -263,6 +263,7 @@ export type MinimalAppMetric = {| 'email_bounce_prevented'| 'message_frequency_capped'| 'email_suppressed'+ | 'email_missing_recipient'| 'email_suspended'| 'email_paused'| 'email_blocked'