Send transactional emails with Python
The Python SDK sends transactional emails from version 7.84.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
For async code, install posthog[async] instead.
Set up the client
Add your project secret API key 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 ship it to the browser.
The client loads flag definitions only when it evaluates a flag. If your app evaluates feature flags locally with the same client, give the key the Feature flag read scope too.
In a process that only sends email, you can use Emails or AsyncEmails from posthog.emails instead of a full client.
Shut the client down when your app exits, not after each send. 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
With AsyncPosthog, await the call. send returns a SendEmailResult, and result.id is the message ID. That means PostHog queued the email, not that it delivered it. When PostHog doesn't queue the email, send raises EmailError. Branch on its code, and see what to retry. A wrong argument raises TypeError or ValueError before any request.
For a longer email, render the HTML with your template engine, such as Jinja or Django templates, and pass it as html. Fields use snake case, and from_ takes the place of from. See send emails from code for every field.
Write automated tests
Pass your own HTTP transport to see exactly what your code sends, with no network call. This tests your integration, not delivery. To test delivery, send a real email to your own address and check the Log on the Transactional tab.
For sync code, pass a requests.Session with a test adapter to Emails(session=...). Any library that mocks requests or httpx works too.
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 with
posthog.emails.get - SDK reference for every method and option





