Monday 4 June 2012

Setting environment variables in ruby on rails

I use GMail's SMTP gateway for sending out system generated emails in my RoR project. Once, I got everything working, I thought of deploying it on Heroku. However, I host my code on Github and did not want to commit the id and password on the GMail account. So, I decided to read the email id and password as environment variables.

I set the environment variables on Heroku using the following:

heroku config:add ID=my.id@gmail.com
heroku config:add PASS=mypass


In the code I accessed these variables using ENV['ID'] and ENV['PASS'] respectively. So, the code was somewhat as follows:

class Emailer < ActionMailer::Base
    default from: "#{ENV['ID']}", :charset => "UTF-8"
    # other methods
end


With this I could easily send out mails from Heroku.

Whenever I want to test anything on my local using emails, I set environment variables before running the Rails server.

export ID=my.id@gmail.com
export PASS=mypass
rails s

1 comment:

Anonymous said...

Thanks for this post: easy and 'everything usefull about the subject' is here, just the way I like it !