From d6e25e5e37e6d591f94cc04a2a52c1128fde81db Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Mon, 21 May 2012 21:37:52 +1200 Subject: [PATCH] Refactored SSL context code in Mail::SMTP; allow :ca_path or :ca_file to be passed in as settings. --- lib/mail/network/delivery_methods/smtp.rb | 51 ++++++++++------------- spec/spec_helper.rb | 2 +- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/lib/mail/network/delivery_methods/smtp.rb b/lib/mail/network/delivery_methods/smtp.rb index 53c13305d..21359e54e 100644 --- a/lib/mail/network/delivery_methods/smtp.rb +++ b/lib/mail/network/delivery_methods/smtp.rb @@ -100,35 +100,11 @@ def deliver!(mail) smtp = Net::SMTP.new(settings[:address], settings[:port]) if settings[:tls] || settings[:ssl] if smtp.respond_to?(:enable_tls) - unless settings[:openssl_verify_mode] - smtp.enable_tls - else - openssl_verify_mode = settings[:openssl_verify_mode] - if openssl_verify_mode.kind_of?(String) - openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize - end - context = Net::SMTP.default_ssl_context - context.verify_mode = openssl_verify_mode - smtp.enable_tls(context) - end + smtp.enable_tls(ssl_context) end elsif settings[:enable_starttls_auto] - if smtp.respond_to?(:enable_starttls_auto) - unless settings[:openssl_verify_mode] - smtp.enable_starttls_auto - else - openssl_verify_mode = settings[:openssl_verify_mode] - if openssl_verify_mode.kind_of?(String) - openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize - end - if RUBY_VERSION >= '1.9.0' - context = Net::SMTP.default_ssl_context - context.verify_mode = openssl_verify_mode - smtp.enable_tls(context) - else - smtp.enable_tls(openssl_verify_mode) - end - end + if smtp.respond_to?(:enable_starttls_auto) + smtp.enable_starttls_auto(ssl_context) end end @@ -140,6 +116,25 @@ def deliver!(mail) return settings[:return_response] ? response : self end - + + private + + # Allow SSL context to be configured via settings, for Ruby >= 1.9 + # Just returns openssl verify mode for Ruby 1.8.x + def ssl_context + openssl_verify_mode = settings[:openssl_verify_mode] + if openssl_verify_mode.kind_of?(String) + openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize + end + if RUBY_VERSION < '1.9.0' + return openssl_verify_mode + end + + context = Net::SMTP.default_ssl_context + context.verify_mode = openssl_verify_mode + context.ca_path = settings[:ca_path] if settings[:ca_path] + context.ca_file = settings[:ca_file] if settings[:ca_file] + context + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7e9d9d5e3..2f020860b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -95,7 +95,7 @@ def enable_tls(context = nil) end end - def enable_starttls_auto + def enable_starttls_auto(context = :dummy_ssl_context) true end