Please see the main PostHog docs.
SDK usage examples and code snippets live in the official documentation so they stay up to date.
- PHP library docs
- Laravel integration docs
- Upgrade notes, including GeoIP configuration and compatibility
The optional PostHog\OpenTelemetry\ContextSpanProcessor adds session and person IDs to application spans inside this client's request context. Register it on the application's existing OpenTelemetry provider. OpenTelemetry remains an optional application dependency.
A disabled client keeps request context but adds no span attributes. That covers a client with no project token, one built with the disabled option, and one turned off with setDisabled(true). A client with a valid token and with the noop consumer still adds them.
The registered processor also enables request span enrichment when withContext() starts. This lookup skips Fibers because uninitialized OpenTelemetry Fiber storage can return another execution path's span. Child spans still use the active Fiber's PostHog context. New Fibers and queue jobs need explicit context propagation.
Existing span attributes take precedence. The processor preserves the application's provider, sampling, and exporters. See the PHP tracing guide and Laravel guide for setup.
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():
$client = new \PostHog\Client('phc_...', [
'host' => 'ph.example.com', // your proxy
'app_host' => 'https://eu.posthog.com',
], loadFeatureFlags: false, secretKey: 'phs_...');
PostHog::init(client: $client);PostHog::init('phc_...', ['host' => 'https://eu.i.posthog.com'], secretKey: 'phs_...');
// A template made in PostHog, by its ID
$result = PostHog::emails()->send([
'to' => 'user@example.com',
'template_id' => '0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b',
'template_data' => ['name' => 'Ada'], // fills {{ variables.name }}
'distinct_id' => 'user_123',
'idempotency_key' => 'welcome:user_123',
]);
// Content your code rendered, named by a key
PostHog::emails()->send([
'to' => 'user@example.com',
'template_key' => 'password-reset',
'subject' => 'Reset your password',
'html' => $html,
'distinct_id' => 'user_123',
]);
// With copies and a file: content is the file's bytes, and the SDK uploads them before the send
PostHog::emails()->send([
'to' => 'user@example.com',
'template_key' => 'receipt',
'subject' => 'Your receipt',
'html' => $html,
'bcc' => 'finance@example.com',
'attachments' => [['filename' => 'receipt.pdf', 'content' => file_get_contents($receiptPath)]],
]);
// Later, such as in a job: for a few seconds after the send, get() throws not_found
$status = PostHog::emails()->get($result->getId()); // queued, sent, delivered, bounced, ...- Two forms. Pass
template_idwith optionaltemplate_data, ortemplate_keywithsubjectandhtml,textor both. The content form also takesfrom(a verified sender),reply_to,cc,bcc,attachments,sensitive,tracking,headersandtags. PostHog derives a missingtextfrom thehtml. To send an email built with a template library such as React Email, render it to HTML in your app and passhtml. The SDK adds no rendering dependency. - Copies and files.
ccandbcctake one address or a list of up to 10.attachmentstakes up to 10 files, each withfilename,content(the file's bytes) and optionalcontent_typeandcontent_id. The whole email can be at most 40 MB after Base64 encoding, which leaves about 29 MB for the files. The 40 MB counts the subject and the bodies, and withouttextit counts thehtmltwice, since PostHog sends the text it derives from it. The SDK checks both limits before any request (too_many_attachments,message_too_large). Pass the bytes, not Base64: when you hold Base64, such as from a Resend payload, passbase64_decode($content). Show a file inline with<img src="cid:logo">and'content_id' => 'logo'. A PostHog version that doesn't take these fields yet refuses the send with an error naming the field, and never drops it. - How files travel. The SDK hashes each file with SHA-256, asks PostHog which files it still needs, uploads those straight to PostHog's file storage with the URL and headers PostHog answers (never with your secret key), then sends the email with each file's hash. A file the project already uploaded isn't uploaded again. The storage host differs from your app host, so allow it if your servers restrict outbound traffic. Files go only to an
httpsstorage URL, unless yourapp_hostis plainhttp://for local development. A storage 429, 500, 502, 503 or 504 is retried like the send. A failed upload throwsattachment_upload_failedwith the storage's answer, and no email is sent. An upload never follows a redirect: storage that answers one fails that file the same way, without naming the URL. - Sensitive emails. Pass
'sensitive' => truewhen the email carries login links, reset tokens or codes. A sensitive email keeps its links unchanged unlesstrackingturns clicks on, leaves its subject off engagement events, and keeps its content out of logs. Left out, the email takes the setting of the template made in PostHog whose key matchestemplate_key, and is sensitive when no template matches. - Tracking. Left out, PostHog counts the email's opens and captures its engagement events, and tracks clicks only when the email isn't sensitive.
'tracking' => trueturns on opens, clicks and engagement events.'tracking' => falseturns them all off: no open pixel, no rewritten links, no engagement events. An array such as['clicks' => true]setsopens(defaulttrue),clicks(default: on unless the email is sensitive) andengagement(defaulttrue) one by one. Turn clicks on for a sensitive email only when its links are safe to rewrite. Click tracking needs open tracking.capture_engagementis deprecated in favor oftrackingwithengagement. It still works, but a send can't set both. - Transactional only. Code-sent emails are transactional.
categoryis deprecated: any non-null value throwsinvalid_formoncategorybefore any request, andnullcounts as left out. Send marketing email through a PostHog workflow. - Headers and tags.
headerstakes header names to string values, such as['X-Entity-Ref-ID' => 'order-1234'], that PostHog puts on the email itself:In-Reply-To,References,List-Unsubscribe,List-Unsubscribe-Post,List-IdandX-headers, at most 8.tagstakes up to 10 names to values, such as['plan' => 'pro'], that come back as$email_tagson the email's events. The SDK checks only that each is an array of names to values, and PostHog refuses the rest withinvalid_headerorinvalid_tag. Neither works withtemplate_idor from the PostHog test sender (sandbox_fields_unsupported). - Pass
distinct_id. Without it, the email's engagement events go to a person whose distinct ID is the email address. - Scheduling. Pass
scheduled_at, aDateTimeInterfaceor an ISO 8601 string with an offset such as2026-12-01T09:00:00Z, to send the email up to 7 days later. A string without an offset throwsinvalid_scheduled_atbefore any request. The SDK rounds a time finer than a millisecond up to the next millisecond, so the email never goes out early. Only PostHog Cloud schedules emails: with any otherapp_host, such as a self-hosted PostHog, a reverse proxy or a custom domain, a send withscheduled_atthrowsscheduling_requires_cloudbefore any request. The result'sgetStatus()isscheduledwith the confirmedgetScheduledAt(), orqueuedwhen the time had already passed and the email goes out now. When PostHog accepts the email without confirming the time, the send throwsschedule_not_confirmed, and so does aqueuedanswer for a time more than a minute ahead of your server's clock when the send's first attempt went out, even when a retry gets the answer (the minute allows for clock difference); read the email's status withgetMessageId()before you send it again.get()readsscheduleduntil the email goes out. Compute a relative time once and reuse it on your own retries: the idempotency key covers the time. To stop scheduled emails, disable the email's workflow in PostHog until they have reached a final status. - A result means accepted, not delivered.
send()returns aSendEmailResultwith the message ID, andgetStatus()says whether PostHog queued or scheduled the email.get()reads the status for 30 days after the send. For a few seconds after the send, it throwsnot_found. - Errors. A send that is not queued or scheduled throws
PostHog\EmailError, and so does a scheduled send whose time PostHog didn't confirm (schedule_not_confirmed, withgetMessageId()). Branch ongetErrorCode(), such asrecipient_suppressedormissing_app_host, or ongetErrorName(), such asvalidation_error. An invalid argument throws before any request, with aninvalid_*code. - Retries. Network errors, rate limits and an unavailable service are retried up to
email_request_max_retriestimes (default 2) with the same idempotency key, so a retry never sends twice. Each request times out afteremail_request_timeout_ms(default 15000). When a job runner already retries the send, pass'max_retries' => 0and anidempotency_keyderived from what the email is for, such aswelcome:user_123. A generated key does not survive a job retry. - Moving from another provider. Set the
migrated_fromoption, such as'resend', so PostHog knows where your sends came from.
batch() sends the same email to many recipients, one email each, and returns a result per recipient in the order of recipients:
// $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
toand optionaldistinct_id,idempotency_keyandtemplate_data. A recipient'stemplate_datais merged over the sharedtemplate_data, and its keys replace the shared ones. Withtemplate_key, the content is the same for every recipient, so a recipient can't carrytemplate_data. A batch takes nocc,bcc,attachmentsorscheduled_at: send those emails withsend(). - 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_recipientsbefore 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 answerqueued. - Statuses.
getStatus()isqueuedwhen PostHog accepted the email,failedwhen it refused it (getError()says why, such as a suppressed address), andunconfirmedwhen the client can't tell, such as after a lost answer or a 409 other thanbatch_unavailable. Send eachunconfirmedemail 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()throwsPostHog\EmailBatchError, whosegetResults()holds every recipient.getFix()carries PostHog's advice.content_too_largewith status 413 means the request is larger than PostHog takes: split the recipients into smaller batches, or send a smaller email. A 413 without a code, such as a proxy's, reads the same. With status 400 it means the content ortemplate_dataevery recipient shares is too large, so fewer recipients won't help: make it smaller, or send those emails one at a time withsend().batch_unavailable(409) means the project can't send batches yet, and is never retried: send each email the batch didn't queue withsend()and its recipient'sidempotency_key.not_found(404) without a field means PostHog found no template with thattemplate_id, or batch sends aren't available for the project. Check thetemplate_id. If it's right, send each recipient on its own withsend()and itsidempotency_keyuntil 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 staysunconfirmed, andgetFix()says to send each one again with the sameidempotency_key.
See CONTRIBUTING.md for local setup and test instructions.
See RELEASING.md.