5

I need some advice on configuring mail in production Ruby-on-Rails sites.

I deploy my Rails app on EngineYard. I have a couple of sites, like demo.mydomain.com or staging.mydomain.com - how can I configure Devise so that at deploy time I can make sure confirmation mails come from demo.mydomain.com or staging.mydomain.com automatically? ie, I want the same GitHub codebase, and want to fill the configuration in dynamically.

Currently in config/environments/production.rb I have the line:

config.action_mailer.default_url_options = { :host => 'demo.mydomain.com' }

But that's incorrect when the same code is deployed to staging.mydomain.com as they both run in RAILS_ENV=production

Any ideas?

Thanks, Dave

Update: For now, to be practical, I've added specific environments to hardcode the mailer domain. So now demo.mydomain.com runs on environments/demo.rb, and www.mydomain.com runs on environments/productions.rb. What I don't like about this is the duplication between the files, it's not clear to me how to DRY them up as I have with, eg, database.yml

David Kennedy
  • 333
  • 1
  • 3
  • 13

3 Answers3

4

in your devise configuration, usually config/initializers/devise.rb you can configure the mail-sender for devise. this configuration takes a proc, so that it's possible to evaluate something at runtime.

Devise.setup do |config|
  config.mailer_sender = Proc.new { your_magic_here }
end
phoet
  • 18,688
  • 4
  • 46
  • 74
  • Thanks, I'll have a play on the production box and see if I can figure out the domain - but as far as I can tell, the domain name isn't "visible" on the running box. After all the app could be running on a couple of domains, all handled at DNS level. – David Kennedy Feb 01 '12 at 11:28
  • exactly. that's why we setup a global object in each request that holds configurations on a domain basis. this object is then accessed in the proc. the object is similar to what is done in the I18n gem. – phoet Feb 01 '12 at 14:47
1

First of all, I think you should separate the environments of your application. Check this guide to learn how you can do it.

Then, try something like this in your devise configuration:

Devise.setup do |config|
  if Rails.env.production?
    config.mailer_sender = "no-reply@domain.com"
  elsif Rails.env.staging?
    config.mailer_sender = "no-reply@staging.domain.com"
  else
    config.mailer_sender = "no-reply@domain.com"
  end
  ...

Check this guide to understand more about Proc object.

1

Ideally staging & production servers should run on different rails environment. Still if you wanted to have production env running on both staging & production servers with different action mailer urls then it should be done at deployment level. You can always write environment file while deployment.

Sandip Ransing
  • 7,583
  • 4
  • 37
  • 48
  • Could you be more specific about how I could write an environment file at deployment time? That's the trick I'm missing. – David Kennedy Feb 01 '12 at 11:27
  • well. specify host as HOST_URL inside environment file and in your deployment recipe grep file for HOST_URL and replace it wth url you wanted to have based on server(staging,production) to be deployed. `config.action_mailer.default_url_options = { :host => 'HOST_URL' }` – Sandip Ransing Feb 01 '12 at 11:36
  • Ah, I see. In this case I don't have an explicit recipe to work with ('ey deploy' is how we work), but otherwise that's the tactic I'd have used in other frameworks. Thanks. – David Kennedy Feb 01 '12 at 15:43