Rails 4:调用ActionMailer时的Net :: ReadTimeout

时间:2014-10-02 17:16:18

标签: ruby-on-rails actionmailer

当我在开发模式下运行邮件程序时,出现以下错误:

Net::ReadTimeout in SchoolApplicationsController#create

以下是获取超时的控制器方法

  def create
    @school_application = SchoolApplication.new(school_application_params)
     @school_application.program_cost =    @school_application.calculate_cost_to_charge(params[:school_application][:program], params[:school_application][:duration])
    if @school_application.save
      Rails.logger.debug("Hey mufugga")
      NotificationsMailer.send_application(@school_application).deliver
      redirect_to application_path(@school_application.id)
    else  
      Rails.logger.debug(@school_application.errors.full_messages)
          @school_application.errors.full_messages.each do |msg|
        flash.now[:error] = msg
      end
      render action: "new"
    end
  end

我很肯定错误是由NotificationsMailer调用引起的,因为什么时候 我发表评论,我不再收到错误。

这是我的邮件和设置:

class NotificationsMailer < ActionMailer::Base

  default :from => "from@fls.net"
  default :to => "ryan@fls.net"

  def send_application(application)
    @application = application 
    mail(:subject => "New Application")
  end
end

以下是我的environments/development.rb smtp设置:

Fls::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  address:              'secure3209.hostgator.com',
  port:                 465,
  domain:               'fls.net',
  ssl: true,
  user_name:            ENV['fls_username'],
  password:             ENV['fls_password'],
  authentication:       'plain',
  enable_starttls_auto: true  }
end

当我在Rails控制台中编写ENV['fls_username']时,我得到了正确的值。与密码相同。用户名的格式为user@fls.net。这是正确的还是正确的格式只是“用户”,并且domain参数隐含了域名?

3 个答案:

答案 0 :(得分:33)

阅读此post后,我又看了一下我的smtp设置并添加了

tls: true 

改变    port: 465port: '465'因为我注意到大多数人都把它写成字符串。同样,将字符串"plain"更改为符号:plain

答案 1 :(得分:3)

当我将smtp邮件连接到qq邮件(商业邮件)时,我遇到了类似的问题。 我按照post更新了我的设置,如下所示:

config.action_mailer.smtp_settings = {
address:              'smtp.exmail.qq.com',
port:                 '465',
domain:               'groobusiness.com',
user_name:            ENV['GMAIL_USER_NAME'],
password:             ENV['GMAIL_PASSWORD'],
authentication:       :plain,
enable_starttls_auto: true,
openssl_verify_mode:  'none',
ssl:                   true,
tls:                   true
}

问题解决了。 希望它可能对面临这个问题的人有所帮助。

答案 2 :(得分:-1)

在intitialize

中添加以下代码
require 'net/smtp'

module Net
  class SMTP
    def tls?
      true
    end
  end
end