diff --git a/.sampo/changesets/emails-send-category.md b/.sampo/changesets/emails-send-category.md new file mode 100644 index 000000000..c6b0cadaf --- /dev/null +++ b/.sampo/changesets/emails-send-category.md @@ -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`. diff --git a/posthog/emails.py b/posthog/emails.py index c180a9224..7b50158d2 100644 --- a/posthog/emails.py +++ b/posthog/emails.py @@ -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. @@ -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 @@ -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__) @@ -428,6 +456,7 @@ class CodeEmailArgs(_RecipientArgs): "capture_engagement", "sensitive", "tracking", + "category", } ) @@ -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( diff --git a/posthog/test/test_emails.py b/posthog/test/test_emails.py index 9e1a15db6..ef104becd 100644 --- a/posthog/test/test_emails.py +++ b/posthog/test/test_emails.py @@ -661,6 +661,7 @@ def send_code(emails: Emails, **overrides: Any): "from_": "Acme ", "reply_to": "help@example.com", "capture_engagement": False, + "category": "newsletter", "distinct_id": "user_123", }, { @@ -669,6 +670,7 @@ def send_code(emails: Emails, **overrides: Any): "from": "Acme ", "reply_to": ["help@example.com"], "capture_engagement": False, + "category": "newsletter", "distinct_id": "user_123", }, ), @@ -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}, @@ -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), ] @@ -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"}) diff --git a/references/public_api_snapshot.txt b/references/public_api_snapshot.txt index d6063b7b2..e02518cf8 100644 --- a/references/public_api_snapshot.txt +++ b/references/public_api_snapshot.txt @@ -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]]