Dreamhost, ActionMailer, SMTP and Google Apps
04 September 2009
I was banging my head for a little bit this morning trying to figure out why a Rails app I had running on Dreamhost wasn’t delivering emails (signup confirmations, contact requests, etc.) as expected. A little bit of Googling later, I pieced the following together. Here’s a quick recap, in case it saves any of you some time and confusion:
(Note: These instructions are for an app running Rails v.2.3.2. Earlier versions of Rails require some extra fiddling to make SMTP work with TLS, which is an additional protocol that Gmail servers use. With 2.3.2, that support is now built into ActionMailer.)
ActionMailer has default SMTP settings which are specified in /vendor/rails/actionmailer/lib/action_mailer/base.rb. Those settings are what your app will use unless you specify something different in your environment configs. It may be that if you’re using Dreamhost’s own mail servers to run email for your domain, these settings work fine (not sure, haven’t tested.) But if you’re using Google Apps to run your email (which is an easy one-click option for Dreamhost to alter your DNS settings appropriately), then those default settings won’t result in your emails being delivered.
So, to get things up and running, we just need to override the default settings. Technically, we could make the change in either /config/environment.rb or in /config/environments/production.rb. Changing it in environment.rb will use these settings no matter which environment you’re running in (including local development), which you probably don’t want, so it’s most appropriate to make the change directly in production.rb — that way, these settings will only be used by your live app.
In production.rb, you probably don’t see anything referencing SMTP settings. You’ll want to paste in the following code:
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'e.g. yourdomain.com -- the domain for which you are using Google Apps',
:user_name => 'user@yourdomain.com -- the address from which you want to send',
:password => 'password for above user', :authentication => :plain,
:enable_starttls_auto => true
}
Once you save your new production.rb file and upload (and restart your Rails app), you should find that emails generated from your app (from, say, a contact form) should now be getting to the intended recipient, and not getting hung up in the internet netherworld.
(This was pieced together from info I gleaned from here.)
