ActionMailer适用于localhost,但不适用于公共服务器

时间:2012-04-04 21:56:18

标签: ruby-on-rails-3 smtp actionmailer

我使用Rails 3构建了一个应用程序,我创建了一个简单的联系我们页面,通过我的SMTP服务器发送邮件。 问题是我的应用程序可以在我的本地主机(我的电脑)中运行它时发送邮件,但是当我在托管服务器(hostgator)中运行应用程序时它不起作用。 有趣的是smtp服务器是一样的!

这是我的localhost中的配置(它可以工作!):<​​/ p>

配置/环境/ developer.rb

# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"

配置/初始化/为setup_mail.rb

ActionMailer::Base.smtp_settings = {  
  :address              => "mail.mydomain.org",  
  :port                 => 26,  
  :domain               => "app.mydomain.org",  
  :user_name            => "register@app.mydomain.org",  
  :password             => "********",  
  :authentication       => "plain",  
  :enable_starttls_auto => false  
} 

我的主机服务器的网址是app.mydomain.org,所以在托管应用中我只更改了这个:

配置/环境/ development.rb

# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'mydomain.org' }
...

在主机服务器中,目前我在开发模式下使用WEBrick运行应用程序。 我收到超时错误:

....
Timeout::Error (execution expired):
  app/controllers/contact_us_controller.rb:13:in `create'
...

我错过了somenthing ??

编辑&amp;解决:

Hostgator支持人员刚刚找到了这个问题的原因。 在ActionMailer设置中,:address必须是localhost,而不是mail.mydomain.org。所以ActionMailer将是:

ActionMailer::Base.smtp_settings = {  
  :address              => "localhost",  
  :port                 => 26,  
  :domain               => "app.mydomain.org",  
  :user_name            => "register@app.mydomain.org",  
  :password             => "********",  
  :authentication       => "plain",  
  :enable_starttls_auto => false  
} 

1 个答案:

答案 0 :(得分:0)

我认为你的意思是development.rb,而不是developer.rb。此设置仅在开发中运行,并假设您已将RAILS_ENV设置为服务器上的生产,它将是production.rb,而不是development.rb,将被处理。

如果两者上的服务器相同,则可以将其移至application.rb(或config / initializers中的脚本)。您可能需要一个不同的生产设置,因此它指向localhost。这也可以解决这里的问题,以防有一些DNS问题或服务器配置阻止外发SMTP请求。