HOW TO GET DEVISE, FIGARO, HEROKU AND EMAILING TO PLAY TOGETHER NICELY

Update:

I recently made the switch over to SparkPost, which is one of Heroku’s newest add-ons (I also work there)! There is a free basic plan that includes up to 10K of emails a month. SparkPost has the highest inbox placement of any provider, and real-time analytics with over 40 different metrics. It's also built by Message Systems, which is the email provider for large scale companies like Facebook, Twitter, and Pinterest, so that made migration an easy choice! The move was straight forward, quick, and easy!

Once you have a card on file, just run
heroku addons:create sparkpost:free
to add SparkPost to your app.

I'm going to write this from the standpoint of where I was last week so here's some background. I, like many other newbies was happily going along completing various Rails Tutorials not aware that my "secret token" was being committed to my public GitHub repo for the world to see. For random tutorials this isn't really a problem, but when it's time to start an actual app you plan to deploy, then this is going to be necessary. The solution... environment variables. There are a ton of ways to go about hiding your apps token (and other confidential info like emails and passwords), but I opted for Figaro.

Configuration is pretty straight forward. Add Figaro to your Gemfile and run
$ bundle install

followed by
$ rails generate figaro:install

Figaro's generate command will create an application.yml file for you and automatically add it to your .gitignore!

Now, add the variables you want hidden into config/application.yml.
It will look something like this if you use Gmail:

GMAIL_USERNAME: person@gmail.com
GMAIL_PASSWORD: foobar
SECRET_TOKEN: 6946a9da401cecfff799b9f2a8c9ef01c454fc4c45b8610350cccb1c82763862f473d390ae549b0919fd327fc01abc49d998202e2e41cee2c07b95d6e6fba3b8</pre>
<p>(No, that's not actually my token. You can generate them whenever needed by running <code>$ rake secret</code> in terminal.)</p>
<p>Then just access your environment variables wherever they're needed within your app. In <strong>config/initializers/secret_token.rb</strong> it would look something like this.</p>
< title="secret_token.rb">MyApp::Application.config.secret_token = ENV["SECRET_TOKEN"]

Next, run
$ rake figaro:heroku
to add your environment variables to Heroku. If you're like me and want to see proof, you can run
$ heroku config
just to make sure they're there.

Now for the emails. (It's worth mentioning, that I used my gmail account initially). Heroku recommends SendGrid, and I'm glad I went this route. The basic plan is free and comes with 6K emails monthly. You'll need a credit card on file with Heroku, but there are no charges if you remain in that range.

Once you have a card on file, run
heroku addons:add sendgrid:starter
to add SendGrid to your app.

To generate your apps email run
$ heroku config:get SENDGRID_USERNAME,

followed by
$ heroku config:get SENDGRID_PASSWORD
to generate your password.

Ok, almost there. Now back to your app's directory. In config/initializers/devise.rb, find
config.mailer_sender
and set that to your SendGrid email environment variable (that was generated for you when running the commands above). It will look like this:

config.mailer_sender = ENV["SENDGRID_USERNAME"]

Next, in envrionments/production.rb you'll need to include the following for Devise.

config.action_mailer.default_url_options = { :host =&gt; 'whatever-heroku-gives-you.herokuapp.com'}

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 25,
domain: "heroku.com", 
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["SENDGRID_USERNAME"],
password: ENV["SENDGRID_PASSWORD"]
}

Run through your deploy process and you will be good to go!

Written on July 11, 2013