11 February 2010

ActionMailer and Multiple SMTP Accounts (useful with gmail)

ActionMailer works with only one smtp account; a single google apps account is limited to 500 (or 2000) mails per day; my app legitimately needs more. Besides, I'd rather notifications come from an appropriately named account rather than a generic "no-reply@example.com" address. Here's what I came up with. Firstly, config/smtp.yml describes my various accounts - I settled on one per mailer class. Secondly, a patch to ActionMailer::Base enables switching smtp accounts based on the mailer class.

Here's an example with four mailers: an account mailer, an exception notifier (works with the lovely ExceptionNotifier plugin), a "share this" mailer so your site can be all viral and stuff, and a prize mailer for the good news.

config/smtp.yml

defaults: &defaults
  address:        smtp.gmail.com
  port:           587
  domain:         example.com
  authentication: !ruby/sym plain

account_mailer:
  <<: *defaults
  user_name:      accounts@example.com
  password:       "pw1"

prize_mailer:
  <<: *defaults
  user_name:      winner@example.com
  password:       "pw2"

exception_notifier:
  <<: *defaults
  user_name:      dev_team_obviously_sucks@example.com
  password:       "pw3"

share_this_mailer:
  <<: *defaults
  user_name:      share_this@example.com
  password:       "pw4"

ActionMailer::Base patch

require 'smtp_tls'

module ActionMailer
  class Base
    cattr_accessor :smtp_config

    self.smtp_config = YAML::load(File.open("#{RAILS_ROOT}/config/smtp.yml"))

    def smtp_settings
      smtp_config[mailer_name].symbolize_keys
    end
  end
end

I put this in config/initializers/action_mailer.rb. And that's it! No changes required to your mailers or email templates or anything else in your application. As you can see, the patch merely overrides ActionMailer's smtp_settings class method and replaces it with an instance method that decides at sending-time which smtp configuration to use.

We needed to do this because sendmail was failing erratically - some users weren't getting any mail from our app at all - presumably due to hypersensitive spam filters somewhere on the chain, maybe related to my not understanding how SPF records are supposed to work.

You could easily fancify this to switch SMTP config based on the time of day, or based on your user's locale (so you can use a nicely localised "from" address - Google overrides the "from" address sent by ActionMailer), or even based on whether the Moon is in Scorpio if you cared. Just replace the call to mailer_name with a call to your config-switching method.

I understand that rails 3 is beautifuller in many ways including the way ActionMailer works so this might well be obsolete in a few months except for you suckers working on legacy systems. I hope this helps, let me know one way or the other.

07 February 2010

it wasn't git actually

Ouch! Panic!

$ git push origin master
fatal: unable to fork

and later,

$ git push origin master
fatal: git-pack-objects failed (Resource temporarily unavailable)

But it had nothing to do with git or a corrupted repository as google seemed to be trying to suggest; it was rather my trusty mac running out of something. The solution: shut down itunes and try again.