Perl Mailsplit

I read and post mail with Netscape, and divide mail up into different folders. That's easy to do for incoming mail; I just drag it where I want it when I'm done. It should be just as easy for outgoing, but I usually forget to open up "Sent" to drag stuff out, so I end up with thousands of messages in Sent.

I finally decided to do something about it, and this is the result.


Hate these ads?

Mailsplit.pl

Mail files begin with a line that says "From ". Everything after that belongs to that message until you find another "From ". This format is true for Netscape and many other mail handling programs.

Netscape also creates another relaed file to handle sorting and deleting, but it watches the date stamps and will recreate the other file if you mess with the original as we are doing here. After adding messages, the folder will appear with "???" until you click on it; Netscape recreates its summary file then (Note that you want Netscape mail CLOSED while doing the splitting).



#!/usr/bin/perl
open(INDEX,"index");
# "index" is a file that contains lines that look like this:
#       To: "vick@vscacs.vsc.edu" <vick@vscacs.vsc.edu>|Vermont
#       To: Raven <Raven@aol.com>|Personal
#       To: Richard MacNevin <richmac@clear.net.nz>|Mac
#

# It was produced by grepping "^To:" from the "Sent" file,
# discarding duplicates with "sort -u" and then adding 
# the "|File" to indicate where I ultimately want to file
# the messages.
# I did this only for the few dozen addresses I want and discarded
# the rest of them.
#
while (<INDEX>) {
        chomp;
        ($who,$file)=split /\|/;
        $who =~ s/To: .*<(.*)>/$1/;
        $who =~ s/To: (.*)/$1/;
        $who = lc($adds);
        # this all just gets the address down to a simple "xxx@yyy"
        if (! $file) {
                $file = "Misc" 
                };
        # I didn't bother with a lot of the addresses, so we need to
        # default them
        $myfiles{$who}=$file;
        #create the hash
}
chdir "/usr/tony/nsmail"
# obviously this varies
open(MAIL,"Sent");
$mesg="";
$inaddr=0;
while(<MAIL>) {
# this won't be true until we hit the second "From " that
# indicates the beginning of a message
        if (/^From / && $inaddr) {
        # when it is true (we have a new message)
        # print out the old and close the file
          print MYFILE "$mesg";
          close MYFILE;
          $mesg=$_;
          $inaddr=0;
          next;
        }



        $mesg .= $_;
        # accumulate lines
        # I've seen people read files like this in paragraph
        # mode, but that won't work for mail files because
        # the end of one message does not necessarily have a blank line







        
        next unless /^To: /m;
        # now we have the "To:" line.
        chomp;
        $adds=$_;
        $adds =~ s/To: .*<(.*)>/$1/;
        $adds =~ s/To: (.*)/$1/;
        $adds = lc($adds);
        # make it look like the index
        $inaddr=1;
        # set this so when we next reach "From: " we will write.
        $outfile=$myfiles{$adds};
        if (! $outfile) {
           # "index" doesn't contain all the addresses that might be in "Sent"
           $outfile="Misc";
           }
                open(MYFILE,">>$outfile");


                
}
# at the very end there is left over stuff to write
print MYFILE "$mesg";
close MYFILE;


Download:




(C) November 1998 Anthony Lawrence




ad

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner



Views for this page
Today This Week This Month This Year  Overall
242553 2,501

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

pavatar.jpg
More:
       - Programming
       - Mail
       - Perl
       - Code




Unix/Linux Consultants


larryi@ccamedical.com SCO OS5, Debian Linux, RedHat Linux, MySQL, Apache, AJAX development using dXport/dL4/Unibasic, Windows Connectivity, Sharing Resouces, Automation, Shell Scripting


http://thatitguy.com Business networking servers, Linux and Unix experts. In business since 1997! Windows and Exchange to Samba and Scalix migration experts.


http://bcstechnology.net Full service Linux & UNIX systems integrator; Windows to UNIX/Linux Client-Server Specialist; Secure E-Mail & Website Hosting; Thoroughbred Software Developer; Custom Industrial Automation; Hardware & Electronics Experts; In Business Since 1985.



Twitter
  • Nov 30 20:25
    I have 37,000 words of a 50,000 word project. I'd like to finish it this week..
  • Nov 30 20:05
    My wife made turkey sandwiches with stuffing and cranberry orange relish - I did not want to eat the last bite. Didn't want it to end!









Change Congress


Related Posts