Posted about 8 years ago. Visible to the public.

Make ActionMailer use the current request host and protocol for URL generation

When you generate a URL in a mailer view, ActionMailer will raise an error unless you previously configured it which hostname to use. Configuring the correct hostname is quite annoying when you have multiple deployment targets with different hostnames, e.g. a staging server and a production server.

Using the hack below you don't need to configure a default hostname for your mailers. They will always use the hostname for the current request:

class ApplicationController < ActionController::Base before_filter :make_action_mailer_use_request_host_and_protocol private def make_action_mailer_use_request_host_and_protocol ActionMailer::Base.default_url_options[:protocol] = request.protocol ActionMailer::Base.default_url_options[:host] = request.host_with_port end end

(If your application server talks only HTTP and relies on the web server for SSL, and if the web server is setting a special HTTPS forwarding header, Rails recognizes that a request originally was on HTTPS and will return the correct protocol.)

With ActiveJob

When you are using ActiveJob to deliver your emails, the above does not help. There is no request context when asynchronously delivering mails.

You need to set the host statically in the respective environment files:

# config/environments/production.rb config.action_mailer.default_url_options = { host: '' } # config/environments/staging.rb config.action_mailer.default_url_options = { host: '' } # config/application.rb as fallback/default config.action_mailer.default_url_options = { host: 'localhost' }

After you configure ActionMailer defaults like this, you no longer need the before_filter above.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Henning Koch
Last edit:
8 months ago
by Henning Koch
route, routing, router
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more