How do I configure the hostname for Rails ActionMailer?
Solution 1
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...')
Solution 2
You probably want to set :protocol => 'https' as well, btw.
config.action_mailer.default_url_options = {
:host => "portal.example.com",
:protocol => 'https'
}
Solution 3
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.
Mohsen Rashidi
Updated on July 09, 2022Comments
-
Mohsen Rashidi almost 2 years
I'm working on a fairly traditional forgot password email - I want to email the user a password change token embedded in a link that they can click on in order to change their password. I'm emailing via the traditional ActionMailer.
If I use a normal link_to tag
<%= link_to "click here", :controller => foo, :action => 'bar', :token => token %>
I get a relative link - rather useless from an email.
If I add in
:only_path => false
, then it errors saying I need to setdefault_url_options[:host]
. The ActionController docs imply that you do that by overriding the #default_url_options methods in your controller. Surely there's a configuration option to tell Rails what it's hostname is without adding my own config file, parsing it, etc?-
Sam Saffron over 14 years+1 for both the question and answer, this is so weird ... why is this setting on action mailer?
-
Pascal almost 13 yearsWhen sending an email, then there is no HTTP Request to take these values from.
-
-
John Douthat almost 15 yearsrequest isn't available from within a mailer
-
Mohsen Rashidi almost 15 yearsExactly. I could pass it as one of the parameters to the email generation - but we just decided to move this particular email to being sent from our Application Server instead of the web front-end. I think that means I need to make that custom config file that has the URL of the frontend website in it.
-
Rob Di Marco almost 15 yearsdoh, missed the mailer part of it
-
Mohsen Rashidi about 14 yearsThat's a good tip, though I'm afraid I need the static configuration. For some reason, I keep running into more and more processing that needs to happen outside of a web hit.
-
Kevin over 11 yearsThanks for the :protocol => 'https' pointer. using 'portal.example.com' does not seem to work properly, at least with Devise mailers. The resulting urls begin with 'example.com' regardless of the subdomain).
-
riley chen about 11 yearscurrent link. pivotallabs.com/…
-
engineerDave almost 10 yearsnot you have a "hanging" comma in your default_url_options hash. I would edit it but its a 1 char edit which is verboten on SO.
-
Noah Passalacqua over 7 yearsI feel like im losing my mind. This is not working for me