2
votes

I am using Rails 3 and implementing the email sending feature. I am not sure if my configuration is correct, but here are my codes:

mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  default :from => "[email protected]"

  def send_to(user)
    @user = user
    subject='welcome !'
    mail(:to=>'[email protected]', :subject=>subject, :content_type => "text/html")
    mail.deliver
  end
end

controller:

def CarsController < BaseController
  ... 
  def register_finish
    UserMailer.send_to(user)
  end

end

config/enviroment.rb

config.action_mailer.delivery_method = :smtp

 config.action_mailer.smtp_settings = {
     :address => "smtp.googlemail.com",
     :port => 532,
     :arguments => '-i'
    :enable_starttls_auto => true
   }

  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

When my controller invoke 'register_finish' function and try to send email to a user, I always get Timeout::Error (execution expired) error message, what could be the reason??

I saw some people define the configuration in config/initializers/setup_email.rb and use

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = { ...}

while I configure it in config/enviroment.rb and use:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {...}

I also saw some people invoke "deliver" method inside controller while I invoke it inside 'UserMailer'.

My questions:

  1. What's the difference between my implementation and the above different way of implementations I found from internet.

  2. Why I got timeout errors?

3

3 Answers

4
votes

I'm also using gmail as my smtp server and I've adder setup_email.rb to initiliazers containing this code

ActionMailer::Base.smtp_settings = {
   :address              => "smtp.gmail.com",
   :port                 => 587,
   :domain               => "domain.pl",
   :user_name            => "username",
   :password             => "password",
   :authentication       => "plain",
   :enable_starttls_auto => true
}

and it works for me :)

EDIT

I've just notice we are using different servers, maybe try with my config?

2
votes

Timeout errors mean that there is some authentication errors.

This line is no longer needed:

ActionMailer::Base.delivery_method = :smtp

While it is adviceable to set the smtp_settings in an initializer.

If you are using it on a development machine this configuration should work with gmail:

ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'your_domain',
:authentication => :plain,
:user_name => 'your_gmail_username',
:password => 'your_gmail_password'
}

EDIT

you can add for a development machine:

ActionMailer::Base.default_url_options[:host] = "localhost:3000"

Very good railscast on subject