Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1ae7ecf
feat(emails): schedule a send with scheduled_at
Silthus Nov 23, 2026
d072918
fix(emails): confirm the exact scheduled instant and keep app host cr…
Silthus Nov 23, 2026
a585386
refactor(emails): format scheduled_at with the shared ISO 8601 helper
Silthus Nov 23, 2026
b1304d2
fix(emails): log send warnings after the send settles
Silthus Nov 23, 2026
7b53e92
fix(emails): let interrupts through while logging and refuse non-UTF-…
Silthus Nov 23, 2026
0e86b5c
fix(emails): refuse a send-now answer for a time more than a minute a…
Silthus Nov 24, 2026
f0fa1c0
test(emails): pin the send-now cutoff to the minute and show a caller…
Silthus Nov 24, 2026
430a8f2
fix(emails): let a deadline with an error class through while logging…
Silthus Nov 24, 2026
9e3b814
fix(emails): measure the send-now minute from the send and never repl…
Silthus Nov 24, 2026
8a90d74
test(emails): pin the send-now clock to the first attempt across retries
Silthus Nov 24, 2026
eadc96e
revert: test(emails): pin the send-now clock to the first attempt acr…
Silthus Nov 24, 2026
3e4f675
test(emails): pin the send-now clock across retries and assert settle…
Silthus Nov 24, 2026
9a20902
fix(emails): give a logger's reused exception the failed send's cause
Silthus Nov 24, 2026
9d093b8
fix(emails): raise a copy of a logger's reused exception
Silthus Nov 24, 2026
abd954b
fix(emails): raise the send's error when a logger's exception can't b…
Silthus Nov 24, 2026
c79edb5
fix(emails): let a caller's deadline through while copying a logger's…
Silthus Nov 24, 2026
df58abf
fix(emails): stop copying a logger's exception
Silthus Nov 24, 2026
f41adaa
fix(emails): never log a failed send's warnings, carry them on its error
Silthus Nov 24, 2026
071000a
chore(emails): name EmailError#warnings in the changeset
Silthus Nov 24, 2026
2dfb241
test(emails): drive the logger specs through a real logger's formatter
Silthus Nov 24, 2026
17744e9
docs(emails): name the pattern-match change of SendResult's new member
Silthus Nov 25, 2026
bdda344
fix(emails): refuse a scheduled_at string in an encoding the pattern …
Silthus Nov 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/scheduled-email-sends.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-ruby": minor
---

Schedule a transactional email with `scheduled_at` on `emails.send`, at most 7 days ahead, on PostHog Cloud. The result's `status` is `scheduled` with the confirmed `scheduled_at`, or `queued` when the time had passed. `emails.get` reads `scheduled` until the email goes out. The client rounds a finer time up to the next millisecond and refuses a time without an offset. It refuses to schedule on any host other than PostHog Cloud US or EU, and raises `schedule_not_confirmed` with `EmailError#message_id` when PostHog accepts the email without confirming its time, also when PostHog would send now an email due more than a minute ahead. That error carries the reply's warnings on the new `EmailError#warnings`, and the client logs warnings only for a send that succeeds. `SendResult` gains a `scheduled_at` member, so a `SendResult` marshaled by an earlier version, such as one in a Rails cache, doesn't load in this version, and one marshaled by this version doesn't load in an earlier one. The new member also changes code that takes `SendResult` apart by position: a pattern match on three elements, such as `in [id, replayed, warnings]`, no longer matches, and `to_h` and `to_a` gain `scheduled_at`. A hash pattern such as `in { id: }` still matches.
18 changes: 15 additions & 3 deletions lib/posthog/email_error.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module PostHog
# PostHog did not queue the email, or the client could not tell whether it did.
# PostHog did not queue the email, or the client could not tell whether it did. An error with a {#message_id}
# is the exception: PostHog accepted that email, which may still go out, so read it with {Emails#get} before you
# send again.
#
# 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.
Expand All @@ -23,9 +25,16 @@ class EmailError < StandardError
attr_reader :field
# @return [Float, nil] How long PostHog asked the client to wait before it sends again, in seconds.
attr_reader :retry_after
# @return [String, nil] The message ID of an email PostHog accepted although the call failed, such as an email
# whose `schedule_not_confirmed` means it may go out at another time. Read its status with {Emails#get}.
attr_reader :message_id
# @return [Array<Emails::Warning>] PostHog's warnings about an email it accepted although the call failed. The
# client logs warnings only for a call that succeeds.
attr_reader :warnings

