Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
dbd27e1
test(emails): split the compatibility test, check a send without sche…
Silthus Nov 24, 2026
90c013a
test(emails): share the email client fixture and request helpers in a…
Silthus Nov 24, 2026
2da67f3
feat(emails): send one email to each of many recipients with emails()…
Silthus Nov 24, 2026
6057be1
docs(emails): document emails()->batch() in the README
Silthus Nov 24, 2026
39c088e
Merge remote-tracking branch 'origin/feat/scheduled-email-send' into …
Silthus Nov 24, 2026
29b11af
Merge remote-tracking branch 'origin/feat/scheduled-email-send' into …
Silthus Nov 24, 2026
31113aa
fix(emails): keep batch emails unconfirmed after a read by key, build…
Silthus Nov 24, 2026
f4a9e96
docs(emails): use per-run keys in the batch example, warn against reu…
Silthus Nov 24, 2026
3dfd017
chore(emails): drop the prose comments from the internal batch classes
Silthus Nov 24, 2026
697627a
Merge remote-tracking branch 'origin/feat/scheduled-email-send' into …
Silthus Nov 24, 2026
8b0551a
fix(emails): keep batch results when a read by key throws, set later …
Silthus Nov 24, 2026
ccad352
docs(emails): build batch keys from a stored ID, call the reads by ke…
Silthus Nov 24, 2026
41a3acd
fix(emails): diagnose oversized batch content from its only encoding
Silthus Nov 24, 2026
6306072
test(emails): cover a retry that loses its answer again, size the mem…
Silthus Nov 24, 2026
b1dc21f
fix(emails): size shared batch content before building the request
Silthus Nov 24, 2026
ab8f68a
test(emails): cover shared batch content at the 4 MiB request limit
Silthus Nov 24, 2026
e1febea
test(emails): assert the failed status at the shared content limit
Silthus Nov 24, 2026
f70860d
chore: merge feat/scheduled-email-send into feat/email-batch
Silthus Nov 24, 2026
691dc68
fix(emails): name the right field when batch content is too large
Silthus Nov 24, 2026
ec1b512
fix(emails): size each recipient field once and name the largest
Silthus Nov 24, 2026
8bdde88
fix(emails): stop reading unconfirmed batch emails back by key
Silthus Nov 24, 2026
70a3be6
Merge remote-tracking branch 'origin/feat/scheduled-email-send' into …
Silthus Nov 24, 2026
15ca143
fix(emails): free each batch request and keep the in-flight cause
Silthus Nov 24, 2026
629afe9
fix(emails): join each batch recipient's json in one allocation
Silthus Nov 24, 2026
ab07a4a
Merge remote-tracking branch 'origin/feat/scheduled-email-send' into …
Silthus Nov 24, 2026
8c9d680
fix(emails): tell batch recipients left in flight to reuse their key
Silthus Nov 24, 2026
19a32cd
Merge remote-tracking branch 'origin/feat/scheduled-email-send' into …
Silthus Nov 24, 2026
d191189
fix(emails): treat only a 409 in flight as unconfirmed in a batch
Silthus Nov 24, 2026
9caa57c
Merge remote-tracking branch 'origin/feat/scheduled-email-send' into …
Silthus Nov 24, 2026
f0ec7b2
fix(emails): refuse batch recipients nested past the json depth
Silthus Nov 24, 2026
35871bc
Merge remote-tracking branch 'origin/feat/scheduled-email-send' into …
Silthus Nov 24, 2026
20f6fe4
Merge remote-tracking branch 'origin/feat/scheduled-email-send' into …
Silthus Nov 24, 2026
de0ad88
feat(emails): send a batch as one request of at most 100 recipients
Silthus Nov 24, 2026
de527e6
docs(emails): name PostHog's 413 code content_too_large for batches
Silthus Nov 24, 2026
5b27443
Merge remote-tracking branch 'origin/feat/scheduled-email-send' into …
Silthus Nov 24, 2026
42228fc
fix(emails): claim a batch key in flight only on a 409
Silthus Nov 24, 2026
5941080
fix(emails): keep retried batch recipients unconfirmed, build body once
Silthus Nov 24, 2026
ea0aaef
docs(emails): say a 400 content_too_large wants smaller or single sends
Silthus Nov 24, 2026
efdab6e
fix(emails): encode each batch recipient into the body, advise resends
Silthus Nov 24, 2026
0ea8d16
fix(emails): encode a batch request once, keep refusals on recipients
Silthus Nov 24, 2026
c17dafd
fix(emails): never send a batch after a failed encode, dedupe warnings
Silthus Nov 24, 2026
d28b91b
fix(emails): refuse an unencodable batch even if a later encode throws
Silthus Nov 24, 2026
7951356
fix(emails): report a batch that fails to encode without encoding it …
Silthus Nov 24, 2026
b5bf73a
docs(emails): say an unconfirmed batch result can carry any code
Silthus Nov 24, 2026
7cf8fb2
test(emails): cover a shutdown after a lost batch answer and an in-fl…
Silthus Nov 24, 2026
685605c
fix(emails): keep batch keys when the retry wait fails
Silthus Nov 25, 2026
cbbaae3
fix(emails): keep a batch unconfirmed when reading a lost answer throws
Silthus Nov 25, 2026
f03ff9b
fix(emails): keep batch warnings and retry waits portable
Silthus Nov 25, 2026
7d94ee4
fix(emails): name a 409 batch_unavailable a validation error
Silthus Nov 25, 2026
6dcfb43
fix(emails): keep a batch unconfirmed when the client fails mid-send
Silthus Nov 25, 2026
436bc10
fix(emails): keep PostHog's in-flight answer when a batch retry fails
Silthus Nov 25, 2026
240916a
fix(emails): point a batch 404 without a field at the template ID
Silthus Nov 25, 2026
37dd76a
fix(emails): don't say a retried batch sent nothing after a lost answer
Silthus Nov 25, 2026
f661ea5
fix(emails): keep batch recipients unconfirmed on unclear 409s and re…
Silthus Nov 26, 2026
6e7ff9e
chore(emails): merge the scheduled send PR into the batch PR
Silthus Nov 26, 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 .changeset/email-batch-sends.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-php": minor
---

