Mandrill API 使用 Rails 4.2.5 Excon::Errors::SocketError

Posted

技术标签:

【中文标题】Mandrill API 使用 Rails 4.2.5 Excon::Errors::SocketError【英文标题】:Mandrill API using Rails 4.2.5 Excon::Errors::SocketError 【发布时间】:2016-03-07 05:23:10 【问题描述】:

我在我的 Rails 应用程序中使用 Mandrill 来发送交易电子邮件。我使用了'mandrill-api' gem,并根据文档进行了配置。

它在生产中运行良好,但在本地出现错误突袭:

Unable to verify certificate, please set `Excon.defaults[:ssl_ca_path] = path_to_certs`, 
`ENV['SSL_CERT_DIR'] = path_to_certs`, `Excon.defaults[:ssl_ca_file] = path_to_file`, `ENV['SSL_CERT_FILE'] = path_to_file`, 
`Excon.defaults[:ssl_verify_callback] = callback` (see OpenSSL::SSL::SSLContext#verify_callback), 
or `Excon.defaults[:ssl_verify_peer] = false` (less secure).

我不明白为什么它在生产而不是在本地工作?

这是我的配置:

宝石文件

source 'https://rubygems.org'

ruby '2.2.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.15'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for javascript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  #gem 'byebug'
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'quiet_assets'
  gem 'pry-rails'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

group :production do
  gem 'rails_12factor'
end

# Privacy
gem 'figaro'

# Heroku
gem 'heroku'

# Private access!
gem 'lockup'

# Mails
gem 'mandrill-api'

development.rb

Rails.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.
  config.action_mailer.raise_delivery_errors = false

  # 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

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # Mails
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true

  # Devise
  #config.action_mailer.default_url_options =  host: 'localhost', port: 3000 

  # TODO: do it with a template: http://blog.nvisium.com/2014/10/mandrill-devise-and-mailchimp-templates.html
  config.action_mailer.smtp_settings = 
    :address   => 'smtp.mandrillapp.com',
    :port      => 587, # ports 587 and 2525 are also supported with STARTTLS
    :enable_starttls_auto => true, # detects and uses STARTTLS
    :user_name => ENV['MANDRILL_USERNAME'],
    :password  => ENV['MANDRILL_API_KEY'], # SMTP password is any valid API key
    :authentication => 'login', # Mandrill supports 'plain' or 'login'
    :domain => ENV['HOST'], # your domain to identify your server when connecting
  
end

ma​​ndrill.rb

require 'mandrill'

# Use an environment variable instead of placing the key in source code
MANDRILL = Mandrill::API.new ENV['MANDRILL_API_KEY'], true

仅在本地引发错误的函数:

  def send_short_email(subject, message, receiver)
    inner_message = 
      to:  [name: receiver.name, type: 'to', email: receiver.email],
      subject: subject,
      html: message,
      text: message,
      from_email: ENV['DEFAULT_EMAIL_SENDER']
    
    MANDRILL.messages.send inner_message
    logger.debug inner_message
  end

我尝试更改设置、主机或 API 密钥,但没有任何效果。而且 gem 文档中也没有提到这个问题。

你有什么想法吗?

谢谢!

席琳

【问题讨论】:

这是 Excon 的 SSL 证书问题。可能与此相关或相同的问题:github.com/excon/excon/issues/473 这可能也值得一读:github.com/excon/excon/issues/239 我阅读了它们,但我不知道如何解决问题。 :-( 也许删除并重新安装 Ruby? 因为它是您的开发机器,您可以尝试跳过证书验证。设置Excon.defaults[:ssl_verify_peer] = false 并尝试。但仅在您的开发机器上,在生产环境中使用是不安全的。 你是我的救星。它正在工作:-) 谢谢! 【参考方案1】:

正如 Casper 在其评论中所建议的,我编辑了我的 development.rb 文件以跳过证书验证。

development.rb

require 'excon'
Rails.application.configure do
     ...
     Excon.defaults[:ssl_verify_peer] = false
end

【讨论】:

以上是关于Mandrill API 使用 Rails 4.2.5 Excon::Errors::SocketError的主要内容,如果未能解决你的问题,请参考以下文章

是否可以为 Mandrill 创建“开发”API 密钥?

在 mandrill 模板中使用 Ruby/rails 变量

带有 Heroku 的 Rails 不发送 Mandrill 电子邮件

使用 Mandrill 和 Rails 的多个收件人

在测试环境中为 Mandrill 配置 rails 应用程序

Rails 4 Mailchimp Mandrill 与设计