pop3.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'net/pop'
  3. class Channel::Driver::Pop3 < Channel::Driver::BaseEmailInbound
  4. =begin
  5. fetch emails from Pop3 account
  6. instance = Channel::Driver::Pop3.new
  7. result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)
  8. returns
  9. {
  10. result: 'ok',
  11. fetched: 123,
  12. notice: 'e. g. message about to big emails in mailbox',
  13. }
  14. check if connect to Pop3 account is possible, return count of mails in mailbox
  15. instance = Channel::Driver::Pop3.new
  16. result = instance.fetch(params[:inbound][:options], channel, 'check')
  17. returns
  18. {
  19. result: 'ok',
  20. content_messages: 123,
  21. }
  22. verify Pop3 account, check if search email is in there
  23. instance = Channel::Driver::Pop3.new
  24. result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)
  25. returns
  26. {
  27. result: 'ok', # 'verify not ok'
  28. }
  29. =end
  30. def fetch(options, channel)
  31. setup_connection(options)
  32. mails = @pop.mails
  33. # fetch regular messages
  34. count_all = mails.size
  35. count = 0
  36. count_fetched = 0
  37. too_large_messages = []
  38. active_check_interval = 20
  39. notice = ''
  40. mails.first(2000).each do |m|
  41. count += 1
  42. break if (count % active_check_interval).zero? && channel_has_changed?(channel)
  43. Rails.logger.info " - message #{count}/#{count_all}"
  44. mail = m.pop
  45. next if !mail
  46. # ignore verify messages
  47. if mail.match?(%r{(X-Zammad-Ignore: true|X-Zammad-Verify: true)}) && mail =~ %r{X-Zammad-Verify-Time:\s(.+?)\s}
  48. begin
  49. verify_time = Time.zone.parse($1)
  50. if verify_time > 30.minutes.ago
  51. info = " - ignore message #{count}/#{count_all} - because it's a verify message"
  52. Rails.logger.info info
  53. next
  54. end
  55. rescue => e
  56. Rails.logger.error e
  57. end
  58. end
  59. # do not process too large messages, instead download and send postmaster reply
  60. max_message_size = Setting.get('postmaster_max_size').to_f
  61. real_message_size = mail.size.to_f / 1024 / 1024
  62. if real_message_size > max_message_size
  63. if Setting.get('postmaster_send_reject_if_mail_too_large') == true
  64. info = " - download message #{count}/#{count_all} - ignore message because it's too large (is:#{real_message_size} MB/max:#{max_message_size} MB)"
  65. Rails.logger.info info
  66. notice += "#{info}\n"
  67. process_oversized_mail(channel, mail)
  68. else
  69. info = " - ignore message #{count}/#{count_all} - because message is too large (is:#{real_message_size} MB/max:#{max_message_size} MB)"
  70. Rails.logger.info info
  71. notice += "#{info}\n"
  72. too_large_messages.push info
  73. next
  74. end
  75. # delete email from server after article was created
  76. else
  77. process(channel, m.pop, false)
  78. end
  79. m.delete
  80. count_fetched += 1
  81. end
  82. disconnect
  83. if count.zero?
  84. Rails.logger.info ' - no message'
  85. end
  86. # Error is raised if one of the messages was too large AND postmaster_send_reject_if_mail_too_large is turned off.
  87. # This effectivelly marks channels as stuck and gets highlighted for the admin.
  88. # New emails are still processed! But large email is not touched, so error keeps being re-raised on every fetch.
  89. if too_large_messages.present?
  90. raise too_large_messages.join("\n")
  91. end
  92. Rails.logger.info 'done'
  93. {
  94. result: 'ok',
  95. fetched: count_fetched,
  96. notice: notice,
  97. }
  98. end
  99. # Checks if mailbox has anything besides Zammad verification emails.
  100. # If any real messages exists, return the real count including messages to be ignored when importing.
  101. # If only verification messages found, return 0.
  102. def check_configuration(options)
  103. setup_connection(options, check: true)
  104. mails = @pop.mails
  105. Rails.logger.info 'check only mode, fetch no emails'
  106. has_content_messages = mails
  107. .first(2000)
  108. .any? do |m|
  109. mail = m.pop
  110. mail.present? && !mail.match?(%r{(X-Zammad-Ignore: true|X-Zammad-Verify: true)})
  111. end
  112. disconnect
  113. {
  114. result: 'ok',
  115. content_messages: has_content_messages ? mails.count : 0,
  116. }
  117. end
  118. def verify(options, verify_string)
  119. setup_connection(options)
  120. mails = @pop.mails
  121. Rails.logger.info 'verify mode, fetch no emails'
  122. mails.reverse!
  123. # check for verify message
  124. mails.first(2000).each do |m|
  125. mail = m.pop
  126. next if !mail
  127. # check if verify message exists
  128. next if !mail.match?(%r{#{verify_string}})
  129. Rails.logger.info " - verify email #{verify_string} found"
  130. m.delete
  131. disconnect
  132. return {
  133. result: 'ok',
  134. }
  135. end
  136. {
  137. result: 'verify not ok',
  138. }
  139. end
  140. def disconnect
  141. return if !@pop
  142. @pop.finish
  143. end
  144. def setup_connection(options, check: false)
  145. ssl = true
  146. if options[:ssl] == 'off'
  147. ssl = false
  148. end
  149. ssl_verify = options.fetch(:ssl_verify, true)
  150. port = if options.key?(:port) && options[:port].present?
  151. options[:port].to_i
  152. elsif ssl == true
  153. 995
  154. else
  155. 110
  156. end
  157. Rails.logger.info "fetching pop3 (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl})"
  158. @pop = ::Net::POP3.new(options[:host], port)
  159. # @pop.set_debug_output $stderr
  160. # on check, reduce open_timeout to have faster probing
  161. if check
  162. @pop.open_timeout = 4
  163. @pop.read_timeout = 6
  164. else
  165. @pop.open_timeout = 16
  166. @pop.read_timeout = 45
  167. end
  168. if ssl
  169. Certificate::ApplySSLCertificates.ensure_fresh_ssl_context
  170. @pop.enable_ssl((ssl_verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE))
  171. end
  172. @pop.start(options[:user], options[:password])
  173. end
  174. end