Skip to content

Commit

Permalink
Refactored SSL context code in Mail::SMTP; allow :ca_path or :ca_file…
Browse files Browse the repository at this point in the history
… to be passed in as settings.
  • Loading branch information
ndbroadbent committed Jan 20, 2013
1 parent 64f4eb5 commit d6e25e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
51 changes: 23 additions & 28 deletions lib/mail/network/delivery_methods/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d6e25e5

Please sign in to comment.