Export mailbox data to a tab-delimited text file

Sep 02, '02 09:17:55AM

Contributed by: wfolta

A simple perl script, used in conjunction with find, can create an export file containing a tab-delimited list of the From, Subject, and Date fields of all emails in Mail. You could bring this into a spreadsheet or database for further processing. Modifying the script (the formail parameters, mainly) would allow you to extract the message body and other fields as well.

Read the rest of the article for the script and find syntax...

Create this script, name it (mailexport.pl was used in this exmaple), make it executable (chmod 755 mailexport.pl), and keep it in the directory from which you'll run the find command:

#!/usr/bin/perl
$full_dir = $ARGV[0] ;
($mail_dir = $full_dir) =~ s|^.+?//|| ;
$mail_dir =~ s|\.mbox/mbox|| ;
$mail_dir =~ s|/| : |g ;
$full_dir =~ s/ /\\ /g ;

open(MAIL, "cat $full_dir | formail -e -s formail -X Subject:
-X From: -X Date: -a Subject: -a From: -a Date: |") ;

$i = 0 ;
while (<MAIL>)
{
if (/^Date: (.+)/)
{ chomp ($date = $1) ; }

if (/^Subject: (.+)/)
{ chomp ($subject = $1) ; }

if (/^From: (.+)/)
{ chomp ($from = $1) ; }

if (++$i == 3)
{
print ("$mail_dir\t$from\t$subject\t$date\n") ;
$i = 0 ;
}
}

close(MAIL) ;
Note: The "open(Mail..." line has been split into two rows for readability; replace the line break with one space character.

Once you've prepared the script, you can export the chosen fields from all of your mailboxes by typing:
find ~/Library/Mail/Mailboxes/ -name mbox -exec ./mailexport.pl \
'{}' ';' > tab-delimited-list
Remember to execute the find from the directory containing the script, and change "mailexport.pl" to match whatever you named the script.

Comments (15)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020902091755147