Send transactional emails with Ruby
Contents
The Ruby SDK sends transactional emails from version 3.30.0. You need a transactional email and a project secret API key first. The quick start sets up both and sends a test email in a few minutes.
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 are easy to get wrong.
Install
Add this to your Gemfile:
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. Give the key the Feature flag read scope too, or the client logs a failed load each time it polls.
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, see use a proxy or self-hosted PostHog.
Send an email
send returns a SendResult, and result.id is the message ID. 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.
A network_error or a 503 doesn't mean that the email failed, because PostHog can still have queued it. Retry with the same idempotency key. If you resend the email through another email provider, the recipient can get it twice.
Timeouts and retries
The client retries for you. It retries network errors, rate limits, an in-flight idempotency key, and a 503 up to two times, with the same idempotency key. It waits about 1 second and then 2 seconds, or longer if the server's Retry-After asks for it. When Retry-After asks for more than 30 seconds, the client raises the error at once. Each request waits up to 15 seconds to connect, and up to 15 seconds for each read or write.
To change this, set email_request_timeout_seconds and email_request_max_retries on the client, or max_retries on one send.
Send from a background job
Send from a job queue when the email shouldn't slow down the request, such as an email address check after a signup. Let the queue own the retries:
- Pass
max_retries: 0tosend, so that the client doesn't retry inside the job. - Keep the idempotency key the same on every attempt.
- Finish the retries within 24 hours. PostHog remembers an idempotency key for 24 hours after it queues the email, so a later retry can send the email twice.
- Retry only the errors that a later attempt can fix. Every other code fails the same way again.
This example uses Sidekiq 6.5.2 or later. sidekiq_retry_in returns nil to retry with Sidekiq's usual backoff, or :kill to move the job to the Dead set with no more retries.
Link emails to your users
Pass the distinct ID that you pass to identify() as distinct_id. This is usually your user's ID. PostHog then records the email's engagement events, such as sent, opened, and clicked, on that user's person, in the same timeline as their product events.
- No distinct ID - PostHog records the email on a person whose distinct ID is the email address. That's a different person from your user, unless your app identifies users by their email address.
- Not a user yet - For an invite, or an email address check before the account exists, leave
distinct_idout.
Send content from your code
send also takes content that your code rendered, with no template in PostHog. Use it when your emails already live in your codebase, or when you move from another email provider. Name the email with template_key, and pass subject with html, text, or both. PostHog sends the content as you wrote it, so render it first with your template engine, such as ERB or Action Mailer views.
template_key- Names the email, such aspassword-reset. PostHog groups the email's metrics and sends under this key, so use the same key every time you send this email.text- If you leave it out, PostHog makes it fromhtml.from- Needs an address on a domain that your project verified. Without it, PostHog uses the project's default sender.reply_totakes one address or a list.capture_engagement- PostHog captures the email's engagement events unless you passcapture_engagement: false.
A send has template_id or template_key, not both, or it raises ArgumentError. Content from your code takes no template_data, because PostHog renders nothing. See content from your code for each field and its checks.
Check an email's status
get returns a Status with id, status, created_at, status_at, and reason. See statuses and reasons for what each value means. The helper returns nil while the status isn't there yet, so check for nil before you read the result as "not bounced".
A not_found right after a send usually means that the first status hasn't arrived yet. It doesn't mean that the email failed. The client doesn't retry a not_found, so try again a few times with a short wait before you treat the email as missing.
Test without sending email
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 email's Sends tab.
The client also loads flag definitions when it starts, so stub that request too.
Use a proxy or self-hosted PostHog
On PostHog Cloud, the client sends email to your region's app host, even when host is an ingestion host such as us.i.posthog.com. If host is a reverse proxy or a self-hosted PostHog, set app_host to your PostHog app URL. The client doesn't send email requests to a host that might be a proxy.
Flag definitions for local evaluation still load from host with the secret key. If that host is a proxy that must not see the key, leave secret_key off that client. Send email from a second client whose host is your PostHog host.





