How do I configure the hostname for Rails ActionMailer? How do I configure the hostname for Rails ActionMailer? ruby-on-rails ruby-on-rails

How do I configure the hostname for Rails ActionMailer?


default_url_options is available from config.action_mailer and should be set in your environment's configuration file.

For example, in config/environments/production.rb:

config.action_mailer.default_url_options = {  :host => 'www.yourdomain.com'}

For local testing, modify config/environments/development.rb:

config.action_mailer.default_url_options = {  :host => '127.0.0.1',  :port => 3000}

Then, assuming you have a named route called forgot_password_login, you can generate the login link URL in your mailer using something like this:

forgot_password_login_url(:token => 'a7s8q15sk2...')


You probably want to set :protocol => 'https' as well, btw.

config.action_mailer.default_url_options = {     :host => "portal.example.com",     :protocol => 'https' }


There is another alternative, as described in http://pivotallabs.com/how-i-leaned-to-stop-hating-and-love-action-mailer/

This solution has the advantage that it doesn't require any configuration (so less of a hassle), and works fine as long as you send emails from within controllers.

But if you plan on sending email without going through a controller (e.g. from command line or in response to another email), you need the static configuration.