Repository navigation
Conversation
…duled_at Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… trait Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…->batch() emails()->batch() takes a template or code-form email and a list of recipients, sends them in requests of at most 100 recipients and 4 MiB, one after another, and returns a result per recipient in input order: queued, failed, unconfirmed or not_attempted. Each recipient's idempotency key is given or generated and reused on every retry. A request that fails as a whole throws EmailBatchError with every recipient's result, after reading lost emails by their key. The reply parsing moves to Internal\EmailReplies and the recipient checks to Internal\EmailRecipient, so send() and batch() share them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
posthog-php-fork_curl Compliance ReportDate: 2026-11-25T12:30:48.713896+00:00 ✅ All Tests Passed!47/47 tests passed Capture Tests✅ 30/30 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
posthog-php-socket Compliance ReportDate: 2026-11-25T12:31:01.730525+00:00 ✅ All Tests Passed!47/47 tests passed Capture Tests✅ 30/30 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
posthog-php-lib_curl Compliance ReportDate: 2026-11-25T12:30:58.416388+00:00 ✅ All Tests Passed!47/47 tests passed Capture Tests✅ 30/30 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
… one body at a time A status read by key now fills in only the message ID: the recipient stays unconfirmed, since an email under the key may be a refusal or an older send. The client reads every unconfirmed email without a message ID at the end of each batch, on success too. A whole-request idempotency_key_in_flight leaves the request's recipients unconfirmed, and a later attempt's answer becomes their later error. The first malformed answer no longer sets a later error. The batch keeps each recipient's JSON and the requests' membership, and builds one request body at a time, reused for its retries. The results' own unconfirmed status replaces the separate maybe-queued map. Shared content over 4 MiB throws content_too_large naming html, text or template_data before any request, and a recipient too large alone names its template_data. A single send checks its form and content again before its idempotency key, as before the batch. The unused Emails::epochSecondsAsFloat is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sing a key The README's batch example names one email of one run with each key, says never to reuse a key for another email, and says that unconfirmed emails come back with their message ID when PostHog has them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…errors on every retry A read by key at the end of a batch is best effort: any Throwable from it, such as an ErrorException from a promoted deprecation or an Error from a missing class, now stops the reads, and batch() still returns its result or throws its EmailBatchError with every recipient. An attempt that can't confirm a request, a 503, a lost answer or an answer without a result per recipient, now sets the later error of each recipient an earlier attempt already left unconfirmed. The first such attempt still leaves it null. Shared content over 4 MiB names its largest field, such as from or reply_to, not only html, text or subject. The memory test runs a batch of 10,000 recipients in a child process under a 26 MiB limit, and the shutdown test no longer asserts the fixture's own flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…y best effort The README and the batch() docblock build each idempotency key from an ID stored with the job, such as the digest's own ID, so a retry after midnight makes the same keys. They say the end-of-batch reads by key stop at the first failure other than a 404. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The batch now encodes each shared field once and builds the request body's opening from those pieces, so content_too_large names the largest field from the same encoding. A JsonSerializable value is never serialized a second time, so one that answers differently, or throws, on a second call can no longer turn content_too_large into invalid_form or another exception. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ory test to what PostHog takes A retry that loses its answer at the transport keeps the recipient unconfirmed with its first error, and records network_error as its later error. The memory test shares 150,000 bytes of template_data, which PostHog accepts for 100 recipients, under a 24 MiB limit that the batch before it built one body at a time still exhausts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…feat/email-batch # Conflicts: # test/EmailsTest.php
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…feat/email-batch # Conflicts: # lib/Emails.php
More than 100 recipients is refused before any request. A refusal of the whole batch, such as a 413, is the error, with every recipient's result. The chunk loop, not_attempted, getLaterError() and the client-side 4 MiB sizing are gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A recipient a retry refuses after a lost answer stays unconfirmed with the same-key resend advice, since the first attempt may have queued it. - A 413 keeps PostHog's own fix. Without one, the advice covers a smaller email as well as fewer recipients. A 400 content_too_large gets no split advice. - The request body is encoded once and grown in place, so a batch near PostHog's body limit no longer runs out of PHP memory first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Each recipient is encoded straight into the request body instead of being held encoded until the body is built, so a batch near PostHog's body limit fits a tighter PHP memory limit. - Every unconfirmed recipient carries the same-key resend advice, also when PostHog's first answer leaves it unconfirmed or unreadable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The request is encoded in one json_encode call, so a batch holds only the caller's input and the body. Encoding errors are traced to the shared fields or a recipient only when that call fails. - A whole-request refusal after an attempt that may have queued leaves each recipient unconfirmed with the refusal and the resend advice. - An empty fix from PostHog on a 413 falls back to the SDK's advice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- When encoding the batch fails, the client traces the failing field and then throws, instead of encoding again: a value that encodes only on a later call no longer goes out. - A warning PostHog repeats in one answer reaches the caller once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Tracing which field failed to encode can't let a serializer's later exception replace the invalid_form error; nothing is sent either way. - Test lines wrapped to the 120-character limit CI checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…again A batch whose data fails to encode now fails with invalid_form at once, without running the caller's serializers a second time. The oversized answer is tested against the loopback server, and the near-limit memory test only uses recipients PostHog can queue. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ight fix Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
PostHog Review · Standard · reviewed 7cf8fb2: 2 Should fix. This pull request is large, so the review ran in 4 parts with less depth than usual.
New pushes to this pull request won't start an automatic review. Comment @posthog review to review new changes.
See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.
A batch now keeps every recipient's result and key when anything in the retry machinery throws, and the retry wait falls back to a monotonic clock on symfony/clock 6.2, which has no Clock class. A 409 without a readable code leaves recipients unconfirmed. Warnings are deduplicated with a keyed lookup, and the resend advice counts the 24 hours from the key's first use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A 503 or in-flight 409 now marks the batch as possibly queued before it reads the answer, so a Retry-After that throws while parsing leaves the recipients unconfirmed with their keys. The retry wait falls back to usleep when symfony/clock 6.2.0 can't build a MonotonicClock either. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Warning dedupe keys on nested arrays instead of serialize(), so an app that disables serialize() keeps PostHog's queued results. The fallback retry wait reads hrtime() as seconds and nanoseconds, so a wait longer than two seconds doesn't overflow on 32-bit PHP. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A project without batch sends isn't a request still in progress, so its error name matches Python's validation_error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Anything the client throws while it sends a batch, such as a disabled curl function after the request went out, now leaves every recipient unconfirmed with its key instead of failed. A recipient's template_data gets the same JSON depth as a single send, and the retry wait calls hrtime() only when no clock is available. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A client failure after a recorded in-flight or lost answer no longer replaces each recipient's error with a generic one, so the caller keeps PostHog's code and fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
marandaneto
left a comment
There was a problem hiding this comment.
The code looks good; no qualifying findings. Verdict: correct.
💡 Motivation and Context
A PHP app that sends the same transactional email to many people, such as a digest or an announcement, has to call
emails()->send()once per recipient. PostHog Cloud now takes a batch of up to 100 recipients in one request. This PR addsemails()->batch(), one request per call,, ported from the Python SDK (PostHog/posthog-python#1291), the reference, with the Node SDK (PostHog/posthog-js#5847) as a second source.Stacked on #330 (scheduled sends).
Impact
The server counts batch sends on its own send events, so the SDK adds no event. This PR adds the PHP path to them.
Caution
🔴 Merge risk: high · one-way door once the release publishes:
batch(),SendBatchResult,BatchEmailResultandEmailBatchErrorbecome public APIbatch()is a new entry point. Only apps that call it reach the batch code, on PostHog Cloud US and EU, or a self-hosted app host that serves the endpoint.send()andget()are unchanged. Their recipient checks and reply parsing moved into shared internal classes, and the base's email tests pass against this head (shadow diff).queuedwithout PostHog's answer for it, so the worst case isunconfirmedfor an email that went out. The live batch at this head covers a template batch and a code-rendered batch, their same-key replays, and three refusals, not the retry paths.EmailBatchErrorwith every recipientfailed, and nothing went out. The live run covers a 404, a 413 and a 400.Retry-Afteron PHP 8.5 in an app that throws on deprecations, or the retry wait itself, comes back asEmailBatchErrorwith every recipientunconfirmedand keeping its key, and an earlier in-flight answer keeps PostHog's code and fix (tests). If the client failed before the request went out, those recipients areunconfirmedfor emails that weren't sent, and a same-key resend sends them once. On symfony/clock 6.2 the retry wait falls back to a monotonic clock, and tohrtimewithusleepwhen that clock can't load either, on 32-bit PHP too. The retry wait is shared, so this also fixes the retries ofsend(),get()and attachment uploads, and their waits on a working clock are unchanged (shadow diff).invalid_formwith nothing sent.getErrorName()of adelivery_unavailableerror without an HTTP status is nowservice_unavailable, notapplication_error. Batch results carry one, and so does anEmailErroran app builds itself with that code and no status.send()gets that code with a status, so its errors are unchanged (tests).resultsis an object keyed by numbers reads as a list, so a recipient can readqueuedthat PostHog never confirmed, and the app skips its resend (Known issues, base reply check). PostHog's endpoint answers with a list.workflows-email-batch-sendsflag answers 404 while it is off, andbatch()reportsnot_foundwithout falling back to single sends.Rollout
unconfirmedresults in an app that batches. The SDK has no alert for them.Changes
emails()->batch()takesrecipients(eachto, and optionaldistinct_id,idempotency_keyandtemplate_data) with one form for the whole batch:template_idwith sharedtemplate_datathat each recipient'stemplate_datamerges over;email_request_timeout_mswhen that is longer. More than 100 throwsinvalid_recipientsbefore any request. The SDK checks no byte size: PostHog's refusal is the error, with PostHog's own fix. A 413content_too_largemeans the request is too large: send fewer recipients. A 400content_too_largemeans the shared content is too large, so fewer recipients won't help. The SDK encodes the request in one call, so it holds only the caller's input and the body, and a batch near PostHog's limit doesn't run out of PHP memory first. A recipient'stemplate_datacan nest as deep as it can insend(). When encoding fails, the SDK throwsinvalid_formwith PHP's reason, assend()does, and sends nothing.invalid_idempotency_keybefore any request.SendBatchResult::getResults()returns oneBatchEmailResultper recipient, in order, with statusqueued,failedorunconfirmed, the message ID,isReplayed()and the error.send()retries, such as a rate limit or a network error, resends the whole request with the same keys.unconfirmedrecipient keeps its idempotency key, and its error says to send it again with the same key within 24 hours of its first use. The client reads nothing back after a batch, so a message ID comes only from PostHog's answer to the batch request.idempotency_key_in_flightleaves its recipientsunconfirmedwith that answer as their error, as in Python. So does a 409 whose body has no readable code. When PostHog gives no fix, the error says to send again with the sameidempotency_keywithin 24 hours of its first use. The same code on any other status doesn't claim the key is in flight.EmailBatchErrorwith every recipient:failed, orunconfirmedwhen an earlier attempt of the call lost its answer and so may have queued them. It uses Python's codes:batch_unavailable(409, namedvalidation_error),not_found(404) andclient_closed. After an attempt that lost its answer, a recipient or a whole request that a retry refuses staysunconfirmedwith the refusal as its error and the same-key resend advice, since the first attempt may have queued it. Everyunconfirmedrecipient carries that advice. A warning PostHog repeats in one answer reaches the caller once.cc,bcc,attachments,scheduled_at, and a top-levelto,distinct_idoridempotency_key, before any request.batch()docblock, the public API snapshot and a minor changeset.💚 How did you test it?
Test-first in
test/EmailsBatchTest.php, throughemails()->batch()with the fake PostHog transport. The cases cover both forms, the shared and per-recipienttemplate_datamerge, 100 recipients in one request and 101 refused before any, generated and explicit keys, duplicate keys, results in order, each status, replays, retries with the same body, unconfirmed recipients that keep their keys with nothing read back, an in-flight answer on a 409 and on a 503, a request that fails as a whole, the 404, 409, 413 and 400content_too_largeanswers, a recipient a retry refuses after a lost answer, a client closed before a retry and the refused fields. Two memory-limited child processes cover a batch near PostHog's body limit and shared data over it. A mutation run broke each guarded line, and each break failed a test.Locally, through PHP 8.5 in Docker: the full PHPUnit suite,
phpcson the changed files andcomposer api:checkpass.git archivetest included.failedand nothing sent. An unknown template got 404. Shared data over the request limit got 413content_too_largewith PostHog's fix to split the recipients. Shared data over the shared-content limit got 400content_too_largewith no split advice. Then one real batch of two recipients through a test template, and the same batch with the same keys. Both were queued in one request, in order, the replay returned the same message IDs, and the status reads and the mailbox show one email per recipient.Known issues
template_dataonce per recipient against its own request limits, so a batch over them is refused as a whole withcontent_too_largebefore any email goes out, and the error carries PostHog's fix. The client doesn't split.ErrorExceptionwhen PostHog answers with an HTTP-dateRetry-After. This is base behavior: the parser uses the deprecatedDATE_RFC7231constant, moved here unchanged.batch()now returns every recipient's result in that case;send()still throws theErrorException.emails()->send()does, so a value that grows a lot as JSON, such as one full of control characters, can still exhaust a tight PHP memory limit before PostHog'scontent_too_large.unconfirmederror. The thrownEmailBatchErrorcarries the last answer's detail, field and retry timing.resultsis an object keyed by numbers reads as a list.jsonSerialize()intemplate_datathat throws something other thanJsonExceptionescapesbatch()as that exception, notinvalid_form. Nothing is sent.send()behaves the same.idempotency_key_reused, the message ID names the email the key first sent, soget()reports that email's status, not the refused one. The docblock is feat(emails): schedule a transactional email with scheduled_at #330's, unchanged here.📝 Checklist
If releasing new changes
pnpm changeto generate a change intent file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Written by Claude Opus 5.5 (
claude-opus-5-5) in Claude Code, as an implementation lane under a conductor thread that Michael directs. Python's batch is the reference. PHP has no async client, so there is no asyncbatch(), and bad arguments throwEmailErrorwithinvalid_*codes, assend()already does, instead of Python'sTypeErrorandValueError.unconfirmedwith the retry's error and the same-key resend advice, as in Python and Elixir. Replaced:failed.unconfirmed. Rejected: failing the request.Involvement: no grilling rounds; tests mutation-checked; one five-lens review panel (Claude fresh eyes, GPT correctness, risk, design and anti-overengineering) and fixes reads.
🤖 Generated with Claude Code
How did you test this code?
Merge card: high, one-way door · 6 rounds, 21 final reads · gaps: judge (🤖 agent d-62ddebf4) · 7 known issues · 1700 min opened to ready
436bc10(reused from7cf8fb2)436bc1039c088eto436bc10436bc10Not checked: none recorded
Full receipt: https://artifacts.posthog.com/a/Z5lDIHD8vsfRWM40K2spbu