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

Mandrill API using Rails 4.2.5 Excon::Errors::SocketError

我在 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).

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

这是我的配置:

gem文件

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

mandrill.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文档中也没有提到这个问题。

你有什么想法吗?

谢谢!

席琳

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

development.rb

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