Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Cleaner but two-script version' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Cleaner but two-script version
Authored by: wfolta on Sep 02, '02 03:07:19PM

For those who want to build on the script, it would actually be better done as two scripts. The kludge with counting every three lines is ugly and may break.

So, with two files and the commandline:

find ~/Library/Mail/Mailboxes/ -name mbox \\
-exec ./mailbox1.pl '{}' ';' > tab-delimited-list

You can accomplish the same thing, but it's easier to then modify mailbox2.pl to get the particular fields you want to have.

(Note that mailbox1.pl doesn't really do much. The only reason it exists is because formail reads stdin and I can't figure a way to pipe or redirect input to formail as a part of a find or xargs command. If there were a way to do it, all the work could be done in mailbox2.pl.)

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

system("cat '$full_dir' | formail -e -X Subject: -X From: -X Date: -s ./mailbox2.pl '$mail_dir'") ;
====================

========== mailbox2.pl ==========
#!/usr/bin/perl
$mail_dir = $ARGV[0] ;

$date = "???" ;
$subject = "???" ;
$from = "???" ;

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

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

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

print ("$mail_dirt$fromt$subjectt$date\\n") ;
====================



[ Reply to This | # ]
Stupid backslashes!!
Authored by: wfolta on Sep 02, '02 03:28:19PM

When I previewed the routines, I had to double-up my backslashes for it to look right. But when it posted, they stayed doubled. Also, the backslash in front of greater-than and less-than need to go. Can the message be fixed by the admin?



[ Reply to This | # ]
Beginning of body extract code
Authored by: wfolta on Sep 02, '02 03:37:51PM

(I'll try without doubled backslashes to see what I get...)

If you modify mailbox2.pl, you can get the body text:

#!/usr/bin/perl
$mail_dir = $ARGV[0] ;

$date = "???" ;
$subject = "???" ;
$from = "???" ;

while (<STDIN>)
{
last if (/^$/) ;

if (/^Date: (.+)/)
{ chomp ($date = $1) ; }

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

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

$body = <STDIN> ;

print ("$mail_dir\t$from\t$subject\t$date\t", $body, "\n") ;

Note that the $body = line reads only a single line of the body. You'd have to read all the rest of the lines and filter them to handle tabs and newlines somehow, if you intend to output to tab-delimited format. If you'll use DB routines to go straight to a database, that wouldn't be a problem.



[ Reply to This | # ]
Also have to change mailbox1.pl
Authored by: wfolta on Sep 02, '02 03:40:43PM

Sorry for so many posts. Getting questions offline...

Need to supply the -k argument so mailbox2.pl gets the body:

cat '$full_dir' | formail -e -k -X Subject: ...



[ Reply to This | # ]