How to set Action Mailer by default after running config / initializers?

I want to save email account information in config.yml file. I load this information into a constant in the Rails configuration / initialization file, following the general pattern that I saw on the Internet, and outlined in this RailsCast . I am trying to set default values ​​for Action Mailer using config.action_mailer.smtp_settings inside the config / application.rb file, following the example of Mat Harvard Blog . I keep getting uninitialized persistent errors when starting my rails server. I assume application.rb is called before the configuration / initializers. Is there any other place where I can set config.action_mailer.smtp_settings during startup, but after running configuration / initializers?

Update: Perhaps I was not clear from my original question / question. I am reading the config.yml file in the initializer. This configuration file stores email account information such as username and password. I do not want to put this information (username and password) in application.rb or environment.rb files. I tried to move my code to the environment.rb file, but ran into the same uninitialized persistent error when starting the rails.

My code for setting up the mail program is as follows:

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 am reading from the config.yml file to set the APP_CONFIG constant in the load_config.rb initializer. This file contains two lines below:

 raw_config = File.read(RAILS_ROOT + "/config/config.yml") APP_CONFIG = YAML.load(raw_config)[RAILS_ENV] 
+6
source share
3 answers

You can put something like this in the initializer:

 ActionMailer::Base.default_url_options = { :host => 'mysite.com' } 
+4
source

The initializer is probably not suitable for this information, at least not in Rails 3. Following the recommendations in the official ActionMailer manual , I would put the information in an environment file - I assume that the definition of constants will change depending on which environment you are in stay and it will have the same effect. Therefore, in config / environment / 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 manual I just referred to.

0
source

An alternative is to 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 
0
source

Source: https://habr.com/ru/post/909153/


All Articles