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]
The ActionMailer::MessageDelivery object is a wrapper around a Mail::Message . If you want to inspect, alter, or do anything else with the Mail::Message object you can access it with the message method on the ActionMailer::MessageDelivery object.
The default_url_options setting is useful for constructing link URLs in email templates. Usually, the :host , i.e. the fully qualified name of the web server, is needed to be set up with this config option. It has nothing to do with sending emails, it only configures displaying links in the emails.
You can put something like this in an initializer:
ActionMailer::Base.default_url_options = { :host => 'mysite.com' }
An alternative is use Figaro for environment variables:
# 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With