Skip to content

Instantly share code, notes, and snippets.

@kbuckler
Created June 12, 2009 21:13
Show Gist options
  • Save kbuckler/128925 to your computer and use it in GitHub Desktop.
Save kbuckler/128925 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'net/imap'
require 'date'
require 'rubygems'
require 'active_support/core_ext/array/grouping'
include ActiveSupport::CoreExtensions::Array::Grouping
mailbox = "Inbox/Cron"
host = ''
port = 143
username = ''
password = ''
conn = Net::IMAP.new(host, port)
conn.login(username, password)
conn.select(mailbox)
date = (Date.today - 10).strftime("%e-%b-%Y")
msg_ids = conn.search(["SUBJECT", "Cron", "SINCE", date ])
msg_ids.reverse!
puts "Found #{msg_ids.length} messages in #{mailbox} since #{date}"
puts "First subject line: #{conn.fetch(msg_ids.first, "BODY[HEADER.FIELDS (SUBJECT)]")}"
flags = [:Seen, :Deleted]
msg_ids.in_groups_of(200) do |msg_group|
conn.store(msg_group.compact, "+FLAGS", flags)
conn.expunge
end
conn.expunge
conn.logout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment