skip to content

How do Action Mailer's delivery_method, interceptors and preview settings keep a Rails staging server from emailing real furniture-store customers?

level: seniorimportance: should knowfreq 32%

answer

  1. :smtp default, :test in test.rb
  2. perform_deliveries and raise_delivery_errors
  3. delivering_email(message) before handoff
  4. previews: development only by default
  5. bang deliveries skip the checks

basics

~10 s

Pick the delivery_method per environment, and register an interceptor whose delivering_email(message) rewrites recipients before every send on staging. Keep previews development-only, because show_previews defaults to development and staging previews render real records.

solid answer

~40 s

`delivery_method` defaults to `:smtp`; the built-ins are `:smtp`, `:sendmail`, `:file` and `:test`, and the generated `test.rb` uses `:test`, which collects mail in `ActionMailer::Base.deliveries`. A staging server that should exercise the real pipeline keeps SMTP but adds an **interceptor**: a class with `self.delivering_email(message)` registered through `config.action_mailer.interceptors` or `ActionMailer::Base.register_interceptor`, which rewrites `message.to` to a team inbox before the message reaches the delivery method. Observers (`delivered_email`) only see mail afterwards. `perform_deliveries = false` switches sending off entirely, but `deliver_now!` and `deliver_later!` bypass that check. Previews are routed only when `show_previews` is true, which defaults to development; enabling them on staging serves rendered emails built from real data.

code

ruby · 7 lines
ruby
# config/environments/staging.rb
require_relative "production"

Rails.application.configure do
  config.action_mailer.default_url_options = { host: "staging.shop.example" }
  config.action_mailer.interceptors = %w[StagingRecipientInterceptor]
end

go deeper

for a junior

Know that test.rb uses the :test delivery method and that development previews live at /rails/mailers.

for a middle

List the built-in delivery methods, explain perform_deliveries and raise_delivery_errors, and write an interceptor with delivering_email.

for a senior

Design staging safety with an interceptor that rewrites to, cc and bcc, keep previews off or authenticated, and audit bang deliveries.

for a principal

Decide how non-production environments handle real customer data and outbound email as one policy, not a per-app afterthought.

## The levers A staging server runs production code against production-like data, often a copy of real customer records. Three groups of Action Mailer settings decide whether an order confirmation leaves the building: | Setting | Default | What it controls | |---|---|---| | `delivery_method` | `:smtp` | which delivery agent receives the message | | `perform_deliveries` | `true` | whether `deliver_now` hands the message to the agent at all | | `raise_delivery_errors` | `true` | whether a failed send raises; generated `development.rb` sets it to `false` | | `interceptors` | none | classes that may change each message just before delivery | | `show_previews` | `true` only in development | whether `/rails/mailers` is routed | ## Delivery methods The built-in delivery methods are: - **`:smtp`**, the default, configured with `smtp_settings` (the generated `production.rb` shows a commented example reading credentials). - **`:sendmail`**, piping to the local sendmail binary, `/usr/sbin/sendmail -i` by default. - **`:file`**, writing each message under `tmp/mails` in a Rails app. - **`:test`**, which stores messages in `ActionMailer::Base.deliveries`; the generated `test.rb` sets it so tests never send real mail. - **Custom agents**, registered with `add_delivery_method :name, SomeClass, default_options`. Switching staging to `:file` or `:test` is the bluntest safety net, but it also stops the team from seeing real emails arrive. Most shops keep SMTP on staging and redirect recipients instead. ## Interceptors and observers An **interceptor** is any class or object with a `delivering_email(message)` method. Action Mailer passes it the `Mail::Message` just before handing it to the delivery agent, and the interceptor mutates it in place: ```ruby class StagingRecipientInterceptor def self.delivering_email(message) message.subject = "[staging to #{Array(message.to).join(", ")}] #{message.subject}" message.to = ["[email protected]"] message.cc = nil message.bcc = nil end end ``` Register it only where it belongs: 1. In `config/environments/staging.rb`: `config.action_mailer.interceptors = %w[StagingRecipientInterceptor]`. 2. Or in an initializer: `ActionMailer::Base.register_interceptor(StagingRecipientInterceptor) if Rails.env.staging?`. Because it runs at delivery, it covers `deliver_now`, mail sent from a `deliver_later` job and mail from any mailer in the app, including generated ones such as `PasswordsMailer`. Clearing `cc` and `bcc` matters: rewriting only `to` still leaks copies. An **observer**, registered through `observers` or `register_observer`, implements `delivered_email(message)` and runs **after** sending, so it can log but not prevent anything. ## Switches that are bypassed `perform_deliveries = false` turns sending into a no-op for `deliver_now` and `deliver_later`. The bang variants, `deliver_now!` and `deliver_later!`, deliver **without checking** `perform_deliveries` and `raise_delivery_errors`. A code path that uses them will send even on a server where you switched delivery off, which is one reason an interceptor that rewrites recipients is the safer guard. ## Previews on staging Mailer previews live in `test/mailers/previews` (more directories can be added to `config.action_mailer.preview_paths`) and render at `/rails/mailers`. The routes are added only when `show_previews` is true, and its default is `Rails.env.development?`. Setting `config.action_mailer.show_previews = true` on staging also lifts the controller's local-request check, so anyone who reaches the URL can browse emails rendered from whatever records the previews load, often real orders. If you need previews there, put them behind authentication and make the preview methods use fixtures or fake data. Previews do not run delivery interceptors. They have their own hook, **preview interceptors** with a `previewing_email(message)` method, registered through `config.action_mailer.preview_interceptors` or `register_preview_interceptor`; an interceptor that should affect both must be registered both ways. ## A staging checklist 1. Give staging its own environment file and its own `default_url_options` host. 2. Register a recipient-rewriting interceptor there, clearing `cc` and `bcc` as well as rewriting `to`. 3. Search the codebase for `deliver_now!` and `deliver_later!`, and justify each one. 4. Leave `show_previews` at its default, or protect `/rails/mailers` and make previews load fake data. 5. Send one real email through staging after each deploy to prove the SMTP settings work end to end. None of these replaces the others: delivery method and interceptors decide where mail goes, previews decide who can read rendered mail, and the bang methods are the hole in the `perform_deliveries` switch.

  • What is the difference between an interceptor and an after_deliver callback?
    An interceptor is registered globally and receives every `Mail::Message` before it is handed to the delivery agent, whatever mailer built it. `after_deliver` is a callback on one mailer class that runs after delivery with access to the mailer instance and its params, which suits logging or recording that a specific email was sent.
  • Why is raise_delivery_errors false in the generated development.rb?
    Development usually has no mail server, and the default delivery method is still `:smtp`, so sends would fail on every action that emails. The generated config sets `raise_delivery_errors = false` so those failures do not break the page; previews are the intended way to look at emails locally.

  • An observer's delivered_email can stop an email from being sent.
  • perform_deliveries = false also stops deliver_now! and deliver_later!.
  • A delivery interceptor also changes what /rails/mailers previews show.
  • Enabling show_previews on staging is harmless because previews never send.
  • Rewriting message.to alone is enough to keep copies away from customers.