imap.rb 831 B

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