smtp.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 || secret || authtype
  7. check_auth_args authtype, user, secret
  8. end
  9. s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
  10. tcp_socket(@address, @port)
  11. end
  12. logging "Connection opened: #{@address}:#{@port}"
  13. @socket = new_internet_message_io(tls? ? tlsconnect(s, @ssl_context_tls) : s)
  14. check_response critical { recv_response() }
  15. do_helo helo_domain
  16. if ! tls? and (starttls_always? or (capable_starttls? and starttls_auto?))
  17. unless capable_starttls?
  18. raise SMTPUnsupportedCommand, "STARTTLS is not supported on this server"
  19. end
  20. starttls
  21. @socket = new_internet_message_io(tlsconnect(s, @ssl_context_starttls))
  22. # helo response may be different after STARTTLS
  23. do_helo helo_domain
  24. end
  25. #
  26. # ADD auto detection of authtype - https://github.com/zammad/zammad/issues/240
  27. #
  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_cram_md5_auth?
  34. authtype = :cram_md5
  35. elsif capable_login_auth?
  36. authtype = :login
  37. elsif capable_plain_auth?
  38. authtype = :plain
  39. end
  40. end
  41. end
  42. #
  43. # /ADD auto detection of authtype - https://github.com/zammad/zammad/issues/240
  44. #
  45. authenticate user, secret, (authtype || DEFAULT_AUTH_TYPE) if user
  46. @started = true
  47. ensure
  48. unless @started
  49. # authentication failed, cancel connection.
  50. s.close if s
  51. @socket = nil
  52. end
  53. end
  54. end
  55. end
  56. # rubocop:enable all