Transactional email: `emails()->batch()` sends the same email to many recipients, one email each, from a template with `template_data` per recipient or from content in your code. A call takes up to 100 recipients and sends one request. The client gives each recipient an idempotency key that every retry reuses, and returns a `SendBatchResult` with a `BatchEmailResult` per recipient: `queued`, `failed` or `unconfirmed`. A batch PostHog refuses as a whole, such as `content_too_large`, throws `EmailBatchError` with every recipient's result.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Existing span attributes take precedence. The processor preserves the applicatio

## Transactional email

`emails()` sends one email to one recipient, from an email template made transactional in PostHog or from content your code rendered, and reads what happened to it.
`emails()` sends one email to one recipient, or the same email to many recipients, one email each, from an email template made transactional in PostHog or from content your code rendered, and reads what happened to it.

The client needs a project secret API key (`phs_...`) as `secretKey`. Create it with the "Send transactional email" preset (`transactional_email:write`) in your project settings, and add `transactional_email:read` if you read statuses with a key that can't send. Keys made with `hog_flow:write` still work. A client whose `host` is PostHog Cloud sends to its region's app host. For any other `host`, such as a reverse proxy, set the `app_host` option to your PostHog app host: email requests never go to a custom `host`. Loading flag definitions still sends the secret key to `host`, so when your proxy must not see the key, build the client with `loadFeatureFlags: false` and don't call `loadFlags()`:

Expand Down Expand Up @@ -84,6 +84,33 @@ $status = PostHog::emails()->get($result->getId()); // queued, sent, delivered,
- **Retries.** Network errors, rate limits and an unavailable service are retried up to `email_request_max_retries` times (default 2) with the same idempotency key, so a retry never sends twice. Each request times out after `email_request_timeout_ms` (default 15000). When a job runner already retries the send, pass `'max_retries' => 0` and an `idempotency_key` derived from what the email is for, such as `welcome:user_123`. A generated key does not survive a job retry.
- **Moving from another provider.** Set the `migrated_from` option, such as `'resend'`, so PostHog knows where your sends came from.

### Batches

`batch()` sends the same email to many recipients, one email each, and returns a result per recipient in the order of `recipients`:

```php
// $digestId is stored with the job, so a retry of the job makes the same keys
$result = PostHog::emails()->batch([
'template_id' => '0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b',
'template_data' => ['team' => 'Analytics'], // shared by every recipient
'recipients' => [
['to' => 'ada@example.com', 'template_data' => ['name' => 'Ada'], 'idempotency_key' => "$digestId:ada"],
['to' => 'alan@example.com', 'template_data' => ['name' => 'Alan'], 'idempotency_key' => "$digestId:alan"],
],
]);
foreach ($result->getResults() as $email) {
if ($email->getStatus() !== 'queued') {
echo $email->getTo(), ': ', $email->getStatus(), PHP_EOL;
}
}
```

- **Recipients.** Each recipient has `to` and optional `distinct_id`, `idempotency_key` and `template_data`. A recipient's `template_data` is merged over the shared `template_data`, and its keys replace the shared ones. With `template_key`, the content is the same for every recipient, so a recipient can't carry `template_data`. A batch takes no `cc`, `bcc`, `attachments` or `scheduled_at`: send those emails with `send()`.
- **Requests.** A batch takes 1 to 100 recipients and goes out in one request. The client waits at least 45 seconds for the answer. More than 100 recipients throws `invalid_recipients` before any request.
- **Idempotency keys.** Every retry reuses each recipient's key, so a retry never queues an email twice. Without a key, the client makes one that covers its own retries but not a second call. When a batch may be cut off and run again, pass keys you store: send it again with the same key within 24 hours of its first use, so it goes out once. Each recipient of a call needs a different key. Build each key from an ID you store with the job, such as the digest's own ID, never from the time of the run: a retry after midnight would make new keys and send the emails again. Never reuse a key for another email: PostHog sends one email per key, so the other doesn't go out. PostHog usually refuses it with `idempotency_key_reused`, but two sends that arrive at the same time can both answer `queued`.
- **Statuses.** `getStatus()` is `queued` when PostHog accepted the email, `failed` when it refused it (`getError()` says why, such as a suppressed address), and `unconfirmed` when the client can't tell, such as after a lost answer or a 409 other than `batch_unavailable`. Send each `unconfirmed` email again with the same key within 24 hours of its first use, so it goes out once. `getWarnings()` holds PostHog's warnings.
- **Errors.** When PostHog refuses the batch as a whole, such as a rate limit past its retries, `batch()` throws `PostHog\EmailBatchError`, whose `getResults()` holds every recipient. `getFix()` carries PostHog's advice. `content_too_large` with status 413 means the request is larger than PostHog takes: send fewer recipients in each batch, or a smaller email. With status 400 it means the content or `template_data` every recipient shares is too large, so fewer recipients won't help: make it smaller, or send those emails one at a time with `send()`. `batch_unavailable` (409) means the project can't send batches yet, and is never retried: send each email the batch didn't queue with `send()` and its recipient's `idempotency_key`. `not_found` (404) without a field means PostHog found no template with that `template_id`, or batch sends aren't available for the project. Check the `template_id`. If it's right, send each recipient on its own with `send()` and its `idempotency_key` until batch sends are available. When an earlier attempt got no answer, a refusal of the retry doesn't prove nothing went out: the error says some emails may have gone out, every recipient stays `unconfirmed`, and `getFix()` says to send each one again with the same `idempotency_key`.

## Questions?

