Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
60d6d30
feat(emails): send a message category from code
Silthus Nov 9, 2026
65aba14
feat(emails): define the message category in code
Silthus Nov 10, 2026
faafa5a
Merge remote-tracking branch 'origin/main' into feat/emails-send-cate…
Silthus Nov 10, 2026
073154c
test(emails): send an empty category description to clear it
Silthus Nov 10, 2026
4836173
test(emails): prove categories against a fake of the send API
Silthus Nov 10, 2026
99d6d0e
test(emails): prove a transactional category is created by its send
Silthus Nov 10, 2026
8919451
test(emails): prove transactional is refused on a template in a category
Silthus Nov 10, 2026
d8667f6
fix(emails): say a send's category name and description apply only at…
Silthus Nov 12, 2026
8590215
fix(emails): say a template made in PostHog is transactional only
Silthus Nov 12, 2026
337add9
Merge branch 'main' into feat/emails-send-category
Silthus Nov 23, 2026
0d64cf2
chore(emails): tighten category docs and rewrite category tests on Fa…
Silthus Nov 23, 2026
f871324
chore(emails): match server refusal payload shape and clarify restore…
Silthus Nov 23, 2026
b0fb9f0
chore(emails): document categories_not_enabled prerequisite and tight…
Silthus Nov 23, 2026
b651bca
fix(emails): require exact str for TRANSACTIONAL_CATEGORY and clarify…
Silthus Nov 23, 2026
7e39fae
test(emails): test category.type exact str and qualify template docs
Silthus Nov 23, 2026
bfcae69
fix(emails): preserve str enum support with exact str.__eq__ check
Silthus Nov 23, 2026
bddb973
test(emails): stock second reply and assert no_sleep in refusal test
Silthus Nov 23, 2026
2418295
test(emails): drop mock sleep call assertion in refusal test
Silthus Nov 23, 2026
6d52b88
test(emails): add route-matched categorized attachment send tests
Silthus Nov 23, 2026
b9113dd
test(emails): reuse FakePostHog for categorized attachment tests
Silthus Nov 23, 2026
0178d39
feat(emails): send category as the key of an existing category
Silthus Nov 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/emails-send-category.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: minor
---

`emails.send` takes an optional `category` on the code form, the one with `template_key`: the key of one of the project's message categories, such as `"newsletter"`. PostHog refuses an unknown key with `category_not_found`, and a marketing email with `cc` or `bcc` with `marketing_copies_unsupported`.
31 changes: 31 additions & 0 deletions posthog/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,26 @@
html=rendered_html,
distinct_id="user_123",
)

