ActionMailer Balancer
Making sure email gets delivered can be one of the more challenging aspects of developing a web application. One way to alleviate this is to use a trusted email server like Google’s. However, Google only allows users to send to 500 recipients/day with their free accounts and 2000 recipients/day with their paying accounts. This works ok for small volumes but the limits become an issue with a medium to large applications.
To get around this problem, Tony joked about creating a load balancer for ActionMailer. I thought it was a great idea so I created it! I would like to introduce ActionMailer Balancer. It allows you to configure several SMTP user accounts that will be used at random every time an email is created or delivered.
To use it, configure your smtp server as usual in environment.rb:
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "test.com",
:authentication => :plain,
:user_name => "no-reply1@test.com",
:password => "password"
}
It is probably a good idea to have a default account set up. Then add something like the following to an initializer:
ActionMailer::Base.smtp_users = [
{
:user_name => "no-reply1@test.com",
:password => "password"
},
{
:user_name => "no-reply2@test.com",
:password => "password"
}
]
Check out the source at GitHub.
Update: If you plan to use Gmail or Google Apps to send those emails, make sure you use something like action_mailer_optional_tls until you’re using Ruby 1.9