Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to extract the header and body information from a Unix mbox one message at a time, do some edit checks on the header & body, return a message to the sender if errors are detected in the body, and delete the processed messages from the mailbox. I'm trying to use the Mail::Box module to accomplish this task; however being new to Perl using this module is over my head. I saw the module Mail-Mbox Parser on CPAN. Will this module accomplish what I need to do? I need some guidance! Should I try using one of the above mentioned modules or try writing a script to read the mbox head-on? Any guidance you can provide will be appreciated. Thanks. bod

Replies are listed 'Best First'.
Re: Parsing a Unix Mailbox
by jwest (Friar) on Jul 18, 2002 at 16:22 UTC
    I'd look into avoiding reading the mbox file to begin with. With a properly defined alias or .forward file, you could use a script using Mail::Procmail to intercept the email as it's being delivered.

    By doing so, you've eliminated all the headaches that go with handling the mbox file yourself. In addition, Mail::Procmail's pm_init method returns a Mail::Internet object, which will allow for easy manipulation of headers and bodies (and re-sending the email, as necessary).

    Of course, if you're not using any of the more advanced features of Mail::Procmail, you could eliminate your use of the module by creating your Mail::Internet object directly:
    my $mail = Mail::Internet->new(\*STDIN);

    Hope this helps!

    --jwest


    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
Re: Parsing a Unix Mailbox
by ides (Deacon) on Jul 18, 2002 at 16:16 UTC
    Yes it looks like Mail::MboxParser will do the trick for you. What you are going to need to do is use Mail::MboxParser::Mail's header function to return you a hash of all the header variables and Mail::MboxParser::Mail::Body to work with the body.

    You are probably going to have to write your own code to actually remove it from the file however, but Mail::MboxParser will make finding and checking each message much easier.

    -----------------------------------
    Frank Wiles <frank@wiles.org>
    http://frank.wiles.org