Skip to content
Mailcycle
Get started
Ruby email SDK

Ruby email API: receive, read and send email in Ruby.

The Mailcycle Ruby email API receives email without a mail server or an inbound relay. Create an address, wait for the next message, read its text, HTML and attachments, and reply in the thread. Mail is stored encrypted and opened by your Ruby process; its only dependency is the openssl gem that ships with Ruby.

Install the Ruby gem
gem install mailcycle
Ruby: receive and read a message
require "mailcycle"

mc = Mailcycle.sign_in(ENV.fetch("MAILCYCLE_PHRASE"))
address = mc.addresses.create
puts "Send an email to #{address.email_address}"

message = mc.messages.wait_for(address_id: address.id, timeout: 300)
puts "#{message.subject} #{message.text}"
  • mailcycle
  • Ruby 3.1 or later

The Ruby SDK is one way into the Mailcycle email API. Full reference at https://docs.mailcycle.email.

Quickstart

Receive and send email in Ruby.

Sign in, create an address, wait for a message and answer it. That is the whole loop, and it is the same loop whether the mail is a sign-up code, a customer or a receipt.

  1. 1Install mailcycle. Create a free account (no card) and set MAILCYCLE_PHRASE to the twelve-word recovery phrase it shows you.
  2. 2Create an address with mc.addresses.create. It receives mail straight away.
  3. 3Wait for the next message with mc.messages.wait_for, or take it from a webhook.
  4. 4Reply with mc.messages.send, threaded under the original.
The full Ruby guide
Ruby: create an address, wait for mail, reply
require "mailcycle"

mc = Mailcycle.sign_in(ENV.fetch("MAILCYCLE_PHRASE"))

# A new address that receives mail, sealed to your key.
address = mc.addresses.create(prefix: "signup", label: "Test run 42")
puts address.email_address # signup-x7k2@...

# Wait for the verification email, decrypted here.
message = mc.messages.wait_for(address_id: address.id, subject: "verification")
puts "#{message.subject} #{message.text}"

# Reply in the same thread.
mc.messages.send(
  from: address.email_address,
  to: message.reply_to,
  subject: "Re: #{message.subject}",
  text: "Thanks, confirmed.",
  reply_to: message
)
Receive email

Receive email in Ruby.

Create an address, wait for the next email that lands on it, then read the sender, subject, text and attachments from a plain Struct. Mail that is already there comes back from all.

  • wait_for returns the next message, decrypted, within the timeout you set.
  • mc.messages.all(address.id) lists the mail already received.
  • Attachments come back as binary strings, opened by your Ruby process.
Ruby: receive email and save its attachments
require "mailcycle"

mc = Mailcycle.sign_in(ENV.fetch("MAILCYCLE_PHRASE"))

# An address that receives email from now on.
address = mc.addresses.create(prefix: "orders")
puts "Send an email to #{address.email_address}"

# Receive the next email, waiting up to five minutes.
message = mc.messages.wait_for(address_id: address.id, timeout: 300)
puts "#{message.from_address}: #{message.subject}"
puts message.text

# Save each attachment, opened on this machine.
message.attachments.each do |file|
  bytes = mc.messages.attachment(message, file.index)
  File.binwrite(File.basename(file.filename), bytes)
end
What it does

The whole email API, from Ruby.

Everything the Mailcycle console can do is in the gem: addresses, inbound mail, sending, webhooks, events, API keys and devices.

Create email addresses from Ruby

mc.addresses.create mints a new address on a shared domain, or on your own domain from Scale, and it receives mail at once. Give it a prefix and a label sealed on your machine.

Receive email and wait for it

mc.messages.wait_for listens on the event stream and returns the next matching message, filtered by sender or subject, opened and ready to read. Without a stream it checks every five seconds.

Read and parse incoming email

mc.messages.all and .attachment return the sender, subject, plain text, the original HTML, a safe HTML with remote images and trackers removed, and every attachment as bytes.

Send email and reply in the thread

mc.messages.send sends from an address your account owns, with up to ten files. Pass the message you are answering and the threading headers are filled in.

Inbound email webhooks

Register a URL and every message.received event is POSTed to it, signed with HMAC-SHA256 and retried. Mailcycle.verify_webhook_signature checks the signature in one line.

A live event stream

mc.events.stream carries the same events over a WebSocket the moment they happen, and reconnects after a drop. Events carry ids, never content.

Email testing

Test email verification in RSpec, Minitest and Capybara.

Create an address in a before block, fill your sign-up form with Capybara, then call wait_for and read the code from message.text. A timeout raises Mailcycle::ApiError with code "timeout", so the spec fails with a reason instead of hanging.

  • A real address on a real domain, so the mail goes through the same path your users' does.
  • One call blocks until the message lands, filtered by subject or sender, with a timeout you set.
  • Text, HTML and attachments come back parsed, so pulling out a code or a link is a regular expression.
  • Mail received from the moment you start waiting counts, even if it lands before the stream is up.
Inbound email webhooks

Handle incoming email in Rails, Sinatra and Hanami.

Rather than polling, let the mail come to you. A webhook POSTs each event to your URL the moment it happens, signed so you can prove it came from Mailcycle, and retried if your server is down.

Events carry ids, never content. Fetch the message by id and it is opened in your code, exactly as it would be otherwise. Webhooks are on Operator and above.

Webhooks in the docs
Rails: verify a webhook, then fetch the message
class MailcycleWebhooksController < ActionController::API
  def create
    raw = request.raw_post # the body exactly as it arrived
    signature = request.headers["Mailcycle-Signature"]
    return head :unauthorized unless Mailcycle.verify_webhook_signature(SECRET, raw, signature)

    event = JSON.parse(raw)
    if event["type"] == "message.received"
      message = MAILCYCLE.messages.get(event.dig("payload", "messageId"))
      HandleMailJob.perform_later(message.subject, message.text)
    end
    head :no_content
  end
