skip to content

In an Action Mailbox mailbox, how do before_processing, bounce_with and bounced! reject a forwarded receipt from an unknown sender?

level: seniorimportance: should knowfreq 20%

answer

  1. callbacks halt once delivered or bounced
  2. bounced! changes status, sends nothing
  3. bounce_with: bounced! plus deliver_later
  4. bounce_now_with delivers immediately
  5. failed is set before rescue_from runs

basics

~10 s

Check the sender in a before_processing callback; calling bounce_with(SomeMailer.action(inbound_email)) marks the email bounced, enqueues the reply and halts the chain so process never runs. bounced! halts the same way without sending anything.

solid answer

~30 s

Mailboxes have `before_processing`, `after_processing` and `around_processing` callbacks. After each one, the chain checks whether the `InboundEmail` is already `delivered` or `bounced`; if so it stops, so `process` and the after callbacks never run. `bounced!` only sets the status, a silent rejection. `bounce_with(message)` calls `bounced!` and then `message.deliver_later`, where `message` is an Action Mailer delivery such as `ReceiptsMailer.unknown_sender(inbound_email)`; `bounce_now_with` uses `deliver_now`. Inside `process` itself these calls do not stop execution, so return after them. An exception from a callback or `process` marks the email `failed` before the mailbox's `rescue_from` handlers run; a handler can call `bounced!` to record a rejection instead.

go deeper

for a junior

Know that before_processing checks preconditions and that bounce_with rejects an email with a reply.

for a middle

Explain the terminator that halts on delivered or bounced, and the difference between bounced!, bounce_with and bounce_now_with.

for a senior

Handle exceptions with rescue_from deliberately, return after bouncing inside process, and avoid auto-replies to forged senders.

for a principal

Set a rejection policy across inbound channels, silent versus replied, balancing user feedback against backscatter and abuse handling.

## The processing callbacks A mailbox's work happens in `process`, wrapped by three callback macros from `ActionMailbox::Callbacks`: - **`before_processing`**: preconditions, such as "the sender is a known user" or "the message has an attachment"; - **`around_processing`**: wrapping, such as timing or a transaction; - **`after_processing`**: follow-up work once `process` has run. The chain is defined with a custom **terminator**. After each before callback runs, Rails asks the mailbox `finished_processing?`, which is true when the `InboundEmail` is `delivered` or `bounced`. A true answer halts the chain: `process` is skipped and so are the after callbacks. That is why a precondition check can simply change the status and return. ## Three ways to reject | Call | Status | Sends a reply? | |---|---|---| | `bounced!` | `bounced` | no, a silent rejection | | `bounce_with(message)` | `bounced` | yes, `message.deliver_later` | | `bounce_now_with(message)` | `bounced` | yes, `message.deliver_now` | `bounced!` is the enum bang method on `InboundEmail`, delegated by the mailbox. `bounce_with` and `bounce_now_with` take an Action Mailer **delivery object**, the return value of calling a mailer action without delivering it. Passing `inbound_email` to that mailer lets the reply quote the original subject or address. ```ruby class ReceiptsMailbox < ApplicationMailbox before_processing :require_known_sender before_processing :require_attachment def process sender.expenses.create!(note: mail.subject, source_message_id: inbound_email.message_id) end private def sender @sender ||= User.find_by(email_address: mail.from_address&.address) end def require_known_sender bounce_with ReceiptsMailer.unknown_sender(inbound_email) unless sender end def require_attachment bounced! if mail.attachments.none? end end ``` Here an unknown sender gets a polite reply and the second callback never runs; a known sender who forgot the attachment is rejected silently. ## Traps 1. **Inside `process`, nothing halts.** The terminator only acts between callbacks. Calling `bounce_with` halfway through `process` sets the status and sends the reply, but the rest of the method still runs unless you `return`. 2. **Exceptions mark the email `failed` first.** Rails wraps processing so that any exception sets the status to `failed` and is re-raised; then the mailbox's `rescue_from` handlers get a chance. A handler such as `rescue_from(ActiveRecord::RecordInvalid) { bounced! }` turns that into a recorded rejection; with no handler, the exception propagates and the routing job fails. 3. **The reply is an ordinary outbound email.** `bounce_with` enqueues it like any `deliver_later`, so it needs working mail delivery and a queue; building that mailer is outbound work. 4. **Bouncing every stranger can spray mail at forged senders.** Spam often forges the From address, so automatic replies to unknown senders can land on innocent people. Many apps reply only when the sender looks legitimate and use silent `bounced!` otherwise. ## Reading sender addresses Rails extends `Mail::Message` with address helpers that return `Mail::Address` objects: `from_address` and, **new in Rails 8.1**, `reply_to_address`. `mail.from` returns an array of strings, so `mail.from_address&.address` is the tidy way to get one sender string. When a user forwards from a shared inbox that sets Reply-To, `reply_to_address` is the better identity check. ## Statuses after the dust settles If no callback bounced and `process` returns normally, Rails sets `delivered`. A bounce leaves `bounced` in place. Both, like `failed`, count as processed and schedule incineration. ## Choosing silent or replied rejections | Situation | Typical choice | |---|---| | Known user, fixable mistake (no attachment) | `bounce_with` a helpful reply | | Unknown sender, plausible human | `bounce_with` once, or silent | | Obvious spam or forged sender | `bounced!` silently | | Temporary failure in your own code | let it raise, fix, then reroute | The last row matters: a bounce is a **final** decision recorded on the `InboundEmail`, while a failure is a bug report. Turning every exception into `bounced!` hides defects and tells users their mail was rejected when the app was at fault. ## Where the reply mailer fits The mailer passed to `bounce_with` is ordinary Action Mailer code: an action such as `ReceiptsMailer.unknown_sender(inbound_email)` that reads `inbound_email.mail.from_address` and `subject` to address and word the reply. Because `bounce_with` calls `deliver_later`, the `InboundEmail` travels to the mail job as a Global ID; it still exists then, since incineration is weeks away. Keep the reply short, never quote the full original body back, and set a sender address that does not route into the same mailbox, or a reply loop can bounce mail back and forth.

  • When would you use bounce_now_with instead of bounce_with?
    `bounce_now_with` calls `deliver_now`, so the reply is sent inside the routing job instead of a second job. That suits apps whose routing job already runs in the background and where an extra enqueue adds nothing; `bounce_with` keeps the reply's delivery retries separate from routing.
  • How do you make a validation error a rejection instead of a failure?
    Declare `rescue_from(ActiveRecord::RecordInvalid) { bounced! }` on the mailbox. The exception first marks the email `failed`, then the handler runs and sets `bounced`, and because the handler dealt with it the exception is not re-raised, so the routing job succeeds.

  • bounced! sends an automatic bounce email to the sender.
  • Calling bounce_with inside process stops the rest of process from running.
  • bounce_with takes the mailer class and action name as symbols.
  • A rescue_from handler prevents the email from ever being marked failed.
  • After a bounce in before_processing, after_processing callbacks still run.

Assumes Rails 8.1: Mail::Message#reply_to_address was added to Action Mailbox's mail extensions in 8.1.0; the callback, bounce and status behaviour is unchanged across 7.1-8.1.