diff --git a/.github/scripts/check_strict_types.sh b/.github/scripts/check_strict_types.sh index d1647516b..149b10d86 100755 --- a/.github/scripts/check_strict_types.sh +++ b/.github/scripts/check_strict_types.sh @@ -12,6 +12,7 @@ python -m venv "$tmp/.venv" cat > "$tmp/strict_posthog_types.py" <<'PY' # pyright: strict import atexit +from datetime import datetime, timezone import posthog from posthog import FeatureFlagEvaluations, FlagValue, Posthog @@ -50,11 +51,14 @@ invite_send: SendEmailResult = emails.send(to="user@example.com", template_key=" tracked_send: SendEmailResult = emails.send(to="user@example.com", template_key="welcome", subject="Welcome", html="

Hi

", tracking={"clicks": True, "engagement": False}) untracked_send: SendEmailResult = emails.send(to="user@example.com", template_key="welcome", subject="Welcome", html="

Hi

", tracking=False) not_sensitive_send: SendEmailResult = emails.send(to="user@example.com", template_key="newsletter", subject="News", html="

News

", sensitive=False) -_ = (template_send, rendered_send, invite_send, tracked_send, untracked_send, not_sensitive_send) +scheduled_send: SendEmailResult = emails.send(to="user@example.com", template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", scheduled_at=datetime(2026, 12, 1, tzinfo=timezone.utc)) +scheduled_status: str = scheduled_send.status +scheduled_time: datetime | None = scheduled_send.scheduled_at +_ = (template_send, rendered_send, invite_send, tracked_send, untracked_send, not_sensitive_send, scheduled_status, scheduled_time) async def async_email_types() -> None: - sent: SendEmailResult = await AsyncEmails("phs_example").send(to="user@example.com", template_key="welcome", subject="Hi", html="

Hi

") + sent: SendEmailResult = await AsyncEmails("phs_example").send(to="user@example.com", template_key="welcome", subject="Hi", html="

Hi

", scheduled_at="2026-12-01T09:00:00Z") _ = sent @@ -114,6 +118,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", sensitive=False) # expected-type-error Emails("phs_example").send(to="user@example.com", template_key="welcome", subject="Hi", html="

Hi

", tracking={"open": False}) # expected-type-error Emails("phs_example").send(to="user@example.com", template_key="welcome", subject="Hi", html="

Hi

", template_data={"name": "Ada"}) # expected-type-error +Emails("phs_example").send(to="user@example.com", template_key="welcome", subject="Hi", html="

Hi

", scheduled_at=1796115600) # expected-type-error Emails("phs_example").send(to="user@example.com", template_key="welcome", subject="Hi", html="

Hi

", attachments=[{"filename": "a.txt", "content": [104, 105]}]) # expected-type-error Emails("phs_example").send(to="user@example.com", template_key="welcome", subject="Hi", html="

Hi

", attachments=[{"filename": "a.txt", "content": b"hi", "path": "/tmp/a.txt"}]) # expected-type-error Emails("phs_example").send(to="user@example.com", template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", attachments=[{"filename": "a.txt", "content": b"hi"}]) # expected-type-error diff --git a/.sampo/changesets/scheduled-email-sends.md b/.sampo/changesets/scheduled-email-sends.md new file mode 100644 index 000000000..338ded9f2 --- /dev/null +++ b/.sampo/changesets/scheduled-email-sends.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: minor +--- + +Schedule a transactional email with `scheduled_at` on `emails.send()`, up to 7 days ahead, on PostHog Cloud. A naive `datetime`, a string without an offset, or a client whose host isn't PostHog Cloud (`scheduling_requires_cloud`) raises before any request. The client rounds the time up to the millisecond and checks that PostHog confirmed it, raising `schedule_not_confirmed` with the email's `message_id` otherwise. Send results now carry `status` and `scheduled_at`. diff --git a/posthog/emails.py b/posthog/emails.py index c180a9224..6db60413b 100644 --- a/posthog/emails.py +++ b/posthog/emails.py @@ -111,6 +111,38 @@ ``not_found`` (404) for an unknown ID, an email older than 30 days, or for a few seconds after the send. +Scheduling: pass ``scheduled_at``, an aware ``datetime`` or an ISO 8601 string +such as ``2026-12-01T09:00:00Z`` or ``2026-12-01 11:00:00.5+02:00``, to send the +email up to 7 days later. Only PostHog Cloud +schedules emails: on any other app host, such as a reverse proxy or a custom +domain, a send with ``scheduled_at`` raises ``scheduling_requires_cloud`` +before any request. A naive ``datetime``, or a string that isn't a date, a +``T`` or a space, a time and ``Z`` or a ``+HH:MM`` offset, raises +``ValueError`` before any request. The client rounds the time up to the next +millisecond, so the email never goes out early. A result with ``status`` +``"scheduled"`` carries the confirmed ``scheduled_at``; ``get`` reads +``scheduled`` until the email goes out. A time that has already passed sends +the email now, and the result's ``status`` is ``"queued"``. The client accepts +that answer only for a time at most 60 seconds ahead of its clock at the send's +first attempt, however long the answer or a retry takes: for a time further +ahead it raises ``schedule_not_confirmed``, so an email PostHog didn't +schedule never reads as scheduled or sent on time. Compute the time +once and reuse it on your own retries: the idempotency key covers it, so the +same key with another time raises ``idempotency_key_reused``. To stop scheduled +emails before they go out, disable the email's workflow in PostHog until they +have reached a final status. That refuses every new send of it meanwhile. + +```python +from datetime import datetime, timedelta, timezone + +result = posthog.emails.send( + to="user@example.com", + template_id="