In Rails Action Mailbox, what happens to an inbound email between the ingress request and a mailbox's process method?
answer
- config.action_mailbox.ingress picks one controller
- raw source saved, then 204
- Message-ID plus checksum, unique
- after_create_commit enqueues RoutingJob
- pending, processing, delivered, failed, bounced
basics
~20 sThe configured ingress controller saves the raw message as an InboundEmail with status pending and answers at once. A RoutingJob then asks ApplicationMailbox for the first matching route and runs that mailbox's callbacks and process, updating the status.
solid answer
~40 s`config.action_mailbox.ingress` selects one ingress: `:relay` for Exim, Postfix or Qmail through `bin/rails action_mailbox:ingress:postfix` and friends, or a provider such as `:mailgun`, `:mandrill`, `:postmark` or `:sendgrid`; the others answer 404. The ingress authenticates the request and calls `InboundEmail.create_and_extract_message_id!`, which stores the raw source in Active Storage and records the Message-ID and a SHA1 checksum; a duplicate hits a unique index and is silently skipped. The record starts `pending`, and `after_create_commit` enqueues `ActionMailbox::RoutingJob`, so the HTTP request returns fast. The job calls `ApplicationMailbox.route`, which picks the first matching route, sets the status to `processing`, runs callbacks and `process`, then `delivered`, or `bounced` / `failed`. On load_defaults 8.1 the routing job uses the `"default"` queue.
code
ruby · 3 lines# config/environments/production.rb
config.action_mailbox.ingress = :relay
config.action_mailbox.queues.routing = :inbound_mailgo deeper
Know that an ingress receives raw mail, Rails stores an InboundEmail, and a mailbox's process method handles it later.
Walk through ingress selection, create_and_extract_message_id!, the after_create_commit RoutingJob and the five statuses.
Reason about duplicates, unmatched mail, failed statuses and queue placement, and monitor InboundEmail rows stuck or failing in production.
Choose between running your own MTA with the relay ingress and a provider webhook, weighing control, deliverability work and operational load.
## Step 1: the ingress accepts the message An **ingress** is a controller that receives raw email over HTTP. Only one is active, chosen with `config.action_mailbox.ingress` in the production environment file. Every ingress controller inherits a `before_action` that answers `404 Not Found` unless it is the configured one. | Ingress | Mounted at | Fed by | |---|---|---| | `:relay` | `/rails/action_mailbox/relay/inbound_emails` | your own MTA piping to `bin/rails action_mailbox:ingress:exim`, `:postfix` or `:qmail` | | `:mailgun` | `/rails/action_mailbox/mailgun/inbound_emails/mime` | Mailgun's webhook | | `:mandrill` | `/rails/action_mailbox/mandrill/inbound_emails` | Mandrill's webhook | | `:postmark` | `/rails/action_mailbox/postmark/inbound_emails` | Postmark's webhook | | `:sendgrid` | `/rails/action_mailbox/sendgrid/inbound_emails` | SendGrid's inbound webhook | The relay ingress uses HTTP basic authentication with the user name `actionmailbox` and an ingress password, requires a `message/rfc822` body (`415` otherwise) and answers `204 No Content` on success. The Mailgun and Mandrill ingresses verify the provider's signature with its signing key; the Postmark and SendGrid ingresses use the same basic-auth ingress password as the relay. The `bin/rails action_mailbox:ingress:*` tasks read the message from standard input and post it to the `URL` you pass, with `INGRESS_PASSWORD`; the relayer gives every network or HTTP failure a temporary `4.x.x` status code, so the mail server can retry later. ## Step 2: an `InboundEmail` is created Every ingress calls `ActionMailbox::InboundEmail.create_and_extract_message_id!` with the raw source. It: 1. computes a SHA1 `message_checksum` of the source; 2. reads the `Message-ID` header, generating one from the checksum (with a logged warning) if it is missing or unparsable; 3. uploads the source as an Active Storage blob named `message.eml` with content type `message/rfc822`, attached as `raw_email`; 4. creates the row. If the identical raw source arrives twice, the unique index on `message_id` and `message_checksum` raises `ActiveRecord::RecordNotUnique`, which the method rescues and returns `nil`, so a redelivered email is ignored rather than processed twice. ## Step 3: routing happens in a job `InboundEmail` includes the `Routable` concern: `after_create_commit :route_later, if: :pending?`. New rows default to `pending`, so the commit enqueues `ActionMailbox::RoutingJob`, and the ingress request returns without waiting for your code. This keeps the ingress fast and lets slow processing scale on workers. The job calls `inbound_email.route`, which calls `ApplicationMailbox.route`. The router walks the `routing` rules in declaration order, takes the **first** match and calls that mailbox class's `receive`. If nothing matches, the email is marked `bounced` and `ActionMailbox::Router::RoutingError` is raised in the job. ## Step 4: the mailbox runs and the status moves - **`pending`**: stored and waiting for routing. - **`processing`**: set just before the mailbox's `before_processing` callbacks and `process` run. - **`delivered`**: `process` finished and nothing bounced the message. - **`bounced`**: a callback or `process` called `bounced!` or `bounce_with`, or no route matched. - **`failed`**: an exception escaped the callbacks or `process`. The status is set before the mailbox's `rescue_from` handlers run; a handler can still move it to `bounced` with `bounced!`, and an unhandled exception is re-raised so the job fails visibly. `delivered`, `bounced` and `failed` count as **processed**, which schedules incineration. ## Queues The engine names two queues, `:action_mailbox_routing` and `:action_mailbox_incineration`, but `config.load_defaults` 6.1 and later set both to `nil`, so on a new 8.1 app `RoutingJob` and `IncinerationJob` use Active Job's `"default"` queue. Set `config.action_mailbox.queues.routing` if inbound mail should not compete with other work. ## Operating it in production The pipeline has three places where mail can stall, and each leaves a visible trace: - **The ingress refuses the request.** A wrong password gives `401`, a wrong ingress setting `404`, a missing password or broken storage `500`. Nothing is stored yet: the relay command reports a temporary failure so the mail server can retry, and providers apply their own retry rules; check the ingress logs. - **The routing job never runs.** Rows stay `pending`. The usual cause is a queue that no worker consumes, which is easy to miss if you moved `queues.routing` to a dedicated queue and forgot to give it a worker. - **The mailbox raises.** Rows end `failed` and the routing job fails with the original exception, so the queue backend's failed-jobs view and your error reports show it. A periodic check such as `ActionMailbox::InboundEmail.where(status: [:pending, :failed]).where(created_at: ...1.hour.ago).count` catches the second and third cases before users notice. Fix the cause and reroute the affected rows by setting them back to `pending` and calling `route_later`.
- Why does the ingress not call the mailbox directly?Routing runs in `ActionMailbox::RoutingJob`, enqueued by an `after_create_commit` on the new `InboundEmail`. The ingress only stores the message and answers, so the mail server or provider gets a quick success and your processing can be slow, retried or scaled on workers without making deliveries time out.
- What stops a redelivered message from creating two expenses?The `action_mailbox_inbound_emails` table has a unique index on `message_id` and `message_checksum`. A second copy with the same Message-ID and identical source raises `RecordNotUnique` inside `create_and_extract_message_id!`, which rescues it and returns `nil`, so no second record and no second routing job exist.
- All five ingress controllers accept mail at once; the config only sets a default.
- The ingress request waits for the mailbox's process method to finish.
- A redelivered message is processed twice unless the mailbox checks for it.
- When no route matches, the email stays pending until a route is added.
- On a new Rails 8.1 app, RoutingJob runs on the action_mailbox_routing queue.
Assumes Rails 8.1 with load_defaults 8.1: action_mailbox.queues.routing and .incineration are nil (since the 6.1 defaults), so both jobs use the "default" queue.