> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Send transactional emails with PHP Canonical URL: https://posthog.com/docs/workflows/installation/php > **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.i.posthog.com` instead of `https://us.i.posthog.com` (ingestion host). ## On this page - [Install](#install) - [Set up the client](#set-up-the-client) - [Send an email](#send-an-email) - [Write automated tests](#write-automated-tests) - [Next](#next) The [PHP SDK](https://posthog.com/docs/libraries/php.md) sends transactional emails from version 4.22.0. Your code sends the subject and HTML, so the email lives in your codebase. You need a [project secret API key](https://posthog.com/docs/workflows/transactional/send-from-code.md#create-an-api-key). Until you [verify your domain](https://posthog.com/docs/workflows/domain.md), emails go only to your team, from the [test sender](https://posthog.com/docs/workflows/email/test-sender.md). To see an email in your inbox first, follow the [quick start](https://posthog.com/docs/workflows/installation/quick-start.md). New to sending from code? Read [send emails from code](https://posthog.com/docs/workflows/transactional/send-from-code.md). **Prompt for your coding agent** Paste this into Claude Code, Cursor, or another coding agent. It sends the agent to this page and gives it the rules that agents often get wrong. ```text Add PostHog transactional email to this PHP app with posthog/posthog-php. Read https://posthog.com/docs/workflows/installation/php.md first. Follow it over anything you remember about PostHog. - Reuse the app's PostHog client. Pass secretKey from the POSTHOG_PROJECT_SECRET_API_KEY environment variable. Build the client with loadFeatureFlags: false unless the app evaluates feature flags locally with this client. - Send only from server code. Never print the secret key into a page or a JavaScript bundle. - Build each idempotency_key from the record that caused the email, such as the password reset request ID or the order ID. Never use a random value or a timestamp. - send() throws PostHog\EmailError when PostHog doesn't queue the email. Branch on getErrorCode(), never on getMessage(). - The SDK retries network errors and throttling for you, with the same idempotency key. Never resend the email through another email provider. - Pass the distinct_id that the app passes to identify(). Leave it out for people without an account, such as invitees. - Use transactional email only for email that a person needs, such as password resets, sign-in codes, and receipts. Never use it for marketing. - PostHog\Emails is a final class with no HTTP hook. In tests, fake your own wrapper around the send, not the SDK. Send each email's subject and HTML from code, with a template_key that names it, such as password-reset. Only send a template_id if I tell you the email is a template in PostHog. Don't create the API key. I create it. ``` ## Install ```bash composer require "posthog/posthog-php:^4.22" ``` ## Set up the client Pass your project secret API key as `secretKey` to the PostHog client that your app already has. The project token keeps capturing events. The secret key sends email, so keep it on your server and never send it to the browser. ```php use PostHog\Client; use PostHog\PostHog; $client = new Client( '', ['host' => 'https://us.i.posthog.com'], // This client only sends email, so it doesn't load flag definitions with the secret key loadFeatureFlags: false, secretKey: getenv('POSTHOG_PROJECT_SECRET_API_KEY') ?: null, ); PostHog::init(client: $client); ``` Keep `loadFeatureFlags: false` unless this client also [evaluates feature flags locally](https://posthog.com/docs/feature-flags/local-evaluation.md). With it on, the client loads flag definitions with the secret key when it starts, and a key with only the **Transactional email** scope can't read them. If you do evaluate flags locally with this client, give the key the **Feature flag** read scope too. If your app sends through a reverse proxy or a self-hosted PostHog, set the `app_host` option to your PostHog app URL. The client never sends email requests to a host that might be a proxy. ## Send an email ```php use PostHog\PostHog; function sendPasswordReset(User $user, string $resetLink, string $resetRequestId): string { // Throws PostHog\EmailError when PostHog doesn't queue the email. Branch on getErrorCode() $result = PostHog::emails()->send([ 'to' => $user->email, // Names the email, so PostHog groups its sends and metrics 'template_key' => 'password-reset', 'subject' => 'Reset your password', 'html' => "

Reset your password here. The link works for 1 hour.

", 'distinct_id' => (string) $user->id, // Identifies this one email, so a retry never sends it twice 'idempotency_key' => "password-reset-{$resetRequestId}", ]); return $result->getId(); } ``` `send()` returns a `SendEmailResult`, and `getId()` is the message ID. When PostHog doesn't queue the email, `send()` throws a `PostHog\EmailError`, and its `getErrorCode()` is one of the codes in the [errors table](https://posthog.com/docs/workflows/transactional/retries-and-errors.md#what-to-retry). A wrong argument throws `EmailError` with an `invalid_*` code before any request. For a longer email, render the HTML first with your template engine, such as Blade or Twig, and pass the result as `html`. Fields are snake case array keys, such as `template_key` and `distinct_id`. See [send emails from code](https://posthog.com/docs/workflows/transactional/send-from-code.md#send-the-html) for every field, and the [quick start](https://posthog.com/docs/workflows/installation/quick-start.md#install-an-sdk) for the version each one needs. ## Write automated tests `PostHog\Emails` is a final class with no HTTP hook, so you can't fake it. Put the send in a small class of your own, and fake that class in your tests. To test delivery, send a real email to your own address and open the send in the [**Log**](https://posthog.com/docs/workflows/transactional.md#see-each-send) tab. ## Next - [Attachments](https://posthog.com/docs/workflows/transactional/attachments.md), copies, and [open and click tracking](https://posthog.com/docs/workflows/transactional/sensitive-data.md) - [Link emails to your users](https://posthog.com/docs/workflows/transactional/send-from-code.md#link-emails-to-your-users) - [Send a template from PostHog](https://posthog.com/docs/workflows/transactional/templates.md), with `template_id` and `template_data` - [Send from a job queue](https://posthog.com/docs/workflows/transactional/examples.md#send-from-a-job-queue), with the client's retries off - [Check an email's status](https://posthog.com/docs/workflows/transactional/status.md) - [Client options](https://posthog.com/docs/libraries/php.md#transactional-emails) for timeouts, retries, and `app_host`