2

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 => "user@gmail.com"

  def send_to(user)
    @user = user
    subject='welcome !'
    mail(:to=>'y.lan@gmail.com', :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?

Mellon
  • 37,586
  • 78
  • 186
  • 264

3 Answers3

4

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?

Adrian Serafin
  • 7,665
  • 5
  • 46
  • 67
2

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

tommasop
  • 18,495
  • 2
  • 40
  • 51
0

Have a look at this: http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp

Works nicely for me

Pravin
  • 6,592
  • 4
  • 42
  • 51