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(conversations): add customer_email_verified to ticket events (#117455)

Tom Piccirello11de5aa1d979
diff --git a/products/conversations/backend/api/tests/test_email_endpoints.py b/products/conversations/backend/api/tests/test_email_endpoints.py
index 0ec237854efc..49888f8c6aaf 100644
--- a/products/conversations/backend/api/tests/test_email_endpoints.py
+++ b/products/conversations/backend/api/tests/test_email_endpoints.py
@@ -2763,6 +2763,9 @@ class TestEmailInboundTeamMemberDetection(MailgunWebhookTestMixin, BaseTest):
)
ticket = Ticket.objects.get(team=self.team)
assert ticket.identity_verified is expected_verified
+ comment = Comment.objects.get(team=self.team, scope="conversations_ticket")
+ assert isinstance(comment.item_context, dict)
+ assert comment.item_context["email_sender_authenticated"] is expected_verified
def test_no_spf_treated_as_customer(self):
"""From header matches a team member but no SPF pass → customer."""
diff --git a/products/conversations/backend/api/tests/test_events.py b/products/conversations/backend/api/tests/test_events.py
index f336fa0bd8fb..186bc9bdbfd2 100644
--- a/products/conversations/backend/api/tests/test_events.py
+++ b/products/conversations/backend/api/tests/test_events.py
@@ -16,6 +16,7 @@ from posthog.settings import SITE_URL
from products.access_control.backend.models.role import Role
from products.conversations.backend.events import (
EVENT_SOURCE,
+ _get_customer_properties,
_resolve_groups_from_analytics,
capture_message_received,
capture_message_sent,
@@ -246,6 +247,77 @@ class TestConversationEvents(BaseTest):
call_kwargs = mock_capture.call_args.kwargs
assert call_kwargs["properties"]["customer_email"] == "[email protected]"
+ @parameterized.expand(
+ [
+ ("authenticated_email", "email", True, "[email protected]", "[email protected]", True),
+ ("outbound_email_verified_by_reply", "email", True, "recipient-analytics-id", "[email protected]", True),
+ ("non_string_trait_email", "email", True, "[email protected]", 42, True),
+ ("widget_form_email", "widget", True, "customer-123", "[email protected]", False),
+ ("unauthenticated_email", "email", False, "[email protected]", "[email protected]", False),
+ ]
+ )
+ def test_customer_email_verified_requires_attested_email(
+ self, _name, channel_source, identity_verified, distinct_id, trait_email, expected
+ ):
+ ticket = Ticket(
+ team=self.team,
+ channel_source=channel_source,
+ identity_verified=identity_verified,
+ distinct_id=distinct_id,
+ anonymous_traits={"email": trait_email},
+ email_from="[email protected]",
+ )
+
+ assert _get_customer_properties(ticket)["customer_email_verified"] is expected
+
+ @parameterized.expand(
+ [
+ (
+ "email_authenticated_customer",
+ "email",
+ {"email_from": "[email protected]", "email_sender_authenticated": True},
+ True,
+ ),
+ (
+ "email_unauthenticated_customer_address",
+ "email",
+ {"email_from": "[email protected]", "email_sender_authenticated": False},
+ False,
+ ),
+ (
+ "email_other_participant",
+ "email",
+ {"email_from": "[email protected]", "email_sender_authenticated": True},
+ False,
+ ),
+ ("slack_ticket_customer", "slack", {"slack_author_email": "[email protected]"}, True),
+ ("slack_other_thread_member", "slack", {"slack_author_email": "[email protected]"}, False),
+ ("teams_other_thread_member", "teams", {"teams_author_email": "[email protected]"}, False),
+ (
+ "widget_signed_customer",
+ "widget",
+ {"distinct_id": "[email protected]", "distinct_id_verified": True},
+ True,
+ ),
+ ("widget_unsigned_customer_id", "widget", {"distinct_id": "[email protected]"}, False),
+ ]
+ )
+ @patch("products.conversations.backend.events.capture_internal")
+ def test_message_received_verifies_email_only_for_the_ticket_customer(
+ self, _name, channel_source, item_context, expected, mock_capture
+ ):
+ self.ticket.channel_source = channel_source
+ self.ticket.identity_verified = True
+ self.ticket.distinct_id = "[email protected]"
+ self.ticket.email_from = "[email protected]"
+ self.ticket.anonymous_traits = {"email": "[email protected]"}
+
+ capture_message_received(
+ self.ticket, "msg-id", "content", item_context={"author_type": "customer", **item_context}
+ )
+
+ assert mock_capture.call_args.kwargs["properties"]["customer_email_verified"] is expected
+
@patch("products.conversations.backend.events.capture_internal")
def test_capture_message_received_uses_team_token(self, mock_capture):
capture_message_received(self.ticket, "msg-456", "Hello support")
diff --git a/products/conversations/backend/api/tests/test_signals.py b/products/conversations/backend/api/tests/test_signals.py
index 8055ebe8b5de..7a00638d6ad1 100644
--- a/products/conversations/backend/api/tests/test_signals.py
+++ b/products/conversations/backend/api/tests/test_signals.py
@@ -80,6 +80,38 @@ class TestTicketMessageSignals(BaseTest):
assert self.ticket.updated_at == comment.created_at
assert self.ticket.unread_customer_count == 0 # Customer messages don't increment this
+ @parameterized.expand(
+ [
+ ("written_by_the_channel", False, True),
+ ("written_by_a_posthog_user", True, False),
+ ]
+ )
+ @patch("products.conversations.backend.events.capture_internal")
+ def test_customer_message_event_checks_the_message_author(
+ self, _name, has_created_by, expected, mock_capture, mock_on_commit
+ ):
+ Ticket.objects.filter(id=self.ticket.id).update(
+ identity_verified=True,
+ distinct_id="[email protected]",
+ anonymous_traits={"email": "[email protected]"},
+ )
+
+ Comment.objects.create(
+ team=self.team,
+ scope="conversations_ticket",
+ item_id=str(self.ticket.id),
+ content="Hello",
+ created_by=self.user if has_created_by else None,
+ item_context={
+ "author_type": "customer",
+ "distinct_id": "[email protected]",
+ "distinct_id_verified": True,
+ "is_private": False,
+ },
+ )
+
+ assert mock_capture.call_args.kwargs["properties"]["customer_email_verified"] is expected
+
@patch("products.conversations.backend.signals.capture_message_received")
@patch("products.conversations.backend.signals.capture_message_sent")
def test_public_workflow_message_counts_without_emitting_a_trigger_event(
diff --git a/products/conversations/backend/api/widget.py b/products/conversations/backend/api/widget.py
index 54e652b82d4f..a92f84e01e51 100644
--- a/products/conversations/backend/api/widget.py
+++ b/products/conversations/backend/api/widget.py
@@ -474,7 +474,12 @@ class WidgetMessageView(APIView):
scope="conversations_ticket",
item_id=str(ticket.id),
content=message_content,
- item_context={"author_type": "customer", "distinct_id": distinct_id, "is_private": False},
+ item_context={
+ "author_type": "customer",
+ "distinct_id": distinct_id,
+ "distinct_id_verified": verified_distinct_id is not None,
+ "is_private": False,
+ },
)
# tickets + messages caches are invalidated by the post_save signal
diff --git a/products/conversations/backend/events.py b/products/conversations/backend/events.py
index 1998618b5c16..9479da90d61f 100644
--- a/products/conversations/backend/events.py
+++ b/products/conversations/backend/events.py
@@ -6,6 +6,7 @@ Staff/system actions use the actor's distinct_id; customer actions use the ticke
Events are sent to the customer's PostHog project via their team's API token.
"""
+from collections.abc import Mapping
from datetime import datetime
from typing import Literal
@@ -412,18 +413,56 @@ def _get_ticket_base_properties(ticket: Ticket) -> dict:
}
+def _is_customer_email_verified(ticket: Ticket, customer_email: str) -> bool:
+ # A server check attests one identity on a verified ticket: the signed widget distinct_id, the
+ # authenticated sender in email_from, or the platform user's email in distinct_id on Slack and
+ # Teams. An outbound email ticket keeps the recipient's analytics ID in distinct_id, so email
+ # tickets use email_from. The widget form can supply any email, so the email counts as verified
+ # only when it is that attested identity.
+ if ticket.channel_source == Channel.EMAIL:
+ attested_identity = ticket.email_from or ticket.distinct_id
+ else:
+ attested_identity = ticket.distinct_id
+ return (
+ ticket.identity_verified is True
+ and bool(customer_email)
+ and customer_email.lower() == (attested_identity or "").lower()
+ )
+
+
def _get_customer_properties(ticket: Ticket, *, include_distinct_id: bool = False) -> dict:
"""Customer identity on the ticket, for workflow filters and analytics."""
traits = ticket.anonymous_traits or {}
+ # The ticket update API stores traits as any JSON value, so the email trait can be a non-string.
+ trait_email = traits.get("email")
+ customer_email = (trait_email if isinstance(trait_email, str) else "") or ticket.email_from or ""
properties = {
"customer_name": traits.get("name", ""),
- "customer_email": traits.get("email") or ticket.email_from or "",
+ "customer_email": customer_email,
+ "customer_email_verified": _is_customer_email_verified(ticket, customer_email),
}
if include_distinct_id:
properties["customer_distinct_id"] = ticket.distinct_id or ""
return properties
+def _message_author_identity(ticket: Ticket, item_context: Mapping[str, object]) -> str:
+ # The ticket's attested identity belongs to the person who opened it, but other Slack or Teams
+ # thread members, other email senders, and an unsigned widget session can add messages to it.
+ author: object = None
+ if ticket.channel_source == Channel.EMAIL:
+ if item_context.get("email_sender_authenticated") is True:
+ author = item_context.get("email_from")
+ elif ticket.channel_source == Channel.SLACK:
+ author = item_context.get("slack_author_email")
+ elif ticket.channel_source == Channel.TEAMS:
+ author = item_context.get("teams_author_email")
+ elif ticket.channel_source == Channel.WIDGET:
+ if item_context.get("distinct_id_verified") is True:
+ author = item_context.get("distinct_id")
+ return author if isinstance(author, str) else ""
+
+
def _get_assignment_properties(ticket: Ticket) -> dict:
"""Current assignment on the ticket, so workflows can route a single team's tickets.
@@ -630,13 +669,21 @@ def capture_private_message_sent(
_capture_team_message(ticket, message_id, None, author, "$conversation_private_message_sent")
-def capture_message_received(ticket: Ticket, message_id: str, message_content: str) -> None:
+def capture_message_received(
+ ticket: Ticket, message_id: str, message_content: str, item_context: Mapping[str, object] | None = None
+) -> None:
"""Customer sent a message on a ticket."""
properties = _get_ticket_base_properties(ticket)
properties["message_id"] = message_id
properties["message_content"] = (message_content or "")[:1000]
properties["author_type"] = "customer"
- properties.update(_get_customer_properties(ticket))
+ customer_properties = _get_customer_properties(ticket)
+ author_identity = _message_author_identity(ticket, item_context or {})
+ customer_properties["customer_email_verified"] = (
+ customer_properties["customer_email_verified"]
+ and author_identity.lower() == customer_properties["customer_email"].lower()
+ )
+ properties.update(customer_properties)
properties.update(_get_assignment_properties(ticket))
team = ticket.team
diff --git a/products/conversations/backend/services/mailgun_events.py b/products/conversations/backend/services/mailgun_events.py
index ddfc75efc6a3..16362fd244a8 100644
--- a/products/conversations/backend/services/mailgun_events.py
+++ b/products/conversations/backend/services/mailgun_events.py
@@ -809,6 +809,7 @@ def _process_support_email(
"is_private": False,
"from_email": True,
"email_from": sender_email,
+ "email_sender_authenticated": email.sender_authenticated,
"email_from_name": sender_name,
"email_relay_from": email.relay_sender.email if email.relay_sender is not None else None,
"email_message_id": email.message_id,
diff --git a/products/conversations/backend/signals.py b/products/conversations/backend/signals.py
index 15261b21c7ab..fb1519ae0201 100644
--- a/products/conversations/backend/signals.py
+++ b/products/conversations/backend/signals.py
@@ -220,7 +220,15 @@ def update_ticket_on_message(sender, instance: Comment, created: bool, **kwargs)
capture_message_sent(ticket, comment_id, content or "", author=author)
elif not is_team_message:
author = None
- capture_message_received(ticket, comment_id, content or "")
+ capture_message_received(
+ ticket,
+ comment_id,
+ content or "",
+ # The comments API stores the request's item_context, and a thread reply copies its
+ # parent's item_context. Only channel handlers write a customer comment with no
+ # created_by, so only those comments can attest the message author.
+ item_context=item_context if isinstance(item_context, dict) and created_by_id is None else None,
+ )
# Internal analytics (PostHog tracking its own usage)
props = {"channel_source": ticket.channel_source}