Simplify your ActionMailer configuration with YAML

Posted by Jon
on Friday, March 23

Ruby on Rails looks for your outgoing email settings in a hash in your environment files, like this:

1
2
3
4
5
6
7
8
ActionMailer::Base.server_settings = {
  :address => "mail.domain.com",
  :port => 25,
  :domain => "mail.domain.com",
  :user_name => "email_account_login",
  :password => "email_account_password",
  :authentication => :login
}

This isn’t exactly the cleanest way to configure your SMTP settings, and if you want different settings for your development and production environments, you’ll need to define these settings in two different files (environments/development.rb and environments/production.rb). This means that there is more than one place that your SMTP configuration could be. Not very Rails-like.

Our yaml_mail_config plugin allows you to define your SMTP settings just like your database settings, with a clean, readable email.yml config file.

Install the plugin like this:

script/plugin install \ 
svn://rubyforge.org/var/svn/slantwise/yaml_mail_config/trunk yaml_mail_config

After that, all you have to do is create a config/email.yml file that defines your ActionMailer settings. Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 development:
    server: mail.domain.com
    port: 25
    domain: domain.com
    authentication: login
    username: email_account_login
    password: email_account_password
  production:
    server: mail.domain.com
    port: 465
    domain: domain.com
    authentication: login
    username: email_account_login
    password: email_account_password
    tls: true

Thanks to Sebastien Grosjean for his post which inspired this plugin – the idea and most of the code are his.

Comments

Leave a response