smtp.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # rubocop:disable all
  2. module Net
  3. class SMTP
  4. def do_start(helo_domain, user, secret, authtype)
  5. raise IOError, 'SMTP session already started' if @started
  6. if user or secret
  7. check_auth_method(authtype || DEFAULT_AUTH_TYPE)
  8. check_auth_args user, secret
  9. end
  10. s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
  11. tcp_socket(@address, @port)
  12. end
  13. logging "Connection opened: #{@address}:#{@port}"
  14. @socket = new_internet_message_io(tls? ? tlsconnect(s) : s)
  15. check_response critical { recv_response() }
  16. do_helo helo_domain
  17. if starttls_always? or (capable_starttls? and starttls_auto?)
  18. unless capable_starttls?
  19. raise SMTPUnsupportedCommand,
  20. "STARTTLS is not supported on this server"
  21. end
  22. starttls
  23. @socket = new_internet_message_io(tlsconnect(s))
  24. # helo response may be different after STARTTLS
  25. do_helo helo_domain
  26. end
  27. # ADD auto detection of authtype - https://github.com/zammad/zammad/issues/240
  28. # set detected authtype based on smtp server capabilities
  29. if user or secret
  30. if !authtype
  31. if auth_capable?(DEFAULT_AUTH_TYPE)
  32. authtype = DEFAULT_AUTH_TYPE
  33. elsif capable_plain_auth?
  34. authtype = 'PLAIN'
  35. elsif capable_login_auth?
  36. authtype = 'LOGIN'
  37. elsif capable_cram_md5_auth?
  38. authtype = 'CRAM-MD5'
  39. end
  40. end
  41. end
  42. # /ADD auto detection of authtype - https://github.com/zammad/zammad/issues/240
  43. authenticate user, secret, (authtype || DEFAULT_AUTH_TYPE) if user
  44. @started = true
  45. ensure
  46. unless @started
  47. # authentication failed, cancel connection.
  48. s.close if s and not s.closed?
  49. @socket = nil
  50. end
  51. end
  52. end
  53. end
  54. # rubocop:enable all