pop3.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. def check(options)
  100. setup_connection(options, check: true)
  101. mails = @pop.mails
  102. Rails.logger.info 'check only mode, fetch no emails'
  103. content_max_check = 2
  104. content_messages = 0
  105. # check messages
  106. mails.each do |m|
  107. mail = m.pop
  108. next if !mail
  109. # check how many content messages we have, for notice used
  110. if !mail.match?(%r{(X-Zammad-Ignore: true|X-Zammad-Verify: true)})
  111. content_messages += 1
  112. break if content_max_check < content_messages
  113. end
  114. end
  115. if content_messages >= content_max_check
  116. content_messages = mails.count
  117. end
  118. disconnect
  119. {
  120. result: 'ok',
  121. content_messages: content_messages,
  122. }
  123. end
  124. def verify(options, verify_string)
  125. setup_connection(options)
  126. mails = @pop.mails
  127. Rails.logger.info 'verify mode, fetch no emails'
  128. mails.reverse!
  129. # check for verify message
  130. mails.first(2000).each do |m|
  131. mail = m.pop
  132. next if !mail
  133. # check if verify message exists
  134. next if !mail.match?(%r{#{verify_string}})
  135. Rails.logger.info " - verify email #{verify_string} found"
  136. m.delete
  137. disconnect
  138. return {
  139. result: 'ok',
  140. }
  141. end
  142. {
  143. result: 'verify not ok',
  144. }
  145. end
  146. def disconnect
  147. return if !@pop
  148. @pop.finish
  149. end
  150. def setup_connection(options, check: false)
  151. ssl = true
  152. if options[:ssl] == 'off'
  153. ssl = false
  154. end
  155. ssl_verify = options.fetch(:ssl_verify, true)
  156. port = if options.key?(:port) && options[:port].present?
  157. options[:port].to_i
  158. elsif ssl == true
  159. 995
  160. else
  161. 110
  162. end
  163. Rails.logger.info "fetching pop3 (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl})"
  164. @pop = ::Net::POP3.new(options[:host], port)
  165. # @pop.set_debug_output $stderr
  166. # on check, reduce open_timeout to have faster probing
  167. if check
  168. @pop.open_timeout = 4
  169. @pop.read_timeout = 6
  170. else
  171. @pop.open_timeout = 16
  172. @pop.read_timeout = 45
  173. end
  174. if ssl
  175. Certificate::ApplySSLCertificates.ensure_fresh_ssl_context
  176. @pop.enable_ssl((ssl_verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE))
  177. end
  178. @pop.start(options[:user], options[:password])
  179. end
  180. end