In Rails 8.1, what is Action Mailbox for, and what do bin/rails action_mailbox:install and bin/rails generate mailbox add?
answer
- receiving, not sending
- ApplicationMailbox under app/mailboxes
- action_mailbox_inbound_emails table
- one process method per mailbox
- conductor in development
basics
~20 sAction Mailbox receives email into a Rails app and routes each message to a mailbox class whose process method handles it. The installer adds ApplicationMailbox and the migrations; generate mailbox adds a mailbox class with process and a test.
solid answer
~30 sAction Mailbox is Rails' framework for **inbound** email: an ingress accepts each message, Rails stores it as an `ActionMailbox::InboundEmail` record and routes it to a mailbox class. `bin/rails action_mailbox:install` writes `app/mailboxes/application_mailbox.rb` (`class ApplicationMailbox < ActionMailbox::Base` with a commented `routing` line), copies the Active Storage and Action Mailbox migrations (the `action_mailbox_inbound_emails` table) and adds a commented `config.action_mailbox.ingress = :relay` to `production.rb`; then run `bin/rails db:migrate`. `bin/rails generate mailbox receipts` creates `app/mailboxes/receipts_mailbox.rb` with an empty `process` method and `test/mailboxes/receipts_mailbox_test.rb`. In development, `/rails/conductor/action_mailbox/inbound_emails` lets you compose test emails by hand.
go deeper
Explain that Action Mailbox receives mail, run the installer and migrations, and generate a mailbox with a process method.
Describe what each generated file holds, why Active Storage is involved, and that routes are added by hand in ApplicationMailbox.
Plan where extracted data lives, since inbound records are deleted later, and keep the development conductor separate from production tooling.
Judge whether email ingestion belongs in the Rails app at all or in a separate intake service, given volume, spam and data retention rules.
## What Action Mailbox is for Action Mailer **sends** email; **Action Mailbox receives** it. Typical uses are replying to a comment thread by email, opening a support ticket from a message, or, in a bookkeeping app, letting users forward expense receipts to `[email protected]` so each one becomes an `Expense` with the receipt attached. The framework handles the plumbing: - an **ingress** that accepts raw messages from a mail server or an email provider; - an **`InboundEmail`** record per message, with the raw source kept in Active Storage and a processing status; - **routing** rules in `ApplicationMailbox` that pick a mailbox class by address; - **mailbox** classes whose `process` method turns the message into application data; - **incineration**, which deletes processed messages after a retention period. A new app created without `--skip-action-mailbox` already loads the framework through `require "rails/all"`, but its tables and base class arrive only with the installer. ## What the installer adds `bin/rails action_mailbox:install` runs the `action_mailbox:install` generator, which: 1. writes `app/mailboxes/application_mailbox.rb`, a subclass of `ActionMailbox::Base` holding a commented `routing /something/i => :somewhere` line; 2. adds a commented `config.action_mailbox.ingress = :relay` to `config/environments/production.rb`, the setting that chooses how mail arrives; 3. copies migrations from Active Storage and Action Mailbox, because each `InboundEmail` stores its raw message as an Active Storage attachment. After `bin/rails db:migrate`, the `action_mailbox_inbound_emails` table has an integer `status`, a `message_id`, a `message_checksum` and a unique index across the last two. ## What the mailbox generator adds `bin/rails generate mailbox receipts` produces: | File | Content | |---|---| | `app/mailboxes/receipts_mailbox.rb` | `class ReceiptsMailbox < ApplicationMailbox` with an empty `process` method | | `test/mailboxes/receipts_mailbox_test.rb` | an `ActionMailbox::TestCase` with a commented `receive_inbound_email_from_mail` example | | `app/mailboxes/application_mailbox.rb` | only if it does not exist yet | Like the mailer generator, it strips a trailing `_mailbox`, so `generate mailbox ReceiptsMailbox` does not produce `ReceiptsMailboxMailbox`. The generator does **not** add a route; you add `routing(/^receipts@/i => :receipts)` to `ApplicationMailbox` yourself. The symbol `:receipts` is turned into the class name `ReceiptsMailbox` when a message matches. ## Writing the first mailbox Inside a mailbox, two readers matter: `inbound_email`, the Active Record row, and `mail`, the parsed `Mail::Message`, which offers `from`, `to`, `subject`, `attachments` and `decoded` for the body. ```ruby class ReceiptsMailbox < ApplicationMailbox def process user = User.find_by!(email_address: mail.from.first) mail.attachments.each do |file| user.expenses.create!(note: mail.subject, receipt: { io: StringIO.new(file.decoded), filename: file.filename, content_type: file.mime_type }) end end end ``` The mailbox should extract what the app needs into its own models, because the `InboundEmail` and its raw message are deleted after the retention period. ## Trying it without a mail server The **conductor**, at `/rails/conductor/action_mailbox/inbound_emails`, lists every `InboundEmail` with its status and offers forms to compose a new message or paste raw source, plus buttons to reroute or incinerate an existing one. Its controllers answer `403 Forbidden` outside the development environment, so it is a local tool, not an admin screen. ## Configuration you touch next After installing, three settings decide how the feature behaves in each environment: - **`config.action_mailbox.ingress`**: which ingress controller accepts mail, set per environment, usually only in `production.rb`. Every other ingress answers `404`. - **`config.action_mailbox.storage_service`**: an optional Active Storage service name for raw messages, if they should live apart from user uploads. - **`config.action_mailbox.incinerate_after`** and **`incinerate`**: how long processed messages are kept, 30 days by default, and whether they are deleted at all. In development you usually set no ingress, because the conductor creates `InboundEmail` records directly and routes them without any HTTP ingress. In tests, the `ActionMailbox::TestCase` helpers do the same. ## How it differs from Action Mailer | | Action Mailer | Action Mailbox | |---|---|---| | Direction | outbound | inbound | | Classes live in | `app/mailers` | `app/mailboxes` | | Entry point | an action that calls `mail` | `process` | | Persistence | none by default | an `InboundEmail` row per message | The two often work together: a mailbox that rejects a message replies through a mailer.
- Why does the installer copy Active Storage migrations too?`ActionMailbox::InboundEmail` declares `has_one_attached :raw_email`, so the full RFC 822 source of each message is stored as an Active Storage blob named `message.eml`. Without the Active Storage tables an ingress could not save incoming mail.
- Can the conductor be used to inspect inbound mail on a production server?No. `Rails::Conductor::BaseController` runs a `before_action` that answers `403 Forbidden` unless `Rails.env.development?`. For production you query `ActionMailbox::InboundEmail` records yourself, for example by `status` or `message_id`.
- Action Mailbox is the part of Action Mailer that handles replies.
- bin/rails generate mailbox receipts also adds the routing rule.
- A new app already has the inbound email table before the installer runs.
- The conductor is an admin screen you can use in production.
- Mailboxes keep the InboundEmail as the permanent record of the data.