Sending mail with Rails 3 in development environment

Addsy picture Addsy · Nov 30, 2010 · Viewed 33.7k times · Source

I'm sure this has been asked a million times before but I can't find anything that works for me so I'm asking again!

I just need a way of sending emails using ActionMailer in rails 3. I have followed numerous tutorials including the Railscasts tutorial on the new ActionMailer and I can see the mails being generated but I don't receive them.

I have tried a bunch of different ways but they generally amount to configuring the following settings

ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :domain               => "gmail.com",
  :user_name            => "[email protected]",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

I have tried the above code (with valid gmail details of course) in my config/environment.rb, config/environments/development.rb and currently have it in its own initialiser config/initialisers/setup_mail.rb

I have also tried with a few different smtp servers including Gmail and Sendgrid, adjusting the smtp settings accordingly but still nothing. I can see the mail in the terminal and the development log and that's it.

Does anyone know of any other gotcha's that I may have missed that need to be setup for ActionMailer to work? Failing that is there a way of getting more information about why the mails aren't being sent? I have

config.action_mailer.raise_delivery_errors = true

set in my config/development.rb but the development log still just shows the same as I see in the terminal.

For what it's worth, I am developing on a Ubuntu 10.04 laptop just in case there's any specific setup needed for that.

Many thanks

Answer

Addsy picture Addsy · Dec 7, 2010

Well I have resolved the issue, but quite why this works and the other methods did not, I don't know.

The solution was to create an initialiser in config/initialisers/setup_mail.rb containing the following

if Rails.env != 'test'
  email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
  ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil?
end

I then added config/email.yml containing the details of the dev and production email accounts

development:
  :address: smtp.gmail.com
  :port: 587
  :authentication: plain
  :user_name: xxx
  :password: yyy
  :enable_starttls_auto: true
production:
  :address: smtp.gmail.com
  :port: 587
  :authentication: plain
  :user_name: xxx
  :password: yyy
  :enable_starttls_auto: true

Like I say, no idea why, but this seemed to do the trick. Thanks all for the pointers