end
No mail server

Instead of the mail gem and Action Mailbox.

Action Mailer and the mail gem send through an SMTP server you configure. Action Mailbox receives, but only through an ingress: a relay from Postfix or Exim, or an inbound service that forwards to your app.

Here an address is one call, there is no ingress to configure, and mail arrives on a webhook or through wait_for. Each message comes back parsed: sender, subject, text, safe_html with remote content removed, and attachments as binary strings.

Sending is built for answering the mail your addresses receive, within your plan's daily limit.

Encrypted on arrival

Mail is encrypted the moment it arrives and opened in your Ruby code.

Each address has a key pair derived from your recovery phrase. Incoming mail is sealed to its public key before anything is written down, and the gem derives the private key on your machine to open it.

How the encryption works
  • Signing in proves you hold the key by answering a challenge, so the phrase never crosses the wire.
  • Bodies, attachments, address labels and API key names are all sealed. The server stores ciphertext.
  • Mail sealed by one SDK opens in every other, and in the app, because they share one published format.
  • The gem is checked against Mailcycle's published test vectors, including the cases that must be refused.
  • Search runs on your device, where mail is decrypted.
Use cases

What developers build with the Ruby SDK.

It runs in Rails apps, Sidekiq jobs, Rake tasks and scripts: anywhere Ruby code needs an email address of its own.

End-to-end tests for sign-up and login

Verification emails, one-time passcodes, magic links, password resets and invitations, checked by a real test in RSpec, Minitest and Capybara.

A support or orders inbox your code answers

Give an address to a queue, read what customers send, and reply from the address they wrote to, threaded under their message.

Receipts, invoices and alerts as data

Point suppliers, monitoring and other services at an address and pull the text and attachments straight into your own records.

An address per customer, job or device

Mint addresses on demand and keep each stream of mail separate, with its own label, and delete one when the job is done.

Building an AI agent? It can have an inbox too, through the Mailcycle MCP server, without writing any Ruby at all.

The package

Ruby SDK at a glance.

Maintained by Mailcycle, the team that runs the API.

The Mailcycle Ruby SDK at a glance
Packagemailcycle
RegistryRubyGems
Install withgem, or bundle add mailcycle
NeedsRuby 3.1 or later
AlsoIts only dependency is the openssl gem that ships with Ruby.
LicenceApache 2.0, free to use in your own applications
APIJSON over HTTPS at api.mailcycle.email
GuideThe Ruby guide in the docs
GuidesSend email from RubyReceive email in Ruby

What each plan unlocks. Free receives mail. A signed-in session sends from Personal up. API keys and webhooks start at Operator, and sending from an API key at Scale. See pricing.

Questions

Ruby email SDK questions.

Run gem install mailcycle, sign in with Mailcycle.sign_in, and create an address with mc.addresses.create. Mail sent to it is stored encrypted, and mc.messages.all or mc.messages.wait_for returns it decrypted and parsed. No mail server and no inbound relay.

The Mailcycle email API is JSON over HTTPS, and the mailcycle gem is its Ruby client. It creates email addresses, receives and reads their mail, sends replies with attachments, verifies webhooks and streams events, and it decrypts each message inside your Ruby process.

The mail gem builds and parses messages, and delivers or retrieves them through SMTP, POP3 or IMAP servers you set up. The mailcycle gem works over one HTTPS API: an address is one call, its mail is stored for you, and each message comes back already parsed into sender, subject, text, HTML and attachments.

Every message lists its files in message.attachments, each with index, filename, mime_type and size. mc.messages.attachment(message, file.index) returns the bytes as a binary string, decrypted on your machine, ready for File.binwrite or Active Storage.

Create an address in a before block, submit it through your sign-up form with Capybara, then call mc.messages.wait_for(address_id: address.id, subject: "verify") and read the code or link from message.text. Delete the address in an after block to free its place on your plan.

Yes. It is a plain gem whose only dependency is the openssl gem that ships with Ruby. Add it with bundle add mailcycle, keep one signed-in client in an initializer, and put the webhook endpoint in a controller that inherits from ActionController::API.

Call mc.messages.send(from:, to:, subject:, text:) with an address your account owns. Pass reply_to: message to thread a reply under the original. Sending from a signed-in session works from the Personal plan, and from an API key on Scale and above.

The gem is free and open source under the Apache 2.0 licence. The Mailcycle Free plan costs nothing and needs no card: 3 email addresses and 3,000 messages received a month, receive only. Sending starts on Personal at $7 a month, API keys and webhooks on Operator at $15, and sending from an API key on Scale at $19.

Incoming mail is encrypted to the address's public key the moment it arrives, and the private key is derived from your recovery phrase inside your Ruby process. Mail you send leaves in plain text so the recipient's server can read it, and the copy kept in Sent is sealed like the rest.

Call mc.addresses.create at the start of the run and delete the address at the end, which frees its place on your plan. Each address receives mail as soon as it exists. This is for testing your own product and for mail meant for you: registering accounts on other services in bulk, or against their terms, breaks the Acceptable Use Policy.

Up to 60 messages an hour per account and 20 an hour per device or agent (more on Fleet and Enterprise), within your plan's daily limit. The Acceptable Use Policy covers what may be sent.

The API is plain JSON over HTTPS at api.mailcycle.email, so anything can call it. Message content comes back encrypted, though, and the gem is what derives the keys and opens it. Without an SDK you would implement the published encryption format yourself.

Give your Ruby code an email address.

Create an account, create an address, and read what arrives. The free tier needs no card.