Skip to content

Instantly share code, notes, and snippets.

@hadley
Created October 12, 2012 18:13
Show Gist options
  • Save hadley/3880626 to your computer and use it in GitHub Desktop.
Save hadley/3880626 to your computer and use it in GitHub Desktop.
# ruby imap.rb > mail.csv
require 'net/imap'
username = ''
password = ''
imap = Net::IMAP.new('imap.gmail.com','993',true)
imap.login(username, password)
# imap.list("", "*").map{|i| i.name}
imap.select('[Gmail]/All Mail')
# imap.search(["UNSEEN"]).count
def format_date(date)
"#{date.mday}-#{Date::MONTHNAMES[date.month]}-#{date.year}"
end
puts "from,to,all,direct,read,sent"
cur = Date.new(2007, 1, 1)
today = Date.today
while cur < today
cur = cur + 7
week = ["SINCE", format_date(cur), "BEFORE", format_date(cur + 7)]
# * number of messages recieved
all = imap.search(week).count
# * number of messages read (SEEN, day month year)
read = imap.search(week + ["SEEN"]).count
direct = imap.search(week + ["OR", "TO", "h.wickham@gmail.com", "TO", "hadley@rice.edu"]).count
# * number of messages sent
sent = imap.search(week + ["OR", "FROM", "h.wickham@gmail.com", "FROM", "hadley@rice.edu"]).count
puts "#{cur},#{cur+6},#{all},#{direct},#{read},#{sent}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment