Redmine email configuration with environment variables - ruby-on-rails

I've configured email on redmine according to the instructions in the redmine wiki, and it all works fine. Here are the contents of my config/configuration.yml file:
production:
delivery_method: :smtp
smtp_settings:
address: "smtp.sendgrid.net"
port: 587
authentication: :plain
domain: "heroku.com"
user_name: my_email#gmail.com
password: my_password
However I am trying to use environment variables in place of my_email#gmail.com and my_password like so:
production:
delivery_method: :smtp
smtp_settings:
address: "smtp.sendgrid.net"
port: 587
authentication: :plain
domain: "heroku.com"
user_name: <%= ENV['SENDGRID_USERNAME'] %>
password: <%= ENV['SENDGRID_PASSWORD'] %>
When I try to send a test email, with the environment variables in the config file, redmine give this error:
'An error occurred while sending mail (535 Authentication failed: Bad username / password )'.
So I guess the erb snippet is not being evaluated (I have double checked the values of the environment variables).
I've searched for a solution and come up empty, does anyone have any suggestions as to how I should configure email in redmine so that I don't expose my sendgrid credentials in the config file?
Alternatively if someone can tell me that it's not a security risk to use the credentials directly, without environment variables, that would also solve my problem.

I had answered this question 2 weeks ago, and it was deleted by someone else right away.
So I'm not sure if someone will delete this answer again to prevent other guys knowing how to solve this problem. Good luck!
I got the same issue and this article
Set up mailing in redmine on heroku solved my problem.
The idea is moving the settings from config/configuration.yml to config/environments/production.rb and using Ruby code to set it up. Since ENV['key'] is handled by erb, I guess configuration.yml is not handled that way. Maybe there is some security issue?
So in your case, you should add these codes to config/environments/production.rb as follows,
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
And remove codes from config/configuration.yml and make it looks like this,
default:
# Outgoing emails configuration (see examples above)
email_delivery:
delivery_method: :smtp

Related

How to use Ruby on Rails 3.2 Action Mailer with Gmail without turning unsecure device attributes on?

Is there anyway to use action mailer without turning the unsecure device gmail attribute on? I don't want to expose my email account to the world by turning the unsecure option on for my ruby on rails app.
Is there a way to enter the username and password for such an email system without assigning the username and passwords directly to the application environment of development/production modes?
How is this all done and do I need any additonial information?
Bare in mind I am using Ruby on Rails 3.2 version and not 4.0
You can use this as:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:authentication => :plain,
:address => "smtp.gamil.com",
:port => 587,
:domain => Rails.application.secrets.domain,
:user_name =>Rails.application.secrets.username,
:password => Rails.application.secrets.password
}
Write in secret.yml:
development:
domain: <%= ENV Domain_you_want %>,
user_name: <%= ENV user_name_you_want %>,
password: <%= ENV password %>
The standard way of protecting your sensitive information when configuring Rails is to use the shell environment variables. In the given environment config file, use the ENV hash to access the shell environment variables:
# e.g. config/environment/production.rb
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
Then you have to define the env. variables somewhere. This can be done in the ~/.bashrc file of the user under which you run Rails (the file will be different if you use other shell than bash):
# ~/.bashrc
export GMAIL_USERNAME="myname#gmail.com"
# etc...
Then relogin to the console and try start up Rails and it should get the correct info from the shell variables. Take a look at this guide for more info. See also the Rails guides for all mail configuration options.

how to send mail through the localhost?

