2011-03-14 155 views
2

我正在使用Rails 3並實現電子郵件發送功能。我不知道如果我的配置是正確的,但這裏是我的代碼:rails 3電子郵件發送問題

郵寄/ 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 

控制器

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

end 

配置/環境。 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 

當我的控制器調用'register_finish'函數並嘗試向用戶發送電子郵件時,我總是得到Timeout :: Error(執行過期)錯誤消息,可能是什麼原因?

我看到一些人在配置/初始化/ setup_email.rb定義配置和使用

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

,而我在配置/ enviroment.rb和使用它配置:

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

我還看到一些人在控制器內部調用「deliver」方法,同時在'UserMailer'內部調用它。

我的問題

  1. 什麼是我的執行和實現,我從互聯網上找到的與上述不同的方式之間的差異。

  2. 爲什麼我有超時錯誤?

回答

4

我還使用Gmail作爲我的SMTP服務器和我有加法setup_email.rb包含該代碼

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

initiliazers和它的作品對我來說:)

編輯

我剛剛注意到我們正在使用不同的服務器,也許嘗試使用我的配置?

+0

控制檯顯示的電子郵件被髮送,但是郵箱沒有收到... – Mellon 2011-03-14 12:19:20

+0

等待它,它可能會經過一段時間;) – 2011-03-14 12:23:09

+0

還沒收到:(但感謝你幫助:) – Mellon 2011-03-14 12:35:15

2

超時錯誤表示存在一些驗證錯誤。

此線不再需要:

ActionMailer::Base.delivery_method = :smtp 

雖然adviceable設置smtp_settings中的初始化。

如果您使用的是它在開發機器上這個配置應該與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' 
} 

編輯

,你可以添加對開發機器:

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

Very good railscast on subject

+0

放在哪裏,在config/enviroment.rb或在config/initializer/setup_email.rb ??? – Mellon 2011-03-14 11:58:26