This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Reading Mail with IMAP (written by John Wells)
CodeExpectedActual
require 'net/imap'
conn = Net::IMAP.new('mail.myhost.com', 143)
conn.login('username', 'password')
conn.search(['FROM', 'jabba@huttfoundation.org']).each do |sequence|
    fetch_result = conn.fetch(sequence, 'ENVELOPE')
    envelope = fetch_result[0].attr['ENVELOPE']
    printf("%s - From: %s - To: %s - Subject: %s\n", envelope.date, 
  	   envelope.from[0].name, envelope.to[0].name, envelope.subject)
end
Wed Feb 08 14:07:21 EST 2006 - From: The Hutt Foundation - To: You - Subject: Bwah!
Wed Feb 08 11:21:19 EST 2006 - From: The Hutt Foundation - To: You - Subject: Go to do wa IMAP
Error! (Exception?) Here's stdout:
Net::IMAP::NoResponseError: incorrect password or account name
	from /usr/lib/ruby/1.8/net/imap.rb:971:in `get_tagged_response'
	from /usr/lib/ruby/1.8/net/imap.rb:1022:in `send_command'
	from /usr/lib/ruby/1.8/monitor.rb:229:in `synchronize'
	from /usr/lib/ruby/1.8/net/imap.rb:1007:in `send_command'
	from /usr/lib/ruby/1.8/net/imap.rb:374:in `login'
	from (irb):3
Net::IMAP::BadResponseError: no mailbox selected
	from /usr/lib/ruby/1.8/net/imap.rb:973:in `get_tagged_response'
	from /usr/lib/ruby/1.8/net/imap.rb:1022:in `send_command'
	from /usr/lib/ruby/1.8/monitor.rb:229:in `synchronize'
	from /usr/lib/ruby/1.8/net/imap.rb:1007:in `send_command'
	from /usr/lib/ruby/1.8/net/imap.rb:1140:in `search_internal'
	from /usr/lib/ruby/1.8/monitor.rb:229:in `synchronize'
	from /usr/lib/ruby/1.8/net/imap.rb:1136:in `search_internal'
	from /usr/lib/ruby/1.8/net/imap.rb:675:in `search'
	from (irb):4
puts "#{conn.responses["RECENT"]} new messages, #{conn.responses["EXISTS"]} total"
10 new messages, 1022 total
new messages,  total
uids = conn.search(["FROM", "jabba@huttfoundation.org"]).collect do |sequence|
  fetch_result = conn.fetch(sequence, "UID")
  puts "UID: #{fetch_result[0].attr["UID"]}"
end
UID: 203
UID: 206
Error! (Exception?) Here's stdout:
Net::IMAP::BadResponseError: no mailbox selected
	from /usr/lib/ruby/1.8/net/imap.rb:973:in `get_tagged_response'
	from /usr/lib/ruby/1.8/net/imap.rb:1022:in `send_command'
	from /usr/lib/ruby/1.8/monitor.rb:229:in `synchronize'
	from /usr/lib/ruby/1.8/net/imap.rb:1007:in `send_command'
	from /usr/lib/ruby/1.8/net/imap.rb:1140:in `search_internal'
	from /usr/lib/ruby/1.8/monitor.rb:229:in `synchronize'
	from /usr/lib/ruby/1.8/net/imap.rb:1136:in `search_internal'
	from /usr/lib/ruby/1.8/net/imap.rb:675:in `search'
	from (irb):11
puts conn.uid_fetch(203, 'BODY[TEXT]')[0].attr['BODY[TEXT]']
class Net::IMAP
  def get_msg_info(msg_sequence_num)
    # code we used above
    fetch_result = fetch(msg_sequence_num, '(UID ENVELOPE)')
    envelope = fetch_result[0].attr['ENVELOPE']
    uid = fetch_result[0].attr['UID']
    info = {'UID'     => uid,
            'Date'    => envelope.date,
            'From'    => envelope.from[0].name,
            'To'      => envelope.to[0].name,
            'Subject' => envelope.subject}
  end
end
conn.search(['BEFORE', '01-Jan-2006', 
             'SINCE', '01-Jan-2000']).each do |sequence|
    conn.get_msg_info(sequence).each {|key, val| puts "#{key}: #{val}" }
end
...
Net::IMAP::BadResponseError: no mailbox selected
	from /usr/lib/ruby/1.8/net/imap.rb:973:in `get_tagged_response'
	from /usr/lib/ruby/1.8/net/imap.rb:1022:in `send_command'
	from /usr/lib/ruby/1.8/monitor.rb:229:in `synchronize'
	from /usr/lib/ruby/1.8/net/imap.rb:1007:in `send_command'
	from /usr/lib/ruby/1.8/net/imap.rb:1152:in `fetch_internal'
	from /usr/lib/ruby/1.8/monitor.rb:229:in `synchronize'
	from /usr/lib/ruby/1.8/net/imap.rb:1150:in `fetch_internal'
	from /usr/lib/ruby/1.8/net/imap.rb:712:in `uid_fetch'
	from (irb):15
Net::IMAP::BadResponseError: no mailbox selected
	from /usr/lib/ruby/1.8/net/imap.rb:973:in `get_tagged_response'
	from /usr/lib/ruby/1.8/net/imap.rb:1022:in `send_command'
	from /usr/lib/ruby/1.8/monitor.rb:229:in `synchronize'
	from /usr/lib/ruby/1.8/net/imap.rb:1007:in `send_command'
	from /usr/lib/ruby/1.8/net/imap.rb:1140:in `search_internal'
	from /usr/lib/ruby/1.8/monitor.rb:229:in `synchronize'
	from /usr/lib/ruby/1.8/net/imap.rb:1136:in `search_internal'
	from /usr/lib/ruby/1.8/net/imap.rb:675:in `search'
	from (irb):29