Ruby on Rails

Action Mailer talks to a sandbox with one smtp_settings block per environment.

Before you start

  • Rails 6 or later

1Configure Action Mailer

config/environments/development.rb
# config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
  address: 'smtp.mailclutch.com',
  port: 587,
  user_name: 'mc_0000000000000000',
  password: 'pw_0000000000000000',
  authentication: :plain,
  enable_starttls_auto: true
}

raise_delivery_errors is worth turning on here: without it a refused connection is swallowed and the inbox simply stays empty.

2Send a test message

From rails console:

ActionMailer::Base.mail(
  from: 'staging@example.com',
  to: 'anyone@example.com',
  subject: 'Sandbox check',
  body: 'Hello from Rails.'
).deliver_now

Watch it land

Open the sandbox and the message is there — rendered HTML, plain text, raw source, attachments, and the SPF, DKIM and DMARC verdicts. It updates live, so you can leave it open next to your terminal.

Nothing is ever delivered onward, whatever address you send to; claim the inbox with an account and it stays.

Awaiting your email

Send from any address; it stops here rather than going onward. The inbox keeps what it catches for 24 hours.

What usually goes wrong

  • deliver_later needs a worker

    deliver_later hands the mail to Active Job. Either run the queue backend, or use deliver_now while you are testing.

  • letter_opener and friends

    A gem that sets delivery_method to :letter_opener or :file in development overrides the block above. Comment it out for the environment you are pointing here.

  • Credentials belong in ENV

    The snippet inlines the pair so you can paste it as is. For anything you commit, read it from ENV or Rails credentials instead.

Official documentationRails — Action Mailer Basics (opens in a new tab)