loading the initializer before the environment - ruby-on-rails

I have a Rails application and I put all my important configurations, e.g. sendgrid, new relic, twilio, airbrake, etc, in a config/config.yml file. The file looks as follows:
development:
sendgrid:
username: username
password: password
test:
sendgrid:
username: username
password: password
production:
sendgrid:
username: username
password: password
Then in config/initializers/global_configuration.rb, I load the correct environment configuration:
APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
Now I want to be able to access this global constant in config/environments/development or config/environments/production, as so:
config.action_mailer.smtp_settings = {
address: 'smtp.sendgrid.net',
port: 587,
domain: APP_CONFIG['sendgrid']['domain'],
authentication: "plain",
enable_starttls_auto: true,
user_name: APP_CONFIG['sendgrid']['username'],
password: APP_CONFIG['sendgrid']['password']
}
Unfortunately, when Rails starts up, it throws the following error:
Uncaught exception: uninitialized constant APP_CONFIG
It appears that config/environments is loaded before config/initializers. How can I get around this so I can access my global constant in config/environments?

It appears config/application.rb is loaded before the config/environments/*.rb files, so I was able to hook into the before_configuration block and then create a global variable within it:
config.before_configuration do
::APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
end
If there is a better option (rather than using ENV), I will gladly delete this answer and upvote the better one.

Do this in all the environment file inside config/environment/
config.after_initialize do
config.action_mailer.smtp_settings = {
address: 'smtp.sendgrid.net',
port: 587,
domain: APP_CONFIG['sendgrid']['domain'],
authentication: "plain",
enable_starttls_auto: true,
user_name: APP_CONFIG['sendgrid']['username'],
password: APP_CONFIG['sendgrid']['password']
}
end

Related

email confirmation using devise

I follow the instruction in vs code after i run rails g devise:install
I added config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } in development.rb
I also uncomment the confirmable in migration file .
I also added confirmable in user.rb
In environment.rb this is what i have
# Load the Rails application.
require_relative "application"
# Initialize the Rails application.
Rails.application.initialize!
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
address: 'smtp.gmail.com',
port: 465,
domain: 'gmail.com',
user_name: ENV['test#gmail.com'],
password: ENV['123456'],
authentication: 'plain',
:ssl =>true,
:tsl => true,
enable_starttls_auto: true
}
Im not sure on how to put the correct username and password .
Im not getting any error message when i go to signup but when i go to the email that i use to sign up , im not getting any email .
I also enable less secure app in google settings for both email .
Please help
Thank you
The modern Rails approach (5.1+) is to use encrypted secrets. When you run rails credentials:edit Rails will open a basic credentials file in your editor of choice. You can then just add your Gmail password and email to the file:
gmail:
password: your_password_goes_here
user_name: your-email-address#gmail.com
Rails will then create the config/credentials.yml.enc file that can be checked into version control. The file is encrypted with your master.key file which should not be checked into version control.
You can then access the encrypted secrets with Rails.application.credentials:
ActionMailer::Base.smtp_settings = {
address: 'smtp.gmail.com',
port: 465,
domain: 'gmail.com',
user_name: Rails.application.credentials.gmail.fetch(:user_name),
password: Rails.application.credentials.gmail.fetch(:password),
authentication: 'plain',
ssl: true,
tsl: true,
enable_starttls_auto: true
}
Firstly you need to install a gem such as dotenv-rails.
This will allow you to use environment variables.
To do so create a .env file in the root of your project.
Inside of that file you can add environment variables such as:
EMAIL_USERNAME=someusername
EMAIL_PASSWORD=somepassword
And then in your environment.rb you can use it like this:
user_name: ENV['EMAIL_USERNAME'],
password: ENV['EMAIL_PASSWORD']

Net::SMTPAuthenticationError: 530-5.7.0 Authentication Required Rails Mailer using environmental variable on config

I'm using Rails Mailer to send confirmation mails on my app, and I've set up my config>environment>development.rb like below:
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: 'smtp.gmail.com',
# user_name: ENV["MAIL_USERNAME"],
# password: ENV["MAIL_PASSWORD"],
user_name: "myemail#mydomain.com",
password: "myCorrectPassword",
authentication: :login,
enable_starttls_auto: true
}
Problem
When I hardcode the user_name and the password for "myemail#mydomain.com" and "myCorrectPassword", the mailer sends an email and it works fine. But when I use the ENV to make sure that my credentials are safe, I get the Net::SMTPAuthenticationError: 530-5.7.0 Authentication Required error.
On Terminal, I checked my ENV to make sure that I've entered correct values for each variable, and they were right.
What else can I try?
What do you get when you run this in rails console ENV["MAIL_USERNAME"] ??
Does your ~/.bash_profile has this
export MAIL_USERNAME="myemail#mydomain.com"
Try to source ~/.bash_profile once

using figaro to store env variables

I'm confused about how to properly use figaro with Rails 4.2. So in application.yml (which is checked into .gitignore), I have this:
secret_key_base: 123456
And then in secrets.yml, I have this:
development:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
The gem should be handling the ENV part of setting the credentials, according to everything I've read. Why is this not working?
EDIT 1:
In application.yml I have: mandrill_user_name: email#example.com and mandrill_password: 1234567890
And in development.rb I have:
config.action_mailer.default_url_options = { :host => "localhost:3000" }
config.action_mailer.smtp_settings = {
address: "smtp.mandrillapp.com",
port: 587,
domain: "localhost:3000",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["mandrill_user_name"],
password: ENV["mandrill_password"]
}
Shouldn't application.yml be taking care of this?
If is redundant to have the variable in the secrets.yml file and the application.yml file.
I.e. use the application.yml file ONLY to declare ENV Vars.
So long as it is in the apllication.yml file you can call it throughout your rails app just like you are doing:
ENV["SECRET_KEY_BASE"]
Varialbles stored in the secrets.yml file are called via
Rails.application.secrets.SECRET_KEY_BASE

Error on environment.rb file under config (Ruby on rails)

Below is my environment.rb file
require File.expand_path('../application', __FILE__)
Wiyo::Application.initialize!
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: 'atomambition#gmail.com',
password: '********'
}
I have put my gmail password in ** section. But when i do rake:db migrate, it shows error on environment.rb file telling me
7:in '>'.
Please help me. I am creating mailing function in my rails app. I am stuck.
This configuration code should not be included in environments.rb. The config global is localized to the environment specific configuration files found within config/environments. Try adding this code to the appropriate file there:
# config/environment/development.rb
Wiyo:::Application.configure do
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: 'atomambition#gmail.com',
password: '********'
}
# remainder of configuration
end

Rails mailer Net::SMTPAuthenticationError (530-5.5.1 Authentication Required) with Gmail using ENV[USERNAME] and ENV[PASSWORD]

I'm getting an error when sending out mail from the Production environment, specifically:
Net::SMTPAuthenticationError (530-5.5.1 Authentication Required)
On my local machine and development environment I'm declaring the passwords using ENV[GMAIL_USERNAME] and ENV[GMAIL_PASSWORD] and it's working fine, where the declared ENV is stored in my .bash_profile
# Development Environment for Action Mailer config
config.action_mailer.default_url_options = { host: '0.0.0.0:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.default charset: 'utf-8'
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'mydomain.com',
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
# Production Environment for Action Mailer config
config.action_mailer.default_url_options = { host: 'mydomain.com' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default charset: 'utf-8'
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "mydomain.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
Unfortunately, in Production I get an error when sending mail. However, if I hardcode the username and password in environments/production.rb it works. The ENV["GMAIL_USERNAME"] and `ENV["GMAIL_PASSWORD"] is set in the .bash_profile:
export GMAIL_USERNAME="hello#mydomain.com"
export GMAIL_PASSWORD="mypassword"
I thought it would it was the app unable to call ENV["GMAIL_USERNAME"] and ENV["GMAIL_PASSWORD"] but when I jump into the production rails console and puts ENV["GMAIL_USERNAME"] it outputs the correct credentials.
I have restarted Apache and restarted my app multiple times but I'm puzzled what to do next.
Any help is much appreciated.
Thank you in advanced.
Whose bash profile? Your users? Apache won't read those profiles on startup as it doesn't run bash to start the server.. Your profile gets sourced when you login to the server. Which is why it works when you test.
You need to make sure that environment variables are defined in the script or the environment that apache runs under. Depending upon your server this could be in /etc/apache2/envvars or added to the startup script for apache.

Resources