# Marketing email in one of the project's message categories
posthog.emails.send(
to="user@example.com",
template_key="november-digest",
subject="What's new in November",
html=digest_html,
category="newsletter",
distinct_id="user_123",
)
```

Message categories: ``category`` on a code send is the key of one of the
project's message categories, such as ``newsletter``. A marketing category
skips recipients who opted out of it or of all marketing email, fills
``{{ unsubscribe_url }}`` with the recipient's preferences link, and adds
List-Unsubscribe headers. A marketing email can't have ``cc`` or ``bcc``.
PostHog refuses an unknown key with ``category_not_found``, and a marketing
email with copies with ``marketing_copies_unsupported``.

``posthog.emails`` above is the client's property. For async code, use
``AsyncPosthog`` or :class:`AsyncEmails` with ``posthog[async]`` installed.

Expand Down Expand Up @@ -391,6 +409,15 @@ class CodeEmailArgs(_RecipientArgs):
key you leave out keeps its default. Turn clicks on for a
sensitive email only when its links are safe to send through
PostHog's click tracking. The template form doesn't take it.
category: The key of one of the project's message categories, such
as ``"newsletter"``. A marketing category skips recipients who
opted out of it or of all marketing email, fills
``{{ unsubscribe_url }}`` with the recipient's preferences link,
and adds List-Unsubscribe headers. A marketing email can't have
``cc`` or ``bcc``. PostHog raises ``category_not_found`` when the
project has no category with that key, and
``marketing_copies_unsupported`` for a marketing email with
``cc`` or ``bcc``. The template form doesn't take it.
distinct_id: The recipient's distinct ID in your product.
idempotency_key: 1 to 255 visible ASCII characters. Template sends
and code sends share one set of keys: a key already used for
Expand All @@ -413,6 +440,7 @@ class CodeEmailArgs(_RecipientArgs):
capture_engagement: NotRequired[Optional[bool]]
sensitive: NotRequired[Optional[bool]]
tracking: NotRequired[Optional[Union[bool, EmailTracking]]]
category: NotRequired[Optional[str]]


_ATTACHMENT_FIELDS = frozenset(EmailAttachment.__annotations__)
Expand All @@ -428,6 +456,7 @@ class CodeEmailArgs(_RecipientArgs):
"capture_engagement",
"sensitive",
"tracking",
"category",
}
)

Expand Down Expand Up @@ -543,6 +572,8 @@ def _prepare_code_send(args: Mapping[str, Any]) -> _PreparedSend:
if distinct_id is not None:
payload["distinct_id"] = distinct_id
payload.update(_sender(args))
if "category" in args:
payload["category"] = _nonempty_text(args, "category")
if references:
payload["attachments"] = references
return _PreparedSend(
Expand Down
14 changes: 14 additions & 0 deletions posthog/test/test_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ def send_code(emails: Emails, **overrides: Any):
"from_": "Acme <security@example.com>",
"reply_to": "help@example.com",
"capture_engagement": False,
"category": "newsletter",
"distinct_id": "user_123",
},
{
Expand All @@ -669,6 +670,7 @@ def send_code(emails: Emails, **overrides: Any):
"from": "Acme <security@example.com>",
"reply_to": ["help@example.com"],
"capture_engagement": False,
"category": "newsletter",
"distinct_id": "user_123",
},
),
Expand All @@ -690,6 +692,7 @@ def send_code(emails: Emails, **overrides: Any):
"from_": None,
"reply_to": None,
"capture_engagement": None,
"category": None,
"distinct_id": None,
},
{"html": HTML},
Expand Down Expand Up @@ -926,6 +929,16 @@ def handler(request: httpx.Request) -> httpx.Response:
},
TypeError,
),
(
"category_on_template_form",
{
"template_id": TEMPLATE_ID,
"template_key": None,
"subject": None,
"category": "newsletter",
},
TypeError,
),
("tracking_not_bool_or_dict", {"html": HTML, "tracking": "off"}, TypeError),
("sensitive_not_bool", {"html": HTML, "sensitive": "yes"}, TypeError),
]
Expand Down Expand Up @@ -1077,6 +1090,7 @@ def test_a_refused_send_names_the_field_from_attr():
"tracking",
"distinct_id",
"attachments",
"category",
}
)
REFERENCE_FIELDS = frozenset({"sha256", "filename", "content_type", "content_id"})
Expand Down
1 change: 1 addition & 0 deletions references/public_api_snapshot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ attribute posthog.disabled = False
attribute posthog.emails.CodeEmailArgs.attachments: NotRequired[Optional[Sequence[EmailAttachment]]]
attribute posthog.emails.CodeEmailArgs.bcc: NotRequired[Optional[Union[str, Sequence[str]]]]
attribute posthog.emails.CodeEmailArgs.capture_engagement: NotRequired[Optional[bool]]
attribute posthog.emails.CodeEmailArgs.category: NotRequired[Optional[str]]
attribute posthog.emails.CodeEmailArgs.cc: NotRequired[Optional[Union[str, Sequence[str]]]]
attribute posthog.emails.CodeEmailArgs.from_: NotRequired[Optional[str]]
attribute posthog.emails.CodeEmailArgs.html: NotRequired[Optional[str]]
Expand Down