Skip to content

Configure email (default url host) on Heroku

Patrick Bolger edited this page Jul 26, 2017 · 5 revisions

Any internal link in a mail template needs to have a full URL specified (as opposed to a relative URL). This is explained by the comment in this snippet of code from config/environments/production.rb:

  # Mail templates will need to use "_url" helpers rather than "_path" helpers
  # since the template will not have the context of a request
  # (as a controller does) and thus the full URL will be required to create
  # links in the email.  This setting defines the host (domain) for the URL.

  config.action_mailer.default_url_options = { :host => ENV['DEFAULT_HOST'] ||
                                                        'hitta.sverigeshundforetagare.se'}

Since Heroku runs as a "production" environment, the setting above determines the host portion of the URL that is constructed for email links. For example, this statement in views/devise/mailer/reset_password_instructions.html.haml:

%p= link_to t('devise.mailer.reset_password_instructions.change_password'),
            edit_password_url(@resource, reset_password_token: @token)

To make this work in Heroku, we need to add the variable DEFAULT_HOST to "Config Vars" in the settings for a Heroku app. This should be set to the top-level-domain for the app.

For instance, our current "staging" Heroku app is called shf-project, hence this var should be defined as: shf-project.herokuapp.com.

If you have a personal Heroku app for testing SHF, DEFAULT_HOST should be defined for that. For example, my personal Heroku SHF app is called shf-test. Hence, DEFAULT_HOST is defined to be shf-test.herokuapp.com.

Clone this wiki locally