Send transactional emails with Ruby
The Ruby SDK sends transactional emails from version 3.30.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 this to your Gemfile, then run bundle install:
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.
When secret_key is set, the client also loads flag definitions with it for local evaluation when it starts, and polls them after that. Give the key the Feature flag read scope too, or each of those loads fails.
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.
Use in Rails
With posthog-rails, reuse the client initialized by PostHog.init. Add the secret key to your existing initializer:
Use PostHog.emails wherever the examples below use $posthog.emails. Rails handles client shutdown.
Pass email options such as app_host and email_request_max_retries as a hash to PostHog.init(...), not in its configuration block.
Send an email
send returns a SendResult, and result.id is the message ID. A result means PostHog queued the email, not that it delivered it. When PostHog doesn't queue the email, send raises PostHog::EmailError, and its code is one of the codes in the errors table. A wrong argument raises ArgumentError before any request.
For a longer email, render the HTML first with your template engine, such as ERB or Action Mailer views, and pass the result as html.
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
The client sends email with Ruby's Net::HTTP, so WebMock can stub the request and show 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.
test_mode: true doesn't stop email requests or flag definition loads, so stub both.
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