# @api private
def initialize(code:, message:, status_code: nil, why: nil, fix: nil, docs_url: nil, field: nil, retry_after: nil)
def initialize(code:, message:, status_code: nil, why: nil, fix: nil, docs_url: nil, field: nil, retry_after: nil,
message_id: nil, warnings: [])
super(message)
@name = self.class.name_for(status_code, code)
@code = code
Expand All @@ -35,13 +44,16 @@ def initialize(code:, message:, status_code: nil, why: nil, fix: nil, docs_url:
@docs_url = docs_url
@field = field
@retry_after = retry_after
@message_id = message_id
@warnings = warnings.dup.freeze
end

SDK_ERROR_NAMES = {
'network_error' => 'service_unavailable',
'invalid_attachment' => 'validation_error',
'too_many_attachments' => 'validation_error',
'message_too_large' => 'validation_error'
'message_too_large' => 'validation_error',
'scheduling_requires_cloud' => 'validation_error'
}.freeze
private_constant :SDK_ERROR_NAMES

Expand Down
156 changes: 141 additions & 15 deletions lib/posthog/emails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'json'
require 'net/http'
require 'openssl'
require 'date'
require 'securerandom'
require 'time'
require 'uri'
Expand Down Expand Up @@ -44,28 +45,47 @@ module PostHog
# html: rendered_html,
# distinct_id: 'user_123'
# )
#
# @example An email scheduled for tomorrow, on PostHog Cloud
# result = posthog.emails.send(
# to: 'user@example.com',
# template_id: '0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b',
# scheduled_at: Time.now + 86_400,
# idempotency_key: 'trial-ending:user_123'
# )
# result.status # => "scheduled"
# result.scheduled_at # => the time in UTC
class Emails
include PostHog::Logging
include PostHog::Utils

# The queued email. A result means PostHog queued the email, not that it was delivered.
# The accepted email. A result means PostHog queued or scheduled the email, not that it was delivered.
#
# @!attribute [r] id
# @return [String] The queued email's message ID.
# @return [String] The accepted email's message ID.
# @!attribute [r] replayed
# @return [Boolean] Whether PostHog had already accepted this idempotency key and returned the first result.
# @!attribute [r] warnings
# @return [Array<Warning>]
SendResult = Struct.new(:id, :replayed, :warnings, keyword_init: true)
# @!attribute [r] scheduled_at
# @return [Time, nil] When a scheduled email goes out, in UTC. +nil+ for an email that goes out now, also when
# its +scheduled_at+ had already passed when PostHog accepted it.
SendResult = Struct.new(:id, :replayed, :warnings, :scheduled_at, keyword_init: true) do
Comment thread
Silthus marked this conversation as resolved.
Resolved
# @return [String] `scheduled` when the email waits for its {#scheduled_at}, `queued` when it goes out now.
def status
scheduled_at.nil? ? 'queued' : 'scheduled'
end
end

# A warning PostHog returned with a queued email.
Warning = Struct.new(:code, :message, :fix, keyword_init: true)

# What happened to an email that {Emails#send} queued.
#
# @!attribute [r] status
# @return [String] `queued`, `sent`, `failed`, `delivered`, `bounced` or `complained`. Later SDK versions
# can see values this list does not have yet.
# @return [String] `scheduled`, `queued`, `sent`, `failed`, `delivered`, `bounced` or `complained`. A
# scheduled email reads `scheduled` until it goes out. Later SDK versions can see values this list does not
# have yet.
# @!attribute [r] reason
# @return [String, nil] Why a failed email was not sent, such as `recipient_suppressed`, or the bounce type
# of a bounced email: `hard`, `soft` or `unknown`.
Expand All @@ -76,6 +96,7 @@ class Emails
STATUS_PATH = '/api/messaging/emails/%<message_id>s/'
US_APP_HOST = 'https://us.posthog.com'
EU_APP_HOST = 'https://eu.posthog.com'
SCHEDULING_APP_HOSTS = [US_APP_HOST, EU_APP_HOST].freeze
CLOUD_APP_HOSTS = {
'https://us.i.posthog.com' => US_APP_HOST,
'https://us.posthog.com' => US_APP_HOST,
Expand All @@ -88,6 +109,13 @@ class Emails
IDEMPOTENCY_KEY_PATTERN = /\A[\x21-\x7e]{1,255}\z/
TEMPLATE_KEY_PATTERN = /\A[a-zA-Z0-9_-]{1,400}\z/
HEADER_TOKEN_PATTERN = /\A[\x21-\x7e]+\z/
SCHEDULED_AT_PATTERN = /
\A(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?
(?:Z|([+-]\d{2}):?(\d{2}))\z
/x
SEND_NOW_TOLERANCE_SECONDS = 60
SCHEDULED_AT_NEEDED = 'scheduled_at must be a Time, or an ISO 8601 time with an offset, such as ' \
'2026-12-01T09:00:00Z or 2026-12-01T10:00:00+01:00, so no email was sent.'
JSON_CONTENT = { 'Content-Type' => 'application/json' }.freeze
SEND_KEY_NEEDED = 'Sending email needs secret_key, a project secret API key with the ' \
'transactional_email:write scope.'
Expand All @@ -103,9 +131,11 @@ class Emails
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, Zlib::Error
].freeze
private_constant :SEND_PATH, :UPLOADS_PATH, :STATUS_PATH, :US_APP_HOST, :EU_APP_HOST, :CLOUD_APP_HOSTS,
:MAX_RETRY_DELAY_SECONDS, :UUID_PATTERN, :IDEMPOTENCY_KEY_PATTERN, :TEMPLATE_KEY_PATTERN,
:HEADER_TOKEN_PATTERN, :JSON_CONTENT, :SEND_KEY_NEEDED, :READ_KEY_NEEDED, :TEMPLATE_FORM_WHY,
:CODE_FORM_WHY, :TEMPLATE_FIELDS, :CODE_FIELDS, :NETWORK_ERRORS
:SCHEDULING_APP_HOSTS, :MAX_RETRY_DELAY_SECONDS, :UUID_PATTERN, :IDEMPOTENCY_KEY_PATTERN,
:TEMPLATE_KEY_PATTERN, :HEADER_TOKEN_PATTERN, :SCHEDULED_AT_PATTERN, :SEND_NOW_TOLERANCE_SECONDS,
:SCHEDULED_AT_NEEDED,
:JSON_CONTENT, :SEND_KEY_NEEDED, :READ_KEY_NEEDED, :TEMPLATE_FORM_WHY, :CODE_FORM_WHY,
:TEMPLATE_FIELDS, :CODE_FIELDS, :NETWORK_ERRORS

# @api private
def initialize(secret_key:, host:, app_host:, timeout:, max_retries:, migrated_from:, user_agent:, disabled:,
Expand Down Expand Up @@ -181,19 +211,36 @@ def initialize(secret_key:, host:, app_host:, timeout:, max_retries:, migrated_f
# retries the send.
# @param max_retries [Integer] Overrides the client's `email_request_max_retries` for this send. +0+ turns
# retries off.
# @return [SendResult] The queued email.
# @param scheduled_at [Time, DateTime, String] When to send the email, at most 7 days ahead: a +Time+, such as
# +Time.now + 3600+ or a Rails time with zone, a +DateTime+, or an ISO 8601 time with an offset, such as
# `2026-12-01T10:00:00+01:00`. A time that has passed sends the email now. PostHog keeps the time to the
# millisecond, and the client rounds a finer time up to the next millisecond and sends that time on every retry.
# When PostHog sends the email now, the client accepts that for a time up to a minute ahead by this machine's
# clock, to allow for clock skew, so such an email can go out up to a minute early. Work out a relative time
# once and pass the same time when you send again with the same +idempotency_key+: another time with that key
# raises `idempotency_key_reused`. Only PostHog Cloud schedules emails. To stop scheduled emails, disable the
# email's workflow in PostHog until they have reached a final status, which refuses every new send of it
# meanwhile.
# @return [SendResult] The accepted email.
# @raise [EmailError] PostHog did not queue the email, or the client could not tell whether it did.
# With +scheduled_at+, {EmailError#code} is `scheduling_requires_cloud` before any request when the client's
# app host isn't PostHog Cloud US or EU, such as a self-hosted PostHog, a reverse proxy or a custom domain, which
# could send the email at once. It is `schedule_not_confirmed` when PostHog accepted the email without confirming
# its time, also when PostHog sends now an email due more than a minute ahead by this machine's clock: the email
# exists and may go out at another time, and {EmailError#message_id} names it. Read it with {#get} before you
# send again. The reply's warnings are on {EmailError#warnings}, and the client logs none of them.
# Before any request, {EmailError#code} is `invalid_attachment` for a file the client can't send,
# `too_many_attachments` for more than 10 files, and `message_too_large` for files too large for one email.
# It is `attachment_upload_failed` when file storage refused an upload, and `not_found` with
# {EmailError#field} `attachments` when PostHog has no upload route for this project. Unless `app_host` is
# an http URL, an upload URL without https raises `api_error` on the file's field, such as
# `attachments__0`, before any file goes out. No email was sent.
# @raise [ArgumentError] An argument has the wrong type or format, or the call mixes the two forms.
# @raise [ArgumentError] An argument has the wrong type or format, such as a +scheduled_at+ without an offset, or
# the call mixes the two forms.
def send(
to:, template_id: nil, template_data: nil, template_key: nil, subject: nil, html: nil, text: nil, from: nil,
reply_to: nil, cc: nil, bcc: nil, attachments: nil, sensitive: nil, tracking: nil, capture_engagement: nil,
distinct_id: nil, idempotency_key: nil, max_retries: nil
distinct_id: nil, idempotency_key: nil, max_retries: nil, scheduled_at: nil
Comment thread
Silthus marked this conversation as resolved.
Resolved
)
fields = {
template_data: template_data, subject: subject, html: html, text: text, from: from, reply_to: reply_to,
Expand All @@ -202,20 +249,28 @@ def send(
}.compact
check_recipient(to, distinct_id, idempotency_key, max_retries)
payload = content(template_id, template_key, fields).merge(to: to)
due = scheduled_time(scheduled_at)
files = EmailAttachments.from(fields.fetch(:attachments, []), bodies_with_derived_text(fields), fields[:subject])
payload[:attachments] = files.references unless files.empty?
payload[:distinct_id] = distinct_id unless distinct_id.nil?
payload[:scheduled_at] = time_in_iso8601(due) if due
body = JSON.generate(payload)
headers = { 'Content-Type' => 'application/json', 'Idempotency-Key' => idempotency_key || SecureRandom.uuid }

result = call('no email was sent', SEND_KEY_NEEDED) do |secret_key|
result, unconfirmed = call('no email was sent', SEND_KEY_NEEDED) do |secret_key|
check_schedulable if due
upload(files, secret_key, max_retries || @max_retries) unless files.empty?
uri = URI(@app_host + SEND_PATH)
# A slow reply must not move a time that was more than a minute ahead into the send-now window.
sent_at = Time.now
with_retries(max_retries || @max_retries) do
parse_send_reply(request(Net::HTTP::Post.new(uri), uri, secret_key, headers, body))
parse_send_reply(request(Net::HTTP::Post.new(uri), uri, secret_key, headers, body), due, sent_at)
end
end
raise unconfirmed if unconfirmed

result.warnings.each { |warning| logger.warn("PostHog email warning #{warning.code}: #{warning.message}") }

result
end

Expand Down Expand Up @@ -317,6 +372,50 @@ def nonempty_string?(value)
value.is_a?(String) && !value.strip.empty?
end

def scheduled_time(value)
return nil if value.nil?

instant = case value
when DateTime then Time.at((value - DateTime.new(1970, 1, 1)) * 86_400)
when Time then Time.at(value.to_r)
when String then offset_time(value)
end
raise ArgumentError, SCHEDULED_AT_NEEDED if instant.nil?

instant.utc.ceil(3)
end

def offset_time(value)
return nil unless value.valid_encoding? && value.encoding.ascii_compatible?

parts = SCHEDULED_AT_PATTERN.match(value)
return nil if parts.nil?

year, month, day, hour, minute, second, fraction, offset_hours, offset_minutes = parts.captures
return nil unless Date.valid_date?(year.to_i, month.to_i, day.to_i, Date::GREGORIAN)
return nil unless hour.to_i <= 23 && minute.to_i <= 59 && second.to_i <= 59
return nil unless offset_hours.to_s[1, 2].to_i <= 23 && offset_minutes.to_i <= 59

seconds = second.to_i + (fraction ? Rational(fraction.to_i, 10**fraction.size) : 0)
offset = offset_hours ? "#{offset_hours}:#{offset_minutes}" : '+00:00'
Time.new(year.to_i, month.to_i, day.to_i, hour.to_i, minute.to_i, seconds, offset)
end

def check_schedulable
return if SCHEDULING_APP_HOSTS.include?(@app_host)

uri = URI(@app_host)
host = uri.port == uri.default_port ? uri.host : "#{uri.host}:#{uri.port}"
raise EmailError.new(
code: 'scheduling_requires_cloud', field: 'scheduled_at',
message: 'Only PostHog Cloud (us.posthog.com or eu.posthog.com) schedules emails, and this client sends ' \
"email to #{host}, so no email was sent.",
why: "A PostHog that doesn't schedule emails would send this one at once.",
fix: "Send without scheduled_at, or set app_host to PostHog Cloud's app host. A reverse proxy or custom " \
"domain can't schedule emails yet."
)
end

def content(template_id, template_key, fields)
if template_id.nil? == template_key.nil?
raise ArgumentError, 'Send either template_id, or template_key with subject and html or text.'
Expand Down Expand Up @@ -593,7 +692,7 @@ def parse_json(body)
nil
end

def parse_send_reply((status, payload, retry_after))
def parse_send_reply((status, payload, retry_after), due, sent_at)
fields = payload.is_a?(Hash) ? payload : {}
raise reply_error(fields, status, retry_after) unless status.between?(200, 299)

Expand All @@ -603,7 +702,34 @@ def parse_send_reply((status, payload, retry_after))
status_code: status)
end

SendResult.new(id: id, replayed: fields['replayed'] == true, warnings: warnings(fields['warnings']))
result = SendResult.new(id: id, replayed: fields['replayed'] == true, warnings: warnings(fields['warnings']),
scheduled_at: nil)
return [result, nil] if due.nil? || time_had_passed?(fields, due, sent_at)
return [result, schedule_not_confirmed(id, status, result.warnings)] unless confirms?(fields, due)

result.scheduled_at = due
[result, nil]
end

def time_had_passed?(fields, due, sent_at)
fields['status'] == 'queued' && fields.key?('scheduled_at') && fields['scheduled_at'].nil? &&
due <= sent_at + SEND_NOW_TOLERANCE_SECONDS
end

def confirms?(fields, due)
answered = fields['scheduled_at']
fields['status'] == 'scheduled' && answered.is_a?(String) && offset_time(answered) == due
end

def schedule_not_confirmed(message_id, status, warnings)
EmailError.new(
code: 'schedule_not_confirmed', status_code: status, field: 'scheduled_at', message_id: message_id,
warnings: warnings,
message: "PostHog accepted email #{message_id} without confirming its scheduled_at, so it may go out at " \
'another time.',
fix: "Read what happened to the email with emails.get('#{message_id}') before you send it again: a send " \
'with another idempotency_key makes a second email.'
)
end

def warnings(entries)
Expand Down
7 changes: 6 additions & 1 deletion public_api_snapshot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,24 @@ instance_method PostHog::EmailError#code()
instance_method PostHog::EmailError#docs_url()
instance_method PostHog::EmailError#field()
instance_method PostHog::EmailError#fix()
instance_method PostHog::EmailError#message_id()
instance_method PostHog::EmailError#name()
instance_method PostHog::EmailError#retry_after()
instance_method PostHog::EmailError#status_code()
instance_method PostHog::EmailError#warnings()
instance_method PostHog::EmailError#why()
class PostHog::Emails
instance_method PostHog::Emails#get(message_id)
instance_method PostHog::Emails#send(to:, template_id: ..., template_data: ..., template_key: ..., subject: ..., html: ..., text: ..., from: ..., reply_to: ..., cc: ..., bcc: ..., attachments: ..., sensitive: ..., tracking: ..., capture_engagement: ..., distinct_id: ..., idempotency_key: ..., max_retries: ...)
instance_method PostHog::Emails#send(to:, template_id: ..., template_data: ..., template_key: ..., subject: ..., html: ..., text: ..., from: ..., reply_to: ..., cc: ..., bcc: ..., attachments: ..., sensitive: ..., tracking: ..., capture_engagement: ..., distinct_id: ..., idempotency_key: ..., max_retries: ..., scheduled_at: ...)
class_method PostHog::Emails.logger()
class PostHog::Emails::SendResult < Struct
instance_method PostHog::Emails::SendResult#id()
instance_method PostHog::Emails::SendResult#id=(_)
instance_method PostHog::Emails::SendResult#replayed()
instance_method PostHog::Emails::SendResult#replayed=(_)
instance_method PostHog::Emails::SendResult#scheduled_at()
instance_method PostHog::Emails::SendResult#scheduled_at=(_)
instance_method PostHog::Emails::SendResult#status()
instance_method PostHog::Emails::SendResult#warnings()
instance_method PostHog::Emails::SendResult#warnings=(_)
class_method PostHog::Emails::SendResult.[](*arg0)
Expand Down
Loading