简体   繁体   中英

How can I set default_url_options per mailer?

In Rails 5, we have two mailers for some business reason.

Let's call them FooMailer and BarMailer .

The business requirement is to set up the default_url_options for each of them separately :

  • For FooMailer they should be { host: "wut.example.com" }
  • For BarMailer they should be { host: "blah.example.com" }

How can I do that?

For all mailers globally:

In config/environment/production.rb :

config.action_mailer.default_url_options = { :host => 'http://abc.co.uk' }

Per mailer:

For Rails 5.0.x, setting up the default_url_options as method on each mailer seems to be working:

class FooMailer < ApplicationMailer
  ...
  def default_url_options
    { host: "wut.example.com" }
  end
  ...
end

and

class BarMailer < ApplicationMailer
  ...
  def default_url_options
    { host: "blah.example.com" }
  end
  ...
end

A tip : if you want to still set up those options in environment-specific files (like you do with global defaults) you can use the Rails.applicaiton.config.x for that:

class FooMailer < ApplicationMailer
  ...
  def default_url_options
    Rails.application.config.x.default_foo_mailer_url_options ||
      raise('No x.default_foo_mailer_url_options config found')
  end
  ...
end

and then in config/environments/*.rb you can set it up like this:

Rails.application.configure do
  ...
  config.x.default_foo_mailer_url_options = { 
    host: "wut.example.com" 
  }
  ...
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM