imap.rb 757 B

12345678910111213141516171819202122
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module ImapHelper
  3. def imap_delete_old_mails(options)
  4. imap = ::Net::IMAP.new(options[:host], options[:port], options[:ssl], nil, false)
  5. imap.authenticate(options[:auth_type], options[:user], options[:password])
  6. imap.select('INBOX')
  7. message_ids = imap.search(['BEFORE', 1.day.ago.to_date.strftime('%d-%b-%Y')])
  8. Rails.logger.debug { "#{message_ids.count} messages in INBOX will be deleted!" } if message_ids.count.positive?
  9. message_ids.each do |message_id|
  10. imap.store(message_id, '+FLAGS', [:Deleted])
  11. end
  12. imap.expunge if message_ids.count.positive?
  13. end
  14. end
  15. RSpec.configure do |config|
  16. config.include ImapHelper, integration: true
  17. end