Send transactional emails with Elixir
The Elixir SDK sends transactional emails from version 2.20.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
Add posthog to your dependencies in mix.exs:
Set up the client
Add your project secret API key to the PostHog config that your app already has. The project token keeps capturing events. The secret key sends email, so read it from the environment and keep it on your server.
Keep enable_local_evaluation: false unless your app evaluates feature flags locally. With local evaluation on, the SDK polls flag definitions with the secret key, and a key with only the Transactional email scope can't read them. If you do evaluate flags locally, give the key the Feature flag read scope too.
If your app sends through a reverse proxy or a self-hosted PostHog, set app_host to your PostHog app URL. The client never sends email requests to a host that might be a proxy.
Send an email
For a longer email, render the HTML first with EEx, Phoenix templates, or another template engine, and pass the result as html.
send/2 doesn't raise. It returns {:ok, %PostHog.Emails.SendResult{}} or {:error, %PostHog.Emails.Error{}}. result.id is the message ID. The error's code is one of the codes in the errors table. An option that the SDK refuses returns an invalid_* code before any request.
To send through a named PostHog instance instead of the default one, pass its name first: PostHog.Emails.send(MyPostHog, options).
Fields use snake case, 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
Every request goes through the config's api_client_module. Wrap the default client with a Req.Test plug to see exactly what your code sends, with no network call. Req.Test needs the plug package, which a Phoenix app already has. This tests your integration, not delivery. To test delivery, send a real email to your own address and open the send in the Log tab.
test_mode: true keeps captured events in memory, but it doesn't stop email requests. Use the plug for those.
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





