Repository navigation
feat(emails): send transactional email and read its status #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
156067d
feat(emails): send transactional email and read its status
Silthus 16a2c62
fix(emails): forget the parent's requests in a forked child
Silthus e33feda
fix(emails): retry a send whose response body was cut off
Silthus 90c1afb
fix(emails): answer the round 3 review
Silthus e94874c
fix(emails): retry a refusal whose body broke off
Silthus c843d53
test(emails): prove shutdown waits for a send in its retry backoff
Silthus 21c81ad
test(emails): release held retries in an after hook
Silthus a55debf
fix(emails): name the transactional_email scopes for sends and status…
Silthus 5cd43aa
Merge branch 'main' into feat/emails-send
Silthus e1ee0d4
docs(emails): rewrite the release note for transactional email
Silthus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --- | ||
| "posthog-ruby": minor | ||
| --- | ||
|
|
||
| The new `client.emails` sends transactional email from your Ruby code and reads each email's delivery status. | ||
|
|
||
| ```ruby | ||
| posthog = PostHog::Client.new(api_key: 'phc_...', secret_key: 'phs_...') | ||
|
|
||
| result = posthog.emails.send( | ||
| to: 'user@example.com', | ||
| distinct_id: 'user_123', | ||
| template_key: 'password-reset', | ||
| subject: 'Reset your password', | ||
| html: rendered_html | ||
| ) | ||
|
|
||
| # 'delivered', 'bounced', ... | ||
| status = posthog.emails.get(result.id).status | ||
| ``` | ||
|
|
||
| You can also send a template made in PostHog by its `template_id:`. | ||
|
|
||
| You need a project secret API key, the kind that starts with `phs_`. Create one under **Project secret API keys** in your project settings. Set its **Transactional email** scope to Write to send emails, or to Read for a key that only reads statuses. Pass it to the client as `secret_key`. Set `app_host` to your PostHog app URL when you self-host PostHog or send through a reverse proxy. | ||
|
|
||
| PostHog records each email's delivery, opens and clicks on the person with that `distinct_id`. It groups every email with the same `template_key`, so you see their metrics and logs in one place. The SDK retries a request that hits a network error, a rate limit or an outage twice by default. Every send carries an idempotency key, so a retry never sends the email twice. Pass your own `idempotency_key`, such as `welcome:user_123`, and your job runner's retries are safe too. | ||
|
|
||
| A send with your own content also takes `from:`, `reply_to:` and `capture_engagement: false`. Set `migrated_from` on the client to tell PostHog which email provider you moved from. A failed call raises a `PostHog::EmailError` with PostHog's `code` and the HTTP `status_code`. | ||
|
|
||
| Docs: [Send transactional emails with Ruby](https://posthog.com/docs/workflows/transactional-emails/ruby) and the [transactional email overview](https://posthog.com/docs/workflows/transactional-emails). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module PostHog | ||
| # PostHog did not queue the email, or the client could not tell whether it did. | ||
| # | ||
| # Branch on {#code}, PostHog's stable error code, or on {#name}, a coarse category of the failure. | ||
| # The message never contains the secret key or the request body. | ||
| class EmailError < StandardError | ||
| # @return [String] A coarse category of the failure, such as `validation_error`, `rate_limit_exceeded` | ||
| # or `service_unavailable`. | ||
| attr_reader :name | ||
| # @return [String] PostHog's error code, such as `quota_limited`, or an SDK code such as `network_error`. | ||
| attr_reader :code | ||
| # @return [Integer, nil] The HTTP status, when PostHog answered. | ||
| attr_reader :status_code | ||
| # @return [String, nil] Why PostHog refused the email. | ||
| attr_reader :why | ||
| # @return [String, nil] How to fix the request. | ||
| attr_reader :fix | ||
| # @return [String, nil] A link to the docs for this error. | ||
| attr_reader :docs_url | ||
| # @return [String, nil] The request field the error is about, when PostHog named one. | ||
| attr_reader :field | ||
| # @return [Float, nil] How long PostHog asked the client to wait before it sends again, in seconds. | ||
| attr_reader :retry_after | ||
|
|
||
| # @api private | ||
| def initialize(code:, message:, status_code: nil, why: nil, fix: nil, docs_url: nil, field: nil, retry_after: nil) | ||
| super(message) | ||
| @name = self.class.name_for(status_code, code) | ||
| @code = code | ||
| @status_code = status_code | ||
| @why = why | ||
| @fix = fix | ||
| @docs_url = docs_url | ||
| @field = field | ||
| @retry_after = retry_after | ||
| end | ||
|
|
||
| # @api private | ||
| def self.name_for(status_code, code) | ||
| return 'missing_api_key' if code == 'missing_secret_key' || status_code == 401 | ||
| return code == 'network_error' ? 'service_unavailable' : 'application_error' if status_code.nil? | ||
|
|
||
| case status_code | ||
| when 400 then 'validation_error' | ||
| when 403 then 'invalid_permission' | ||
| when 404 then 'not_found' | ||
| when 409 then code == 'template_outdated' ? 'validation_error' : 'concurrent_idempotent_requests' | ||
| when 422 then code == 'idempotency_key_reused' ? 'invalid_idempotent_request' : 'validation_error' | ||
| when 429 then code == 'quota_limited' ? 'monthly_quota_exceeded' : 'rate_limit_exceeded' | ||
| when 503 then 'service_unavailable' | ||
| else 'application_error' | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Medium: Email credentials still reach the ingestion proxy
With
host: 'https://proxy.example.com'andapp_host: 'https://us.posthog.com', this email credential is also passed toFeatureFlagsPolleratclient.rb:236–240, whose constructor fetches/flags/definitionswithAuthorization: Bearer <secret_key>by default (feature_flags.rb:115–121, 1854–1858), exposing the key to the proxy before any email call. Keep email-only credentials out of the ingestion-host poller, or route its secret-bearing requests through the trusted app host.