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

Tha*_*tta 12 ruby-on-rails actionmailer

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

Net::ReadTimeout in SchoolApplicationsController#create
Run Code Online (Sandbox Code Playgroud)

这是获取超时的控制器方法

  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
Run Code Online (Sandbox Code Playgroud)

我很肯定错误是由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
Run Code Online (Sandbox Code Playgroud)

这是我的environments/development.rbsmtp设置:

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
Run Code Online (Sandbox Code Playgroud)

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

Tha*_*tta 36

看完这篇文章后,我又看了一下我的smtp设置并添加了

tls: true 
Run Code Online (Sandbox Code Playgroud)

改为 port: 465,port: '465'因为我注意到大多数人把它写成一个字符串.也类似地将字符串"plain"更改为符号:plain

  • 看起来像`tls:true`是关键.我不相信其他变化很重要.(还要注意它是`tls`,而不是`tsl`.) (8认同)
  • 这也适合我,但我希望我的生命的最后一小时回来. (2认同)

man*_*ire 6

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

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
}
Run Code Online (Sandbox Code Playgroud)

并且问题得到了解决。希望对遇到此问题的人有所帮助。