Email formatting

ok, does anyone know how i can strip out fields i dont want from a mail spool file (eg: /var/mail/usermailbox) and dump to standard output (or file with > filename) ?? i tried using a bunch of grep -v 's but i realized that has two main problems, first of all, if anyone types the text im grepping out it will get removed, so i could tell grep to do it only once with -m 1 and so on, but there is going to be many emails in the mailbox.

so what i need is something like this:

From joe@farragutmarine.com  Tue Nov  4 22:34:54 2003
Return-Path: <joe@farragutmarine.com>
Delivered-To: main@farragutmarine.com
Received: from 192.168.1.103 (dynip4 [192.168.1.103])
        by farragutmarine.com (Postfix) with ESMTP id 0CD261700B
        for <main@farragutmarine.com>; Tue,  4 Nov 2003 22:34:54 -0500 (EST)
From: joe@farragutmarine.com
To: main@farragutmarine.com
Subject: This is a test
Date: Tue, 4 Nov 2003 23:12:40 -0500
User-Agent: KMail/1.5.1
MIME-Version: 1.0
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Message-Id: <200311042312.40075.joe@farragutmarine.com>

This is a test, lets see if i can get this thing working.

now please keep in mind that there will be multiple emails in the mailbox. the fields in BOLD are the fields id like to keep.

i dont expect anyone to write out a whole script for me, just someone to point me in the right direction.

thanks !

this is from one of the guys off the chicago perl mailing lists.

you might want to take a
look at Mail::Box [2] on CPAN. Once you've got installed you can interact with 
all sorts of mail folders (mbox, maildir, imap4, pop) with a common interface.

Assuming you've got an mbox format file at /home/bgates/mbox, you could:

    use Mail::Box::Mbox;

    my $folder = Mail::Box::Mbox->new( folder => '/home/bgates/mbox' );

    foreach my $msg ( $folder->messages() ) { 
	print $msg->body(),"\n";
    }

Lazy, and nice to read -- at least for me :) You can do all sorts of 
crazy stuff like deleting, modifying, searching. Well worth a look at
if you need to do lots of work with mail messages.

//Ed

that looks really great if it does what i think it does !! [EDIT: it does! ]
i will post back what i come up with, and thanks for the help O.P.!! :slight_smile:

ok this is doing exactly what i want! thanks again Optimus_P

this is what i came up with, with only one problem which ill show you below.

#!/usr/bin/perl

use Mail::Box::Mbox;

my $folder = Mail::Box::Mbox->new( folder => '/home/joe/mbox' );
my $head = Mail::Message::Head->new;

foreach my $msg ( $folder->messages() )
{
        print "<b>", $msg->replyPrelude($head->get('From')),"</b>";
        print "<b>",$msg->subject(),"</b> \n";
        print $msg->body(),"\n";
}
mbox.pl lines 1-14/14 (END)

however one thing i cant figure out just yet is why $msg->replyPrelude($head->get('From')) only produces output like this :
On Thu Oct 13 04:54:34 1995, someone wrote:
instead of
On Thu Oct 13 04:54:34 1995, username@domain.com wrote:

the documentation at http://perl.overmeer.net is excellent for this module BTW.

hmm one more thing, do you see anyway to reverse the order of the output? not simply outputing the last lines first, but starting with the last (latest message) then working its way up.

sorry i cant assist you on this one norsk.

in all honesty i have pretty much forgotten all my perl (currently relearning it) since i dont use it offten.

but if you goto http://www.pm.org/ you can find a local perl group mailing list, i am sure they can assist you on anything perl related.

since there are 2 "From" fields. try putting a space after the "From " or use the other from field "From: ". but useing just 1 from would give it to many arguments possably?

maybe. ill give it a try. that does exactly what i want it to, with the added benefit of being able to add in html formatting so i can put some nice looking tables in there and what not :smiley: .

but i wanted to say thanks again because i wouldnt have figured that out without your help.