Skip to content

Instantly share code, notes, and snippets.

@mmaa
Created August 6, 2012 20:31
Show Gist options
  • Select an option

  • Save mmaa/3278180 to your computer and use it in GitHub Desktop.

Select an option

Save mmaa/3278180 to your computer and use it in GitHub Desktop.
def recent_messages(batch = 250, uid = nil)
get_connection.select('INBOX')
if uid
last_seen = get_connection.uid_fetch(uid, 'ENVELOPE').first.seqno
else
last_seen = get_connection.responses['EXISTS'].first
end
if last_seen > batch
mid_range = ((last_seen - batch)..last_seen)
else
mid_range = (0..last_seen)
end
envelope_data = get_connection.fetch(mid_range, ['ENVELOPE', 'UID'])
messages = []
envelope_data.each do |m|
uid = m.attr['UID']
m = m.attr['ENVELOPE']
messages << {
:date => Time.parse(m.date).to_i,
:from_address => [m.from.first.mailbox, m.from.first.host].join('@'),
:subject => m.subject,
:uid => uid
}
end
return messages
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment