Send transactional emails with PHP
The PHP SDK 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. Until you verify your domain, emails go only to your team, from the test sender. To see an email in your inbox first, follow the quick start. New to sending from code? Read send emails from code.
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.
Install
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.
Keep loadFeatureFlags: false unless this client also evaluates feature flags locally. 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
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. 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 for every field, and the quick start 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 tab.
Next
- Attachments, copies, and open and click tracking
- Link emails to your users
- Send a template from PostHog, with
template_idandtemplate_data - Send from a job queue, with the client's retries off
- Check an email's status
- Client options for timeouts, retries, and
app_host





