Read mbox contents


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/env ruby -w
  2. #
  3. # this will only work in 1.8.7, since mailread.rb is not present after that
  4. # supply the path of any mbox file such as ~/mbox or ~/mail/read-mail
  5. # and see a listing of mails, then give a msg number and see body
  6. require 'mailread'
  7.  
  8. BOLD = "\e[1m"
  9. CLEAR = "\e[0m"
  10.  
  11. MAILBOX = ARGV[0] || "mbox"
  12. mbox = File.open(MAILBOX)
  13. count = lines = 0
  14. # array of mails
  15. mails = []
  16. # read up the warning message, we don't want it in our array
  17. msg = Mail.new(mbox)
  18. while !mbox.eof?
  19. msg = Mail.new(mbox)
  20. count += 1
  21. m = msg.header
  22. printf("%2d : %2s [%15s] %s\n", count, m['Status'], m['From'], msg.header['Subject'])
  23. mails << msg
  24. end
  25. mbox.close
  26. #puts mails.size
  27.  
  28. # ask user for a number and print body for that
  29. while true
  30. print "Enter a mail number [1 to #{mails.size}]:"
  31. n = STDIN.gets.chomp
  32. break if n.nil? || n.empty?
  33. msg = mails[n.to_i-1]
  34. body = msg.body
  35. puts
  36. string= "#{msg.header['Subject']}"
  37. puts "#{BOLD}#{string}#{CLEAR}"
  38. puts "-" * string.length
  39. puts body
  40. end
  41.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.