How to set Action Mailer defaults after config/initializers run?
I want to store email account information in a config.yml file. I'm loading that information into a constant in a Rails config/initializers file following a common pattern I've seen online and outlined at this RailsCast. I'm trying to setup defaults for Action Mailer using config.action_mailer.smtp_settings inside the config/application.rb file, following an example on Mat Harvard's Blog. I keep getting uninitialized constant errors when starting my rails server. I'm assuming that application.rb is being called before the config/initializers. Is there another location where I can set the config.action_mailer.smtp_settings during startup, but after the config/initializers run?
Update: I may not have been clear in my initial post/question. I'm reading the config.yml file in an initializer. This config file stores email account information such as username and password. I don't want to put this information (username and password) in either the application.rb or environment.rb files. I did try moving my code to the environment.rb file, but encountered the same uninitialized constant error when starting rails.
My code to set the action mailer settings looks like this:
config.action_mailer.smtp_settings = {
:address => APP_CONFIG[:email_config][:address],
:port => APP_CONFIG[:email_config][:port],
:domain => APP_CONFIG[:email_config][:email_domain],
:user_name => APP_CONFIG[:email_config][:user_name],
:password => APP_CONFIG[:email_config][:password],
:authentication => :plain,
:enable_starttls_auto => true
}
config.action_mailer.default_url_options = {
:host => APP_CONFIG[:email_config][:host]
}
I'm reading from the config.yml file to set the APP_CONFIG constant in a load_config.rb initializer. That file contains the 2 lines below:
raw_config = File.read(RAILS_ROOT + "/config/config.yml")
APP_CONFIG = YAML.load(raw_config)[RAILS_ENV]
你可以在初始化器中加入这样的东西:
ActionMailer::Base.default_url_options = { :host => 'mysite.com' }
An initializer probably isn't the right place for this information, at least not in Rails 3. Following the guidelines in the official ActionMailer guide, I would put the information in the environment file -- I presume the definition of the constants will change depending on what environment you're in, and this will have the same effect. So in config/environments/production.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'baci.lindsaar.net',
:user_name => '<username>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
Shamelessly stolen from the guide I just referenced.
另一种方法是将Figaro用于环境变量:
# config/initializers/smtp_config.rb
Rails.application.configure do
ActionMailer::Base.smtp_settings = { address: Figaro.env.smtp_address,
port: (Figaro.env.smtp_port || 587),
domain: Figaro.env.smtp_domain,
user_name: Figaro.env.smtp_user_name,
password: Figaro.env.smtp_password,
authentication: Figaro.env.smtp_authentication,
enable_starttls_auto: Figaro.env.smtp_enable_starttls_auto,
openssl_verify_mode: Figaro.env.smtp_openssl_verify_mode,
ssl: Figaro.env.smtp_ssl,
tls: Figaro.env.smtp_tls }
end
链接地址: http://www.djcxy.com/p/57794.html