ActionMailer config and Environment Variables

I have set up a local_env.yml config file in my config directory.

And I have this in my applicaction.rb file

config.before_configuration do
  env_file = File.join(Rails.root, 'config', 'local_env.yml')
  YAML.load( do |key, value|
    ENV[key.to_s] = value
  end if File.exists?(env_file)

and in my yaml file I have

GOOGLE_USERNAME: 'googleusername'
GOOGLE_PASSWORD: 'googlepassword'

But when I have this in my actionmailer config

user_name: ENV["GOOGLE_USERNAME"],

I get authentication needed error when trying to send mail. If I replace the environment variables with the actual username and password it works.

If I do this in a view <%= ENV["GOOGLE_USERNAME"] %> It returns the correct information, so I know the files are set up correctly.

Does anyone know what this could be?


Hi Scott,

Can you clarify “I have this in my actionmailer config”?

Where exactly are you attempting to set those values? – we want to make sure that’s being done in a place where the before_configuration has already run.

Maybe try to do some logging/puts’ing from right where you are attempting to set, and be sure they are defined there?..

Hi @mjankowski

I have it in my environments/development.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: '',
  port: 587,
  domain: '',
  user_name: ENV["GMAIL_USERNAME"],
  password: ENV["GMAIL_PASSWORD"],
  authentication: 'plain',
  enable_starttls_auto: true

It looks like in your yaml config file you are setting values like GOOGLE_xxx, but then in your development.rb you are attempting to set from values like GMAIL_xxx

How stupid of me… Thank you @mjankowski