Identifying environmental variables in a rails app - ruby-on-rails

I have inherited a Rails app. My production.rb file has the following:
ActionMailer::Base.smtp_settings = {
port: 587,
address: "smtp.sendgrid.net",
user_name: ENV['NF_SENDGRID_USERNAME'],
password: ENV['NF_SENDGRID_PASSWORD'],
domain: ENV['NF_SENDGRID_DOMAIN'],
authentication: :plain,
This app is not using the figaro gem, and I have logged in to the server (Ubuntu) and issued printevn for both the deployer and root users and don't see these variables. How can I uncover these from the production environment?

Figured this out. These were configured in deploy folder on the server in /current/.rbenv-vars.

Related

Rails 6 ArgumentError (SMTP-AUTH requested but missing user name)

I built a basic rails app only for contact form using - gem 'mail_form'. It works perfectly in the development environment. However, I deploy the app in Heroku and only got an error in the production environment.
config/environments/production.rb
config.action_mailer_default_url_options = { host: "https://myapp.herokuapp.com" }
# Rails.application.routes.default_url_options[:host] = 'https://gmail.com'
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
authentication: 'plain',
enable_starttls_auto: true,
user_name: ENV['GMAIL_EMAIL'],
password: ENV['GMAIL_PASSWORD']
}
enter image description here
Any helps would be appreciated.
There is no issue in your configuration, but fact is on Heroku they have following information
Gmail have protections in place to avoid their service being used to deliver spam, however these are not published for security reasons. However we often see issues where customer applications attempt to use a Gmail account for email delivery which is subsequently blocked. So it mean there is some sort of block.
You are suggested to use email add-ons from email add-ons

Ruby on Rails 4 with SendGrid - authentication fails , but only on production server on Linode

In my application I have set up sendgrid SMTP service. I am using Figaro gem for setting ENV variables for SMTP configuration.
Everything is fine on development mode - I can send e-mails. But when I deploy to my VPS on Linode, every time when I try to send mail I get:
Net::SMTPAuthenticationError (535 Authentication failed: Bad username / password
On the Rails console of the production server I can verify that ENV["GRID_USER'] and ENV["GRID_PASS"] are correct. The same result I get when I'm checking for Figaro.env.grid_user and Figaro.env.grid_pass (in production mode).
The credentials are correct.
What could be the reason for failing authentication, surprisingly only in production mode?
Here you can see my smtp config in production.rb :
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
user_name: ENV['GRID_USER'],
password: ['GRID_PASS'],
domain: "why.bio",
port: 587,
authentication: 'plain',
enable_starttls_auto: true
}
Here is the way I declare my credentials in application.yml :
GRID_USER: "MySendGridUserName"
GRID_PASS: "very_wierd_password"
I have checked a hundreds of questions in StackOverflow and a number of blogs, but could not find the solution.
You need to use ENV['GRID_PASS'] and not just ['GRID_PASS'] in your configuration.

Configure two gmail servers in one Rails 3.2 app?

Is it possible to configure two gmail servers in one rails app? I did a web search and didn't come up with anything. I would prefer not to get involved in using postfix instead of gmail unless that is the only way.
In my production.rb file:
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'domain1.com',
user_name: 'serv#domain1.com',
password: '************',
authentication: 'plain',
enable_starttls_auto: true }
I'd like to configure a second server for my gmail server at serv#domain2.com. Is it possible? If yes, then how?

Why am I unable to send an email with pow?

I have a rails project which sends an email using an ActionMailer. This seems to work fine with 'rails server' on localhost:3000 but when I use pow, I get authentication error messages from the smtp server. I'm guessing this has something to do with environment variables. Here is the config code
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "railscasts.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
I'm on Mountain Lion.
Thanks
I have a different suggestion:
In dev, just use letter_opener. It's more useful in that context than actually sending emails, anyway.
In production, use SendGrid and not GMail. SendGrid is awesome and really easy to set up.
Pow loads environment variables from checking two files in the application root
.powrc
.powenv
I created the .powrc file using the touch command, then added my environment variables
export GMAIL_USERNAME=username
export GMAIL_PASSWORD="my password"
I then restarted the worker for the app this way:
touch tmp/restart.txt
E-mails now work!
As blamattina has suggested, it may have something to do with your domain in your configuration file! According to the example given by Ruby on Rails Guides:
The correct, Gmail compatible configuration looks like this:
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 }
Your domain is set to railscasts.com. Unless that's your website, my guess is that this domain is not correct. In fact, it is apparently optional as well.
This StackOverflow details that a plugin was once necessary, but is no longer needed if you're using Rails 3.2 or higher. A comment below the top answer in the article details that the domain is optional.
Update: Based on the error you described, it looks like you're hitting an authentication error! This may be because of your login credentials not properly registering.
This user is asking in the context of using Heroku, but the error is the same. If your app works properly with the Rails server, but not on Pow, it's a server-side setup issue. The solution involves needing to properly set your ENV (environment) variables to work with your server.

Thin server not loading environment variables with Capistrano

I'm deploying my rails app with capistrano and using the thin server. I have 2 env vars for my mailer username and password defined in ~/.bash_profile with export SMTP_USERNAME=....
When I ssh to the server and try to send emails from the rails console it works fine but it looks like they are not loaded by thin when I run cap deploy or any other command.
What's the correct way to define them? I've seen some posts where they suggest to define them directly in the config/environments/production.rb or in some other file related to the code itself. I was trying to avoid that for security reasons.
UPDATE: I tried to use a yml file to store this but I ran into this problem:
I created a new file email_config.rb in config/initializers. Its content is:
EMAIL_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/email.yml")[RAILS_ENV]
In config/email.yml I have:
production:
username: username
password: password
In my config/environments/production.rb I have:
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
authentication: :plain,
enable_starttls_auto: true,
user_name: EMAIL_CONFIG[:username],
password: EMAIL_CONFIG[:password]
}
Now the problem is that initializers are loaded after environments.rb and if I put the definition of the EMAIL_CONFIG variable just before my email configuration, then the RAILS_ROOT variable is not define.
Like you're saying, not a god idea to save a password directly in the production.rb. A better option would be to reference them from shell variables (closer to your current setup) or from a yaml file (what I do personally). You can find more information in this post

Resources