### [Join our Slack community.](https://join.slack.com/t/posthogusers/shared_invite/enQtOTY0MzU5NjAwMDY3LTc2MWQ0OTZlNjhkODk3ZDI3NDVjMDE1YjgxY2I4ZjI4MzJhZmVmNjJkN2NmMGJmMzc2N2U3Yjc3ZjI5NGFlZDQ)
Expand Down
278 changes: 278 additions & 0 deletions api/public-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,145 @@
"schemaVersion": 1,
"namespace": "PostHog\\",
"symbols": {
"PostHog\\BatchEmailResult": {
"type": "class",
"abstract": false,
"final": false,
"readonly": false,
"extends": null,
"implements": [],
"constants": [],
"properties": [],
"methods": {
"__construct": {
"static": false,
"abstract": false,
"final": false,
"returnType": null,
"parameters": [
{
"name": "index",
"type": "int",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "to",
"type": "string",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "idempotencyKey",
"type": "string",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "status",
"type": "string",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "messageId",
"type": "?string",
"byReference": false,
"variadic": false,
"optional": true,
"default": null,
"defaultConstant": null,
"hasDefault": true
},
{
"name": "replayed",
"type": "bool",
"byReference": false,
"variadic": false,
"optional": true,
"default": false,
"defaultConstant": null,
"hasDefault": true
},
{
"name": "error",
"type": "?PostHog\\EmailError",
"byReference": false,
"variadic": false,
"optional": true,
"default": null,
"defaultConstant": null,
"hasDefault": true
}
]
},
"getError": {
"static": false,
"abstract": false,
"final": false,
"returnType": "?PostHog\\EmailError",
"parameters": []
},
"getIdempotencyKey": {
"static": false,
"abstract": false,
"final": false,
"returnType": "string",
"parameters": []
},
"getIndex": {
"static": false,
"abstract": false,
"final": false,
"returnType": "int",
"parameters": []
},
"getMessageId": {
"static": false,
"abstract": false,
"final": false,
"returnType": "?string",
"parameters": []
},
"getStatus": {
"static": false,
"abstract": false,
"final": false,
"returnType": "string",
"parameters": []
},
"getTo": {
"static": false,
"abstract": false,
"final": false,
"returnType": "string",
"parameters": []
},
"isReplayed": {
"static": false,
"abstract": false,
"final": false,
"returnType": "bool",
"parameters": []
}
}
},
"PostHog\\Client": {
"type": "class",
"abstract": false,
Expand Down Expand Up @@ -1695,6 +1834,73 @@
}
}
},
"PostHog\\EmailBatchError": {
"type": "class",
"abstract": false,
"final": false,
"readonly": false,
"extends": "PostHog\\EmailError",
"implements": [
"Stringable",
"Throwable"
],
"constants": [],
"properties": [],
"methods": {
"__construct": {
"static": false,
"abstract": false,
"final": false,
"returnType": null,
"parameters": [
{
"name": "error",
"type": "PostHog\\EmailError",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "results",
"type": "array",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "warnings",
"type": "array",
"byReference": false,
"variadic": false,
"optional": true,
"default": [],
"defaultConstant": null,
"hasDefault": true
}
]
},
"getResults": {
"static": false,
"abstract": false,
"final": false,
"returnType": "array",
"parameters": []
},
"getWarnings": {
"static": false,
"abstract": false,
"final": false,
"returnType": "array",
"parameters": []
}
}
},
"PostHog\\EmailError": {
"type": "class",
"abstract": false,
Expand Down Expand Up @@ -2191,6 +2397,24 @@
}
]
},
"batch": {
"static": false,
"abstract": false,
"final": false,
"returnType": "PostHog\\SendBatchResult",
"parameters": [
{
"name": "batch",
"type": "array",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
}
]
},
"get": {
"static": false,
"abstract": false,
Expand Down Expand Up @@ -5533,6 +5757,60 @@
}
}
},
"PostHog\\SendBatchResult": {
"type": "class",
"abstract": false,
"final": false,
"readonly": false,
"extends": null,
"implements": [],
"constants": [],
"properties": [],
"methods": {
"__construct": {
"static": false,
"abstract": false,
"final": false,
"returnType": null,
"parameters": [
{
"name": "results",
"type": "array",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "warnings",
"type": "array",
"byReference": false,
"variadic": false,
"optional": true,
"default": [],
"defaultConstant": null,
"hasDefault": true
}
]
},
"getResults": {
"static": false,
"abstract": false,
"final": false,
"returnType": "array",
"parameters": []
},
"getWarnings": {
"static": false,
"abstract": false,
"final": false,
"returnType": "array",
"parameters": []
}
}
},
"PostHog\\SendEmailResult": {
"type": "class",
"abstract": false,
Expand Down
Loading