smtp.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, @ssl_context_tls) : s)
  15. check_response critical { recv_response() }
  16. do_helo helo_domain
  17. if ! tls? and (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, @ssl_context_starttls))
  24. # helo response may be different after STARTTLS
  25. do_helo helo_domain
  26. end
  27. #
  28. # ADD auto detection of authtype - https://github.com/zammad/zammad/issues/240
  29. #
  30. # set detected authtype based on smtp server capabilities
  31. if user or secret
  32. if !authtype
  33. if auth_capable?(DEFAULT_AUTH_TYPE)
  34. authtype = DEFAULT_AUTH_TYPE
  35. elsif capable_cram_md5_auth?
  36. authtype = :cram_md5
  37. elsif capable_login_auth?
  38. authtype = :login
  39. elsif capable_plain_auth?
  40. authtype = :plain
  41. end
  42. end
  43. end
  44. #
  45. # /ADD auto detection of authtype - https://github.com/zammad/zammad/issues/240
  46. #
  47. authenticate user, secret, (authtype || DEFAULT_AUTH_TYPE) if user
  48. @started = true
  49. ensure
  50. unless @started
  51. # authentication failed, cancel connection.
  52. s.close if s
  53. @socket = nil
  54. end
  55. end
  56. end
  57. end
  58. # rubocop:enable all