Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .github/scripts/check_strict_types.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ rendered_send: SendEmailResult = emails.send(to="user@example.com", template_key
invite_send: SendEmailResult = emails.send(to="user@example.com", template_key="invite", subject="Invite", html="<p>Invite</p>", cc="team@example.com", bcc=["audit@example.com"], attachments=[{"filename": "invite.ics", "content": b"BEGIN:VCALENDAR", "content_type": "text/calendar"}, {"filename": "logo.png", "content": "iVBORw0KGgo=", "content_id": "logo"}])
tracked_send: SendEmailResult = emails.send(to="user@example.com", template_key="welcome", subject="Welcome", html="<p>Hi</p>", tracking={"clicks": True, "engagement": False})
untracked_send: SendEmailResult = emails.send(to="user@example.com", template_key="welcome", subject="Welcome", html="<p>Hi</p>", tracking=False)
not_sensitive_send: SendEmailResult = emails.send(to="user@example.com", template_key="newsletter", subject="News", html="<p>News</p>", sensitive=False)
not_sensitive_send: SendEmailResult = emails.send(to="user@example.com", template_key="newsletter", subject="News", html="<p>News</p>", sensitive=False, headers={"List-Unsubscribe": "<https://example.com/unsubscribe>"})
_ = (template_send, rendered_send, invite_send, tracked_send, untracked_send, not_sensitive_send)


Expand Down Expand Up @@ -112,6 +112,7 @@ Emails("phs_example").send(to="user@example.com", template_id="0190f6d2-7c1e-7b3
Emails("phs_example").send(to="user@example.com", template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", capture_engagement=False) # expected-type-error
Emails("phs_example").send(to="user@example.com", template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", tracking=False) # expected-type-error
Emails("phs_example").send(to="user@example.com", template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", sensitive=False) # expected-type-error
Emails("phs_example").send(to="user@example.com", template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", headers={"List-Unsubscribe": "<https://example.com/unsubscribe>"}) # expected-type-error
Emails("phs_example").send(to="user@example.com", template_key="welcome", subject="Hi", html="<p>Hi</p>", tracking={"open": False}) # expected-type-error
Emails("phs_example").send(to="user@example.com", template_key="welcome", subject="Hi", html="<p>Hi</p>", template_data={"name": "Ada"}) # expected-type-error
Emails("phs_example").send(to="user@example.com", template_key="welcome", subject="Hi", html="<p>Hi</p>", attachments=[{"filename": "a.txt", "content": [104, 105]}]) # expected-type-error
Expand Down
5 changes: 5 additions & 0 deletions .sampo/changesets/emails-code-send-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: minor
---

`emails.send`: a code send takes `headers`, such as `List-Unsubscribe` and `List-Unsubscribe-Post` for one-click unsubscribe. The SDK sends them as given, and PostHog refuses a header it doesn't allow. The template form refuses `headers`.
11 changes: 11 additions & 0 deletions posthog/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ 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.
headers: Header names and values to send with the email, such as
``{"List-Unsubscribe": "<https://example.com/unsubscribe>",
"List-Unsubscribe-Post": "List-Unsubscribe=One-Click"}``. PostHog
refuses a header it doesn't allow, with ``code``
``unknown_header``. 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 +418,7 @@ class CodeEmailArgs(_RecipientArgs):
capture_engagement: NotRequired[Optional[bool]]
sensitive: NotRequired[Optional[bool]]
tracking: NotRequired[Optional[Union[bool, EmailTracking]]]
headers: NotRequired[Optional[Mapping[str, str]]]


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

Expand Down Expand Up @@ -582,6 +589,10 @@ def _sender(args: Mapping[str, Any]) -> dict[str, Any]:
sender[flag] = args[flag]
if "tracking" in args:
sender["tracking"] = _tracking(args["tracking"])
if "headers" in args:
if not isinstance(args["headers"], Mapping):
raise TypeError("headers must be a dict of header names and values.")
sender["headers"] = dict(args["headers"])
return sender


Expand Down
21 changes: 21 additions & 0 deletions posthog/test/test_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ def handler(request: httpx.Request) -> httpx.Response:


HTML = "<p>Reset it here.</p>"
UNSUBSCRIBE_HEADERS = {
"List-Unsubscribe": "<https://example.com/unsubscribe?u=1>",
"List-Unsubscribe-Post": "List-Unsubscribe=One-Click",
}
INVITE = b"BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n"
INVITE_BASE64 = "QkVHSU46VkNBTEVOREFSDQpFTkQ6VkNBTEVOREFSDQo="

Expand Down Expand Up @@ -684,6 +688,11 @@ def send_code(emails: Emails, **overrides: Any):
{"html": HTML, "reply_to": []},
{"html": HTML},
),
(
"headers_as_given",
{"html": HTML, "headers": UNSUBSCRIBE_HEADERS},
{"html": HTML, "headers": UNSUBSCRIBE_HEADERS},
),
(
"none_as_left_out",
{
Expand All @@ -692,6 +701,7 @@ def send_code(emails: Emails, **overrides: Any):
"from_": None,
"reply_to": None,
"capture_engagement": None,
"headers": None,
"distinct_id": None,
},
{"html": HTML},
Expand Down Expand Up @@ -928,6 +938,17 @@ def handler(request: httpx.Request) -> httpx.Response:
},
TypeError,
),
(
"headers_on_template_form",
{
"template_id": TEMPLATE_ID,
"template_key": None,
"subject": None,
"headers": UNSUBSCRIBE_HEADERS,
},
TypeError,
),
("headers_not_a_mapping", {"html": HTML, "headers": "x"}, TypeError),
("tracking_not_bool_or_dict", {"html": HTML, "tracking": "off"}, TypeError),
("sensitive_not_bool", {"html": HTML, "sensitive": "yes"}, TypeError),
]
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 @@ -918,6 +918,7 @@ attribute posthog.emails.CodeEmailArgs.bcc: NotRequired[Optional[Union[str, Sequ
attribute posthog.emails.CodeEmailArgs.capture_engagement: NotRequired[Optional[bool]]
attribute posthog.emails.CodeEmailArgs.cc: NotRequired[Optional[Union[str, Sequence[str]]]]
attribute posthog.emails.CodeEmailArgs.from_: NotRequired[Optional[str]]
attribute posthog.emails.CodeEmailArgs.headers: NotRequired[Optional[Mapping[str, str]]]
attribute posthog.emails.CodeEmailArgs.html: NotRequired[Optional[str]]
attribute posthog.emails.CodeEmailArgs.reply_to: NotRequired[Optional[Union[str, Sequence[str]]]]
attribute posthog.emails.CodeEmailArgs.sensitive: NotRequired[Optional[bool]]
Expand Down
Loading