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.

fix(messaging): keep the preheader on email templates (#120327)

Harley Alexander0d005d3295ef
diff --git a/nodejs/src/cdp/services/messaging/email.service.test.ts b/nodejs/src/cdp/services/messaging/email.service.test.ts
index b1bd974778f8..235f12d934ea 100644
--- a/nodejs/src/cdp/services/messaging/email.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/email.service.test.ts
@@ -1456,21 +1456,27 @@ describe('EmailService', () => {
expect(result.error).toBeUndefined()
const sentCommand = sendEmailSpy.mock.calls[0][0] as { input: any }
const htmlData = sentCommand.input.Content.Simple.Body.Html.Data
- expect(htmlData).not.toContain('<tbody><span')
+ expect(htmlData).not.toContain('mso-hide:all')
})
- it('should include preheader at top of HTML if in params', async () => {
+ it.each([
+ [
+ 'a document',
+ '<html><body class="email"><table><tbody><tr><td>Hi</td></tr></tbody></table></body></html>',
+ /^<html><body class="email"><span style=".*">This is a preview text<\/span><table>/,
+ ],
+ ['a fragment without tbody', '<p>Hi</p>', /^<span style=".*">This is a preview text<\/span><p>Hi<\/p>$/],
+ ])('should include preheader at the start of the body of %s', async (_name, html, expected) => {
sendEmailSpy.mockResolvedValue({ MessageId: 'test-message-id' })
invocation.queueParameters = createEmailParams({
from: { integrationId: 1 },
- html: '<tbody>Test email content</tbody>',
+ html,
preheader: 'This is a preview text',
})
const result = await service.executeSendEmail(invocation)
expect(result.error).toBeUndefined()
const sentCommand = sendEmailSpy.mock.calls[0][0] as { input: any }
- const htmlData = sentCommand.input.Content.Simple.Body.Html.Data
- expect(htmlData).toMatch(/<tbody><span style=".*">This is a preview text<\/span>/)
+ expect(sentCommand.input.Content.Simple.Body.Html.Data).toMatch(expected)
})
it('should include unsubscribe headers for non-transactional emails', async () => {
diff --git a/nodejs/src/cdp/services/messaging/helpers/preheader.ts b/nodejs/src/cdp/services/messaging/helpers/preheader.ts
index d77b91c21516..5b76c93bea6f 100644
--- a/nodejs/src/cdp/services/messaging/helpers/preheader.ts
+++ b/nodejs/src/cdp/services/messaging/helpers/preheader.ts
@@ -1,8 +1,17 @@
+const BODY_OPEN_TAG = /<body\b[^>]*>/i
+
export const maybeAddPreheaderToEmail = (html: string, preheader?: string): string => {
if (!preheader) {
return html
}
const preheaderHtml = `<span style="display:none !important;visibility:hidden;mso-hide:all;font-size:1px;color:#ffffff;line-height:1px;max-height:0px;max-width:0px;opacity:0;overflow:hidden;">${preheader}</span>`
- return html.replace('<tbody>', `<tbody>${preheaderHtml}`)
+ // Inboxes take the preview from the first text in the body, so the hidden text goes right after
+ // the opening body tag. HTML without one, such as a fragment saved through the API, gets it first.
+ const bodyOpen = BODY_OPEN_TAG.exec(html)
+ if (!bodyOpen) {
+ return preheaderHtml + html
+ }
+ const insertAt = bodyOpen.index + bodyOpen[0].length
+ return html.slice(0, insertAt) + preheaderHtml + html.slice(insertAt)
}
diff --git a/products/messaging/backend/presentation/views/message_templates.py b/products/messaging/backend/presentation/views/message_templates.py
index cc7de29629b9..2a2980ea374c 100644
--- a/products/messaging/backend/presentation/views/message_templates.py
+++ b/products/messaging/backend/presentation/views/message_templates.py
@@ -119,6 +119,11 @@ class EmailTemplateSerializer(serializers.Serializer):
allow_blank=True,
help_text="Plain-text fallback body for clients that can't render the email.",
)
+ preheader = serializers.CharField(
+ required=False,
+ allow_blank=True,
+ help_text="Preview text that most inboxes show after the subject. Supports Liquid templating.",
+ )
html = serializers.CharField(
required=False,
allow_blank=True,
diff --git a/products/messaging/backend/tests/api/test_message_templates.py b/products/messaging/backend/tests/api/test_message_templates.py
index faba06d4866a..e02d8f6fc09b 100644
--- a/products/messaging/backend/tests/api/test_message_templates.py
+++ b/products/messaging/backend/tests/api/test_message_templates.py
@@ -237,7 +237,14 @@ class TestMessageTemplatesAPI(APIBaseTest):
f"/api/projects/{self.team.id}/messaging_templates/",
data={
"name": "Editor-saved template",
- "content": {"email": {"subject": "Hi", "html": "<p>Editor export</p>", "design": MINIMAL_DESIGN}},
+ "content": {
+ "email": {
+ "subject": "Hi",
+ "preheader": "A quick heads-up",
+ "html": "<p>Editor export</p>",
+ "design": MINIMAL_DESIGN,
+ }
+ },
"type": "email",
},
format="json",
@@ -245,7 +252,9 @@ class TestMessageTemplatesAPI(APIBaseTest):
assert response.status_code == status.HTTP_201_CREATED, response.json()
mock_render.assert_not_called()
- assert response.json()["content"]["email"]["html"] == "<p>Editor export</p>"
+ email = MessageTemplate.objects.get(id=response.json()["id"]).content["email"]
+ assert email["html"] == "<p>Editor export</p>"
+ assert email["preheader"] == "A quick heads-up"
@parameterized.expand(
[
diff --git a/products/messaging/frontend/generated/api.schemas.ts b/products/messaging/frontend/generated/api.schemas.ts
index 522d390a74cb..9721de92e8d2 100644
--- a/products/messaging/frontend/generated/api.schemas.ts
+++ b/products/messaging/frontend/generated/api.schemas.ts
@@ -297,6 +297,8 @@ export interface EmailTemplateApi {
subject?: string
/** Plain-text fallback body for clients that can't render the email. */
text?: string
+ /** Preview text that most inboxes show after the subject. Supports Liquid templating. */
+ preheader?: string
/** Rendered email body — derived from the design at save time. The visual editor's save path supplies it directly; omit it otherwise. */
html?: string
/** Design JSON for PostHog's visual email editor — the authoring surface and source of truth. The server renders the sent email from it, and it opens as editable blocks in the editor. Full schema in the designing-email-templates skill. */
diff --git a/products/messaging/frontend/generated/api.zod.ts b/products/messaging/frontend/generated/api.zod.ts
index 3ed7cfe4dc5a..d94129d9c234 100644
--- a/products/messaging/frontend/generated/api.zod.ts
+++ b/products/messaging/frontend/generated/api.zod.ts
@@ -286,6 +286,12 @@ export const MessagingTemplatesCreateBody = /* @__PURE__ */ zod.object({
.string()
.optional()
.describe("Plain-text fallback body for clients that can't render the email."),
+ preheader: zod
+ .string()
+ .optional()
+ .describe(
+ 'Preview text that most inboxes show after the subject. Supports Liquid templating.'
+ ),
html: zod
.string()
.optional()
@@ -363,6 +369,12 @@ export const MessagingTemplatesCreateBody = /* @__PURE__ */ zod.object({
.string()
.optional()
.describe("Plain-text fallback body for clients that can't render the email."),
+ preheader: zod
+ .string()
+ .optional()
+ .describe(
+ 'Preview text that most inboxes show after the subject. Supports Liquid templating.'
+ ),
html: zod
.string()
.optional()
@@ -448,6 +460,12 @@ export const MessagingTemplatesUpdateBody = /* @__PURE__ */ zod.object({
.string()
.optional()
.describe("Plain-text fallback body for clients that can't render the email."),
+ preheader: zod
+ .string()
+ .optional()
+ .describe(
+ 'Preview text that most inboxes show after the subject. Supports Liquid templating.'
+ ),
html: zod
.string()
.optional()
@@ -525,6 +543,12 @@ export const MessagingTemplatesUpdateBody = /* @__PURE__ */ zod.object({
.string()
.optional()
.describe("Plain-text fallback body for clients that can't render the email."),
+ preheader: zod
+ .string()
+ .optional()
+ .describe(
+ 'Preview text that most inboxes show after the subject. Supports Liquid templating.'
+ ),
html: zod
.string()
.optional()
@@ -611,6 +635,12 @@ export const MessagingTemplatesPartialUpdateBody = /* @__PURE__ */ zod.object({
.string()
.optional()
.describe("Plain-text fallback body for clients that can't render the email."),
+ preheader: zod
+ .string()
+ .optional()
+ .describe(
+ 'Preview text that most inboxes show after the subject. Supports Liquid templating.'
+ ),
html: zod
.string()
.optional()
@@ -688,6 +718,12 @@ export const MessagingTemplatesPartialUpdateBody = /* @__PURE__ */ zod.object({
.string()
.optional()
.describe("Plain-text fallback body for clients that can't render the email."),
+ preheader: zod
+ .string()
+ .optional()
+ .describe(
+ 'Preview text that most inboxes show after the subject. Supports Liquid templating.'
+ ),
html: zod
.string()
.optional()
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 7330d20d293b..c015f0cb7c76 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -39474,6 +39474,8 @@ export namespace Schemas {
subject?: string;
/** Plain-text fallback body for clients that can't render the email. */
text?: string;
+ /** Preview text that most inboxes show after the subject. Supports Liquid templating. */
+ preheader?: string;
/** Rendered email body — derived from the design at save time. The visual editor's save path supplies it directly; omit it otherwise. */
html?: string;
/** Design JSON for PostHog's visual email editor — the authoring surface and source of truth. The server renders the sent email from it, and it opens as editable blocks in the editor. Full schema in the designing-email-templates skill. */
diff --git a/services/mcp/src/generated/email_templates/api.ts b/services/mcp/src/generated/email_templates/api.ts
index 7a6f0e22cce9..b14e2124fd18 100644
--- a/services/mcp/src/generated/email_templates/api.ts
+++ b/services/mcp/src/generated/email_templates/api.ts
@@ -62,6 +62,12 @@ export const MessagingTemplatesCreateBody = () => zod.object({
.string()
.optional()
.describe("Plain-text fallback body for clients that can't render the email."),
+ preheader: zod
+ .string()
+ .optional()
+ .describe(
+ 'Preview text that most inboxes show after the subject. Supports Liquid templating.'
+ ),
design: zod
.object({
counters: zod
@@ -164,6 +170,12 @@ export const MessagingTemplatesPartialUpdateBody = () => zod.object({
.string()
.optional()
.describe("Plain-text fallback body for clients that can't render the email."),
+ preheader: zod
+ .string()
+ .optional()
+ .describe(
+ 'Preview text that most inboxes show after the subject. Supports Liquid templating.'
+ ),
design: zod
.object({
counters: zod
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/workflows-create-email-template.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/workflows-create-email-template.json
index 1ba7d6a4ac98..3c07f5185db1 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/workflows-create-email-template.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/workflows-create-email-template.json
@@ -66,6 +66,10 @@
"required": ["schemaVersion", "body"],
"type": "object"
},
+ "preheader": {
+ "description": "Preview text that most inboxes show after the subject. Supports Liquid templating.",
+ "type": "string"
+ },
"subject": {
"description": "Email subject line. Supports Liquid templating. Required in `content` for email-type templates. A `draft_content` may leave it blank until it is published.",
"type": "string"
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/workflows-update-email-template.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/workflows-update-email-template.json
index 83ba50b4ca0c..ca9e1972466e 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/workflows-update-email-template.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/workflows-update-email-template.json
@@ -66,6 +66,10 @@
"required": ["schemaVersion", "body"],
"type": "object"
},
+ "preheader": {
+ "description": "Preview text that most inboxes show after the subject. Supports Liquid templating.",
+ "type": "string"
+ },
"subject": {
"description": "Email subject line. Supports Liquid templating. Required in `content` for email-type templates. A `draft_content` may leave it blank until it is published.",
"type": "string"