diff --git a/.github/scripts/check_strict_types.sh b/.github/scripts/check_strict_types.sh index 149b10d86..65c0e0b40 100755 --- a/.github/scripts/check_strict_types.sh +++ b/.github/scripts/check_strict_types.sh @@ -16,7 +16,7 @@ from datetime import datetime, timezone import posthog from posthog import FeatureFlagEvaluations, FlagValue, Posthog -from posthog.emails import AsyncEmails, Emails, SendEmailResult +from posthog.emails import AsyncEmails, BatchEmailResult, EmailBatchError, EmailError, Emails, SendBatchResult, SendEmailResult from posthog.ai.evaluations import ( AsyncOfflineEvaluations, BooleanScorerConfig, @@ -54,12 +54,23 @@ not_sensitive_send: SendEmailResult = emails.send(to="user@example.com", templat 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) +template_batch: SendBatchResult = emails.batch(template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", template_data={"team": "Data"}, recipients=[{"to": "ada@example.com", "distinct_id": "user_1", "idempotency_key": "invite:user_1", "template_data": {"name": "Ada"}}]) +code_batch: SendBatchResult = emails.batch(template_key="digest", subject="Digest", html="

Digest

", recipients=[{"to": "ada@example.com"}], max_retries=0) +first_batch_result: BatchEmailResult = template_batch.results[0] +batch_status: str = first_batch_result.status +batch_error_field: EmailError | None = first_batch_result.error +try: + emails.batch(template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", recipients=[{"to": "ada@example.com"}]) +except EmailBatchError as batch_error: + failed_results: tuple[BatchEmailResult, ...] = batch_error.results + _ = failed_results +_ = (template_send, rendered_send, invite_send, tracked_send, untracked_send, not_sensitive_send, scheduled_status, scheduled_time, code_batch, batch_status, batch_error_field) async def async_email_types() -> None: 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 + batch: SendBatchResult = await AsyncEmails("phs_example").batch(template_key="welcome", subject="Hi", html="

Hi

", recipients=[{"to": "user@example.com"}]) + _ = (sent, batch) async def offline_evaluation_types( @@ -119,6 +130,10 @@ Emails("phs_example").send(to="user@example.com", template_id="0190f6d2-7c1e-7b3 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").batch(template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", recipients=[{"to": "user@example.com"}], scheduled_at="2026-12-01T09:00:00Z") # expected-type-error +Emails("phs_example").batch(template_key="welcome", subject="Hi", html="

Hi

", recipients=[{"to": "user@example.com"}], cc="team@example.com") # expected-type-error +Emails("phs_example").batch(template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", recipients=[{"email": "user@example.com"}]) # expected-type-error +Emails("phs_example").batch(template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", to="user@example.com", recipients=[{"to": "user@example.com"}]) # 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/email-batch-sends.md b/.sampo/changesets/email-batch-sends.md new file mode 100644 index 000000000..d0a5dd066 --- /dev/null +++ b/.sampo/changesets/email-batch-sends.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: minor +--- + +Send the same transactional email to up to 100 recipients in one request with `emails.batch()`, one email each, from a template with `template_data` per recipient or from content in your code. More than 100 recipients raises `ValueError` before anything is sent. The client gives each recipient an idempotency key that every retry reuses, and returns a result per recipient: `queued`, `failed` or `unconfirmed`. A request that fails as a whole, such as a `413` `content_too_large`, raises `EmailBatchError` with every recipient's result. diff --git a/posthog/emails.py b/posthog/emails.py index f7c5134e9..5d3a7bf70 100644 --- a/posthog/emails.py +++ b/posthog/emails.py @@ -143,6 +143,50 @@ print(result.status, result.scheduled_at) # scheduled, the time in UTC ``` +Batches: ``emails.batch`` sends the same template or content to up to 100 +recipients in one request, one email each, with ``template_data`` per recipient +for a template. It waits 45 seconds for the answer. Each recipient has its own +``idempotency_key``, given or made by the client, that every retry reuses. +Check each result's ``status``: + +- ``queued``: PostHog accepted the email. +- ``failed``: PostHog refused it, and ``error`` says why, such as a suppressed + address. +- ``unconfirmed``: the client can't tell whether PostHog accepted it, such as + after a lost answer or a 409 other than ``batch_unavailable``. Send it again + with the same key within 24 hours of the first request that used it, so it + goes out once. After a lost answer, a later refusal of the whole request + leaves every recipient ``unconfirmed`` too, and its error says some emails + may still go out. + +More than 100 recipients raises ``ValueError`` before anything is sent. A request +that fails as a whole, such as a rate limit past its retries, raises +:class:`EmailBatchError`, whose ``results`` hold every recipient. ``code`` +``content_too_large`` on a 413 means the request was larger than PostHog takes +at once: split the recipients into smaller batches, as its ``fix`` says, and +when the shared content or ``template_data`` alone passes the limit, which +splitting doesn't help, send less of it. On a 400 it means the shared content or +``template_data`` is too large to copy to every recipient, and its ``message`` +says what to send less of. ``batch_unavailable`` (409) means the project +can't send batches yet: send each email the batch didn't queue with ``send`` +and its recipient's ``idempotency_key``. A 404 ``not_found`` that names no +``field`` means PostHog found no template with that ``template_id``, or batch +sends aren't available for the project: check the template ID, and if it's +right, send each recipient on its own with ``send``, the same content and its +recipient's ``idempotency_key``, until batch sends are available. A batch takes no ``cc``, ``bcc``, +``attachments`` or ``scheduled_at``. + +```python +result = posthog.emails.batch( + template_id="