Ruby SDK
Install the Mailset Ruby SDK, send email, verify webhooks and handle errors, with examples that run.
- Every public command238 commands, each a typed method
- No dependenciesNothing else to install beside it
- Typed throughoutModel classes, RBS signatures checked
- Retries built inTwo more tries, and a send never doubles
- Timeouts per try30 seconds a try, then tried again
- Idempotency keysA send repeated with its key goes once
On this page
gem install mailset
Create a client
One client holds one MAILSET_KEY. Make it when your server starts and use it for every call.
Pass the key in
Where your app keeps its secrets elsewhere, hand the key to the client yourself. A token goes the same way.
Options
Everything has a default. Name a credential, an org, how long a try may take and how often a call is tried again, only where yours differ.
require 'mailset'
# One client for the whole server.
# Mailset::Client.new alone reads MAILSET_KEY too.
# Nothing is sent until you call a method.
MAILSET = Mailset::Client.new(
key: ENV.fetch('MAILSET_KEY')
)
require 'mailset'
# From your secret store or config, never in the code.
# Or token: with an API token.
mailset = Mailset::Client.new(key: config.mailset_key)
require 'mailset'
mailset = Mailset::Client.new(
# Or token: ENV.fetch('MAILSET_TOKEN'), a person's.
key: ENV.fetch('MAILSET_KEY'),
# Unless given: MAILSET_API, else the key's own.
base_url: 'https://mailset.app',
# For a person in more than one org.
org: 'org_7c2e9a4f',
# Each try may take 10 seconds (30 unless told).
# A network failure, a timeout or a 5xx is tried
# 3 more times (2 unless told), waiting longer
# each time.
timeout: 10,
max_retries: 3,
# A command that needs a passkey waits for
# someone to approve it.
on_approval: lambda do |approval|
puts "Approve it: #{approval.url}"
end
)
p mailset.key.check
Send
Send one message and get its receipt. A message Mailset refuses is an error with a code, never a silent drop.
Give sign-in codes a deadlineHonor unsubscribes and complaintsHandle skipped recipientsGuide for Ruby
require 'mailset'
mailset = Mailset::Client.new
sent = mailset.send_email(
from: 'Acme <moc.elpmaxe@olleh>',
to: 'gro.elpmaxe@ana',
subject: 'Your Acme code',
text: 'Your code is 123456.',
html: '<p>Your code is <b>123456</b>.</p>',
deliver_by: '10m'
)
puts sent.id, sent.status
Send many
Send several messages in one call and get one result per message, in the same order. A refused message never stops the rest.
require 'mailset'
mailset = Mailset::Client.new
results = mailset.send_emails([
{
from: 'Acme <moc.elpmaxe@olleh>',
to: 'gro.elpmaxe@ana',
subject: 'Your invoice',
text: 'Invoice 8842 is ready.'
},
{
from: 'Acme <moc.elpmaxe@olleh>',
to: 'gro.elpmaxe@neb',
subject: 'Your invoice',
text: 'Invoice 8843 is ready.'
}
])
results.each do |result|
if result.ok?
puts "#{result.id} #{result.status}"
else
warn "#{result.message_id} #{result.error.code}"
end
end
Promotional email
Send a newsletter or an offer with mode: 'promotional'. {{mailset_unsubscribe}} is a placeholder Mailset fills in for each message, with a signed one-click unsubscribe link unique to that recipient and that message. You never generate the link, and Mailset adds the matching List-Unsubscribe and List-Unsubscribe-Post headers itself.
require 'mailset'
mailset = Mailset::Client.new
text = <<~TEXT
Three new things this month.
Leave this list: {{mailset_unsubscribe}}
TEXT
html = <<~HTML
<p>Three new things this month.</p>
<a href="{{mailset_unsubscribe}}">Leave this list</a>
HTML
sent = mailset.send_email(
from: 'Acme News <moc.elpmaxe.swen@swen>',
to: 'gro.elpmaxe@ana',
subject: 'What shipped in March',
text: text,
html: html,
mode: 'promotional'
)
p sent.id, sent.skipped
List-Unsubscribe: <https://mailset.app/v1/unsubscribe/REjwEgpOG_zgHOqoFABR-wIAnGDtBkNRV-G7ho9WVWEYl8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcKYW5hQGV4YW1wbGUub3JnCmRfMDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3Cm91dF8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1Njc> List-Unsubscribe-Post: List-Unsubscribe=One-Click
Three new things this month.
Leave this list: https://mailset.app/v1/unsubscribe/REjwEgpOG_zgHOqoFABR-wIAnGDtBkNRV-G7ho9WVWEYl8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcKYW5hQGV4YW1wbGUub3JnCmRfMDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3Cm91dF8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcThree new things this month.
Leave this list
A sample link. Mailset makes a new one for every message.
Three new things this month.
Unsubscribe: https://mailset.app/v1/unsubscribe/REjwEgpOG_zgHOqoFABR-wIAnGDtBkNRV-G7ho9WVWEYl8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcKYW5hQGV4YW1wbGUub3JnCmRfMDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3Cm91dF8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcThree new things this month.
Unsubscribe
Mailset adds the last line of each part. The headers are the same.
Idempotency
Name a send with idempotency_key, and sending it again after a timeout or a crash can’t send it twice.
require 'mailset'
mailset = Mailset::Client.new
receipt = {
from: 'Acme <moc.elpmaxe@olleh>',
to: 'gro.elpmaxe@ana',
subject: 'Your receipt',
text: 'Thanks for your order.',
idempotency_key: 'receipt-8842'
}
first = mailset.send_email(**receipt)
# After a timeout or a crash, send it again.
# The same key answers the first receipt,
# and nothing goes out twice.
again = mailset.send_email(**receipt)
puts first.id == again.id
Check the key
Ask what the key may do now. Call it when your server starts and once a day: each call keeps the key alive.
require 'mailset'
mailset = Mailset::Client.new
# When the server starts, and once a day.
status = mailset.key.check
warn "Paused: #{status.paused}" if status.paused
puts "Works until #{Time.at(status.expires_at / 1000)}"
# A mailbox key sends as its mailbox.
status.mailboxes.each do |box|
next if box.sending == 'ready'
warn "#{box.address}: #{box.sending}"
end
# A domain key sends as the addresses it names.
status.domains.each do |domain|
as = domain.from&.join(', ') || 'any address'
puts "#{domain.name} sends as #{as}"
next if domain.sending == 'ready'
warn "#{domain.name}: #{domain.sending}"
end
Delivery search
Look up what became of the mail this key sent, one line per recipient. For a support question or a nightly check.
require 'mailset'
mailset = Mailset::Client.new
# Did Ana get her receipt?
page = mailset.delivery.search(
recipient: 'gro.elpmaxe@ana',
since: '2026-11-30'
)
page.lines.each do |line|
puts "#{line.status} #{line.reason}"
end
# Pass page.next back as cursor: for the page after.
puts page.next
Webhooks
Check that a request came from Mailset and read its events. It needs no key and no client, so a service that only receives events uses it alone.
In a web framework
The signature covers the body exactly as it arrived. Read the raw body before anything parses it, and hand that to the check: a body parsed as JSON and written out again no longer matches.
require 'mailset'
# A Rack app Mailset posts your hook's events to.
MAILSET_EVENTS = lambda do |env|
events = Mailset::Webhooks.verify(
body: env['rack.input'].read,
headers: env,
secret: ENV.fetch('MAILSET_HOOK_SECRET')
)
events.each do |event|
next unless event.type == 'bounced'
next unless event.bounce.hard?
puts "Stop writing to #{event.recipient}"
end
# 2xx once the events are kept.
# Until then Mailset posts the batch again.
[204, {}, []]
rescue Mailset::Error => e
[401, {}, [e.code]]
end
# app/controllers/mailset_events_controller.rb
# config/routes.rb:
# post 'mailset/events', to: 'mailset_events#create'
class MailsetEventsController < ApplicationController
skip_forgery_protection
def create
# The signature covers the body as it came: verify
# request.raw_post. params, or the body parsed as
# JSON first, lose the bytes it was made over.
events = Mailset::Webhooks.verify(
body: request.raw_post,
headers: request.headers,
secret: ENV.fetch('MAILSET_HOOK_SECRET')
)
events.each do |event|
next unless event.type == 'bounced'
next unless event.bounce.hard?
Errors
Every refusal is one kind of error. Branch on its code, which never changes, and show its message to people.
Retries and timeouts
A network failure or a 5xx answer is tried again, twice unless you set max_retries, waiting longer each time. One try may take 30 seconds unless you set timeout. A refusal is never tried again: the same request gets the same answer.
require 'mailset'
mailset = Mailset::Client.new
begin
mailset.send_email(
from: 'Acme <moc.elpmaxe@olleh>',
to: 'gro.elpmaxe@ana',
subject: 'Your Acme code',
text: 'Your code is 123456.',
deliver_by: '10m'
)
rescue Mailset::Error => e
if e.code == 'limit_reached' && e.retry_at
puts "Try at #{Time.at(e.retry_at / 1000)}"
elsif e.refusal == 'recipient'
puts 'Ask them to check their address.'
else
warn "#{e.code}: #{e.message}"
end
end
Build the message yourself
Get the message a send would build, without sending it: for a queue of your own, or a call to the API from another service.
require 'mailset'
message = Mailset.build_message(
from: 'Acme <moc.elpmaxe@olleh>',
to: 'gro.elpmaxe@ana',
subject: 'Your Acme code',
text: 'Your code is 123456.'
)
# The `data` that messages.submit takes.
data = message.base64
puts message.message_id, data.length
Names
A field keeps its name in every SDK, in each language’s own case. The commands write idempotencyKey, and Ruby writes idempotency_key.
A method is named for its command. Ruby can’t spell these, so each has a name that says what it does:
| Command | In Ruby | Why |
|---|---|---|
messages.send | send_message | send is Object#send, which Ruby itself and many libraries call. |
Version and licence
This page is for version 0.7.0 of the Ruby SDK.
You may use the SDK and change it in your own apps and services, to call Mailset. You may not publish it or pass it on, by itself or inside another library.
Something missing, or not working as this page says? Ask us.