please help solve the problem.
i use webrick server on localhost. in development.rb i registered:
config/enviroments/development.rb:
Rails.application.configure do
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end
i use gem devise. and i user 'forgot youre password'-function (devise-recoverable module). the result did not get to the email message. but where can I find a mark(log) that was an attempt to send a letter? whether there is a directory where they are stored?
You will need to edit your config/environments/development.rb file.
Here's something to get you started, add this to that file:
# port 3000 or whatever port you are using for your localhost
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
# this line is what you want to be true, else you won't get messages!
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: 'gmail.com' # or to whatever domain your email is
authentication: "plain",
enable_starttls_auto: true,
user_name: 'my_gmail_or_other_username',
password: 'my_gmail_or_other_password'
}
Now, when you are moving towards production or even just pushing it to a public repository you obviously don't want to hardcode the username and password (for security purposes). For this I suggest you look up how to use environmental variables. The Figaro gem might also come in handy here and is another option.
This is the default 'smtp' way, there are other methods, such as the use of a gem as specified by the earlier answer.
try gem mailcatcher for development purpose.
http://mailcatcher.me/
github: https://github.com/sj26/mailcatcher
This gem will provide you a tab in browser where all your emails will be there unless you delete them.

ArgumentError in Devise::RegistrationsController#create SMTP-AUTH requested but missing user name

I have problem with sending emails from localhost.
it was working on linux and now I'm trying to run my app on mac and it doesn't work already.
I have an error:
ArgumentError in Devise::RegistrationsController#create
SMTP-AUTH requested but missing user name
in config/environments/development.rb:
#Action Mailer config
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
# Send email in development mode.
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: "587",
domain: "mail.google.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USER_ID"],
password: ENV["GMAIL_PASSWORD"]
}
My env variables are good, I tried put my credential into code as well and it still doesn't work :/
I checked similiar topics but didn't found the solution.
What can I try?
I read:
I tried put my credential into code as well and it still doesn't work :/
But have you tried replacing your var env with your gmail id/password like this ?
#Action Mailer config
**********
user_name: "my_username",
password: "my_password"
}
Just to be sure.
Ok, solved.
credentials didn't work because I havn't restarted my server (stupid laziness)
my env vars didn't work because I've puted them into ~.profile but it work in ~.bash_profile, that was helpful (I use OS X Yosemite).

Not able to create Heroku account that was made in the Devise gem

I am working on my first rails application and I have just pushed it to production through Heroku. The next step in my assignment is to create an account in production and have it send an email confirmation to that email.
config/environments/production.rb
config.action_mailer.default_url_options = { host: 'alex-bloccit.herokuapp.com' }
config/initializers/setup_mail.rb
if Rails.env.development?
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'smtp.sendgrid.net',
port: '587',
authentication: :plain,
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
domain: 'heroku.com',
enable_starttls_auto: true
}
end
config/initializers/setup_mail.rb
if Rails.env.development? || Rails.env.production?
config/initializers/devise.rb
config.mailer_sender = "alex.colbert1987#gmail.com"
The code updates to github and pushes to Heroku just fine, but when I go to the website and I attempt to create an email under the email. Hope this provides enough information to troubleshoot.
In config/initializers/setup_mail.rb, you have if Rails.env.development? before the ActionMailer configuration. This config won't be run in production because of the way the if statement is structured.

Sending email through Rails 3 with gmail problems

I am getting this error whilst trying to send emails through gmail -
Net::SMTPAuthenticationError (530 5.7.0 Must issue a STARTTLS command first. pw17sm4922458lab.5
):
app/controllers/contact_controller.rb:11:in `create'
I have tried loads of different things but to no avail, below is my settings from production.rb
# Change mail delvery to either :smtp, :sendmail, :file, :test
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "bizmodev.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: '******.*****#gmail.com',
password: '********'
}
# Specify what domain to use for mailer URLs
config.action_mailer.default_url_options = {host: "bizmo.co.uk"}
Any help on this matter would really be appreciated.
OK fixed it, basically I installed sendmail on my VPS and then restarted Apache and it now works -
Ubuntu Sendmail command line install
apt-get install sendmail
Hope this may help someone in the future...
you can enable starttls like this:
config.action_mailer.smtp_settings = {
....
:enable_starttls_auto => true}

Resources