lib/net/smtp.rb in net-smtp-0.2.2 vs lib/net/smtp.rb in net-smtp-0.3.0
- old
+ new
@@ -177,11 +177,11 @@
# # CRAM MD5
# Net::SMTP.start('your.smtp.server', 25
# user: 'Your Account', secret: 'Your Password', authtype: :cram_md5)
#
class SMTP < Protocol
- VERSION = "0.2.2"
+ VERSION = "0.3.0"
Revision = %q$Revision$.split[1]
# The default SMTP port number, 25.
def SMTP.default_port
@@ -213,29 +213,44 @@
#
# +address+ is the hostname or ip address of your SMTP
# server. +port+ is the port to connect to; it defaults to
# port 25.
#
+ # If +tls+ is true, enable TLS. The default is false.
+ # If +starttls+ is :always, enable STARTTLS, if +:auto+, use STARTTLS when the server supports it,
+ # if false, disable STARTTLS.
+ #
+ # If +tls_verify+ is true, verify the server's certificate. The default is true.
+ # If the hostname in the server certificate is different from +address+,
+ # it can be specified with +tls_hostname+.
+ #
+ # Additional SSLContext params can be added to +ssl_context_params+ hash argument and are passed to
+ # +OpenSSL::SSL::SSLContext#set_params+
+ #
+ # +tls_verify: true+ is equivalent to +ssl_context_params: { verify_mode: OpenSSL::SSL::VERIFY_PEER }+.
# This method does not open the TCP connection. You can use
# SMTP.start instead of SMTP.new if you want to do everything
# at once. Otherwise, follow SMTP.new with SMTP#start.
#
- def initialize(address, port = nil)
+ def initialize(address, port = nil, tls: false, starttls: :auto, tls_verify: true, tls_hostname: nil, ssl_context_params: nil)
@address = address
@port = (port || SMTP.default_port)
@esmtp = true
@capabilities = nil
@socket = nil
@started = false
@open_timeout = 30
@read_timeout = 60
@error_occurred = false
@debug_output = nil
- @tls = false
- @starttls = :auto
+ @tls = tls
+ @starttls = starttls
@ssl_context_tls = nil
@ssl_context_starttls = nil
+ @tls_verify = tls_verify
+ @tls_hostname = tls_hostname
+ @ssl_context_params = ssl_context_params
end
# Provide human-readable stringification of class state.
def inspect
"#<#{self.class} #{@address}:#{@port} started=#{@started}>"
@@ -415,11 +430,11 @@
# SMTP session control
#
#
# :call-seq:
- # start(address, port = nil, helo: 'localhost', user: nil, secret: nil, authtype: nil, tls_verify: true, tls_hostname: nil, ssl_context_params: nil) { |smtp| ... }
+ # start(address, port = nil, helo: 'localhost', user: nil, secret: nil, authtype: nil, tls: false, starttls: :auto, tls_verify: true, tls_hostname: nil, ssl_context_params: nil) { |smtp| ... }
# start(address, port = nil, helo = 'localhost', user = nil, secret = nil, authtype = nil) { |smtp| ... }
#
# Creates a new Net::SMTP object and connects to the server.
#
# This method is equivalent to:
@@ -452,10 +467,15 @@
# The remaining arguments are used for SMTP authentication, if required
# or desired. +user+ is the account name; +secret+ is your password
# or other authentication token; and +authtype+ is the authentication
# type, one of :plain, :login, or :cram_md5. See the discussion of
# SMTP Authentication in the overview notes.
+ #
+ # If +tls+ is true, enable TLS. The default is false.
+ # If +starttls+ is :always, enable STARTTLS, if +:auto+, use STARTTLS when the server supports it,
+ # if false, disable STARTTLS.
+ #
# If +tls_verify+ is true, verify the server's certificate. The default is true.
# If the hostname in the server certificate is different from +address+,
# it can be specified with +tls_hostname+.
#
# Additional SSLContext params can be added to +ssl_context_params+ hash argument and are passed to
@@ -476,28 +496,29 @@
# * Net::ReadTimeout
# * IOError
#
def SMTP.start(address, port = nil, *args, helo: nil,
user: nil, secret: nil, password: nil, authtype: nil,
+ tls: false, starttls: :auto,
tls_verify: true, tls_hostname: nil, ssl_context_params: nil,
&block)
raise ArgumentError, "wrong number of arguments (given #{args.size + 2}, expected 1..6)" if args.size > 4
helo ||= args[0] || 'localhost'
user ||= args[1]
secret ||= password || args[2]
authtype ||= args[3]
- new(address, port).start(helo: helo, user: user, secret: secret, authtype: authtype, tls_verify: tls_verify, tls_hostname: tls_hostname, ssl_context_params: ssl_context_params, &block)
+ new(address, port, tls: tls, starttls: starttls, tls_verify: tls_verify, tls_hostname: tls_hostname, ssl_context_params: ssl_context_params).start(helo: helo, user: user, secret: secret, authtype: authtype, &block)
end
# +true+ if the SMTP session has been started.
def started?
@started
end
#
# :call-seq:
- # start(helo: 'localhost', user: nil, secret: nil, authtype: nil, tls_verify: true, tls_hostname: nil, ssl_context_params: nil) { |smtp| ... }
+ # start(helo: 'localhost', user: nil, secret: nil, authtype: nil) { |smtp| ... }
# start(helo = 'localhost', user = nil, secret = nil, authtype = nil) { |smtp| ... }
#
# Opens a TCP connection and starts the SMTP session.
#
# === Parameters
@@ -508,19 +529,11 @@
# If both of +user+ and +secret+ are given, SMTP authentication
# will be attempted using the AUTH command. +authtype+ specifies
# the type of authentication to attempt; it must be one of
# :login, :plain, and :cram_md5. See the notes on SMTP Authentication
# in the overview.
- # If +tls_verify+ is true, verify the server's certificate. The default is true.
- # If the hostname in the server certificate is different from +address+,
- # it can be specified with +tls_hostname+.
#
- # Additional SSLContext params can be added to +ssl_context_params+ hash argument and are passed to
- # +OpenSSL::SSL::SSLContext#set_params+
- #
- # +tls_verify: true+ is equivalent to +ssl_context_params: { verify_mode: OpenSSL::SSL::VERIFY_PEER }+.
- #
# === Block Usage
#
# When this methods is called with a block, the newly-started SMTP
# object is yielded to the block, and automatically closed after
# the block call finishes. Otherwise, it is the caller's
@@ -554,28 +567,26 @@
# * Net::SMTPUnknownError
# * Net::OpenTimeout
# * Net::ReadTimeout
# * IOError
#
- def start(*args, helo: nil,
- user: nil, secret: nil, password: nil, authtype: nil, tls_verify: true, tls_hostname: nil, ssl_context_params: nil)
+ def start(*args, helo: nil, user: nil, secret: nil, password: nil, authtype: nil)
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..4)" if args.size > 4
helo ||= args[0] || 'localhost'
user ||= args[1]
secret ||= password || args[2]
authtype ||= args[3]
if defined?(OpenSSL::VERSION)
- ssl_context_params = ssl_context_params ? ssl_context_params : {}
+ ssl_context_params = @ssl_context_params || {}
unless ssl_context_params.has_key?(:verify_mode)
- ssl_context_params[:verify_mode] = tls_verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
+ ssl_context_params[:verify_mode] = @tls_verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
end
if @tls && @ssl_context_tls.nil?
@ssl_context_tls = SMTP.default_ssl_context(ssl_context_params)
end
if @starttls && @ssl_context_starttls.nil?
@ssl_context_starttls = SMTP.default_ssl_context(ssl_context_params)
end
- @tls_hostname = tls_hostname
end
if block_given?
begin
do_start helo, user, secret, authtype
return yield(self)