Created
February 14, 2013 22:07
-
-
Save thejefflarson/4956849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'mail' | |
Dir["./**/*.mbox"].each do |file| | |
puts "processing #{file}" | |
dir = File.basename file | |
puts "placing into #{dir}" | |
messages = File.read(file).split(/^From .*$/).reject{|m| m.blank? }.map {|m| Mail.read_from_string m } | |
puts "found #{messages.length} messages" | |
messages.each_with_index do |m, i| | |
puts " message #{i+1} of #{messages.length}" | |
filename = "#{file}.#{i}.txt" | |
puts " writing to #{filename}" | |
File.open(filename, 'w+b') do |f| | |
f.write "CLEAN TEXT: \n" | |
f.puts "Subject: #{m.subject.to_s}" | |
f.puts "From: #{m.from.to_s}" | |
f.puts "To: #{m.to.to_s}" | |
f.puts "Date: #{m.date.to_s}" | |
f.puts | |
if m.multipart? | |
f.puts m.text_part.decoded.to_s | |
else | |
f.puts m.body.to_s | |
end | |
f.puts "\n\nRAW SOURCE: \n" | |
f.puts m.to_s | |
end | |
m.attachments.each_with_index do |attach, a| | |
puts " attachment #{a+1} of #{m.attachments.length}" | |
File.open("#{file}.#{i}.#{attach.filename}", 'w+b') {|f| f.write attach.body.decoded } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment