Skip to content

Navigation Menu

Sign up

How To: Set :host and :port for all devise mailer urls

brendon edited this page Feb 2, 2013 · 3 revisions

If you find yourself needing to set a :host and :port for your mailer url's (e.g. when sending a reset password link) follow the instructions for overriding the devise mailer and then follow these steps:

Create a file in app/concerns called default_url_options.rb. Depending on your version of Rails you might need to add a setting to your environment that loads files from this folder automatically.

In this file put something like the following:

module DefaultUrlOptions
 
 # Including this file sets the default url options. This is useful for mailers or background jobs
 
 def default_url_options
 {
 :host => host,
 :port => port
 }
 end
 
private
 def host
 # Your logic for figuring out what the hostname should be
 end
 
 def port
 # Your logic for figuring out what the port should be
 end
 
end

Next, just include this module in your devise mailer:

class UserMailer < Devise::Mailer 
 include DefaultUrlOptions
end

It's worthwhile noting that you'll need to be able to figure out the host and port in the above example without having access to any request object as mailers and background jobs run outside the context of the request.

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /