Created
May 31, 2016 08:32
-
-
Save gamecreature/2edf6389a2bf31fe48d41b1bb20d8e31 to your computer and use it in GitHub Desktop.
Find emailadresses in an Maildir directory tree and send them to STDOUT
This file contains hidden or 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
#!/usr/bin/env ruby | |
# | |
# EMAIL extractor from Maildir (mbox files) | |
# | |
# extracts all emailadresses from a IMAP directory tree | |
chars = %w{ | / - \\ } | |
def find_email_addresses(base_folder) | |
a=`find #{base_folder} -type d` | |
@emails=[] | |
a.split("\n").each do |folder| | |
Dir[folder+"/*"].each do |email| | |
next if File.directory?(email) | |
content = File.read(email,:encoding => 'iso-8859-1') | |
lines = content.scan(/^(From|To|Cc):(.+)$/).to_a.map{|o| o[1]}.flatten | |
next if lines.length == 0 | |
emails = lines.map { |line| line.scan(/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/).to_a[0] }.flatten | |
@emails += emails.flatten | |
end | |
end | |
@emails = @emails.compact.sort.uniq | |
puts @emails.sort.join("\n") | |
end | |
dir = ARGV[0] || '.' | |
if dir == '-h' | |
puts "Extracts alle e-mailadresses from an IMAP Maildir tree" | |
puts "and sends them to STDOUT " | |
puts "" | |
puts "Usage: extract.rb [path] " | |
exit | |
end | |
find_email_addresses(dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment