@@ -179,7 +179,7 @@ class SMTPUnsupportedCommand < ProtocolError
179179 # user: 'Your Account', secret: 'Your Password', authtype: :cram_md5)
180180 #
181181 class SMTP < Protocol
182- VERSION = "0.2.2 "
182+ VERSION = "0.3.0 "
183183
184184 Revision = %q$Revision$ . split [ 1 ]
185185
@@ -215,11 +215,23 @@ def SMTP.default_ssl_context(ssl_context_params = nil)
215215 # server. +port+ is the port to connect to; it defaults to
216216 # port 25.
217217 #
218+ # If +tls+ is true, enable TLS. The default is false.
219+ # If +starttls+ is :always, enable STARTTLS, if +:auto+, use STARTTLS when the server supports it,
220+ # if false, disable STARTTLS.
221+ #
222+ # If +tls_verify+ is true, verify the server's certificate. The default is true.
223+ # If the hostname in the server certificate is different from +address+,
224+ # it can be specified with +tls_hostname+.
225+ #
226+ # Additional SSLContext params can be added to +ssl_context_params+ hash argument and are passed to
227+ # +OpenSSL::SSL::SSLContext#set_params+
228+ #
229+ # +tls_verify: true+ is equivalent to +ssl_context_params: { verify_mode: OpenSSL::SSL::VERIFY_PEER }+.
218230 # This method does not open the TCP connection. You can use
219231 # SMTP.start instead of SMTP.new if you want to do everything
220232 # at once. Otherwise, follow SMTP.new with SMTP#start.
221233 #
222- def initialize ( address , port = nil )
234+ def initialize ( address , port = nil , tls : false , starttls : :auto , tls_verify : true , tls_hostname : nil , ssl_context_params : nil )
223235 @address = address
224236 @port = ( port || SMTP . default_port )
225237 @esmtp = true
@@ -230,10 +242,13 @@ def initialize(address, port = nil)
230242 @read_timeout = 60
231243 @error_occurred = false
232244 @debug_output = nil
233- @tls = false
234- @starttls = :auto
245+ @tls = tls
246+ @starttls = starttls
235247 @ssl_context_tls = nil
236248 @ssl_context_starttls = nil
249+ @tls_verify = tls_verify
250+ @tls_hostname = tls_hostname
251+ @ssl_context_params = ssl_context_params
237252 end
238253
239254 # Provide human-readable stringification of class state.
@@ -417,7 +432,7 @@ def debug_output=(arg)
417432
418433 #
419434 # :call-seq:
420- # start(address, port = nil, helo: 'localhost', user: nil, secret: nil, authtype: nil, tls_verify: true, tls_hostname: nil, ssl_context_params: nil) { |smtp| ... }
435+ # 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| ... }
421436 # start(address, port = nil, helo = 'localhost', user = nil, secret = nil, authtype = nil) { |smtp| ... }
422437 #
423438 # Creates a new Net::SMTP object and connects to the server.
@@ -454,6 +469,11 @@ def debug_output=(arg)
454469 # or other authentication token; and +authtype+ is the authentication
455470 # type, one of :plain, :login, or :cram_md5. See the discussion of
456471 # SMTP Authentication in the overview notes.
472+ #
473+ # If +tls+ is true, enable TLS. The default is false.
474+ # If +starttls+ is :always, enable STARTTLS, if +:auto+, use STARTTLS when the server supports it,
475+ # if false, disable STARTTLS.
476+ #
457477 # If +tls_verify+ is true, verify the server's certificate. The default is true.
458478 # If the hostname in the server certificate is different from +address+,
459479 # it can be specified with +tls_hostname+.
@@ -478,14 +498,15 @@ def debug_output=(arg)
478498 #
479499 def SMTP . start ( address , port = nil , *args , helo : nil ,
480500 user : nil , secret : nil , password : nil , authtype : nil ,
501+ tls : false , starttls : :auto ,
481502 tls_verify : true , tls_hostname : nil , ssl_context_params : nil ,
482503 &block )
483504 raise ArgumentError , "wrong number of arguments (given #{ args . size + 2 } , expected 1..6)" if args . size > 4
484505 helo ||= args [ 0 ] || 'localhost'
485506 user ||= args [ 1 ]
486507 secret ||= password || args [ 2 ]
487508 authtype ||= args [ 3 ]
488- 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 )
509+ 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 )
489510 end
490511
491512 # +true+ if the SMTP session has been started.
@@ -495,7 +516,7 @@ def started?
495516
496517 #
497518 # :call-seq:
498- # start(helo: 'localhost', user: nil, secret: nil, authtype: nil, tls_verify: true, tls_hostname: nil, ssl_context_params: nil ) { |smtp| ... }
519+ # start(helo: 'localhost', user: nil, secret: nil, authtype: nil) { |smtp| ... }
499520 # start(helo = 'localhost', user = nil, secret = nil, authtype = nil) { |smtp| ... }
500521 #
501522 # Opens a TCP connection and starts the SMTP session.
@@ -510,14 +531,6 @@ def started?
510531 # the type of authentication to attempt; it must be one of
511532 # :login, :plain, and :cram_md5. See the notes on SMTP Authentication
512533 # in the overview.
513- # If +tls_verify+ is true, verify the server's certificate. The default is true.
514- # If the hostname in the server certificate is different from +address+,
515- # it can be specified with +tls_hostname+.
516- #
517- # Additional SSLContext params can be added to +ssl_context_params+ hash argument and are passed to
518- # +OpenSSL::SSL::SSLContext#set_params+
519- #
520- # +tls_verify: true+ is equivalent to +ssl_context_params: { verify_mode: OpenSSL::SSL::VERIFY_PEER }+.
521534 #
522535 # === Block Usage
523536 #
@@ -556,25 +569,23 @@ def started?
556569 # * Net::ReadTimeout
557570 # * IOError
558571 #
559- def start ( *args , helo : nil ,
560- user : nil , secret : nil , password : nil , authtype : nil , tls_verify : true , tls_hostname : nil , ssl_context_params : nil )
572+ def start ( *args , helo : nil , user : nil , secret : nil , password : nil , authtype : nil )
561573 raise ArgumentError , "wrong number of arguments (given #{ args . size } , expected 0..4)" if args . size > 4
562574 helo ||= args [ 0 ] || 'localhost'
563575 user ||= args [ 1 ]
564576 secret ||= password || args [ 2 ]
565577 authtype ||= args [ 3 ]
566578 if defined? ( OpenSSL ::VERSION )
567- ssl_context_params = ssl_context_params ? ssl_context_params : { }
579+ ssl_context_params = @ ssl_context_params || { }
568580 unless ssl_context_params . has_key? ( :verify_mode )
569- ssl_context_params [ :verify_mode ] = tls_verify ? OpenSSL ::SSL ::VERIFY_PEER : OpenSSL ::SSL ::VERIFY_NONE
581+ ssl_context_params [ :verify_mode ] = @ tls_verify ? OpenSSL ::SSL ::VERIFY_PEER : OpenSSL ::SSL ::VERIFY_NONE
570582 end
571583 if @tls && @ssl_context_tls . nil?
572584 @ssl_context_tls = SMTP . default_ssl_context ( ssl_context_params )
573585 end
574586 if @starttls && @ssl_context_starttls . nil?
575587 @ssl_context_starttls = SMTP . default_ssl_context ( ssl_context_params )
576588 end
577- @tls_hostname = tls_hostname
578589 end
579590 if block_given?
580591 begin
0 commit comments