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

Honorable Monks,

I have been reading thru the Monastery, searching CPAN, and have downloaded almost every Mail box or mail parsing module I could find.

All I need to do is rip thru a UNIX mailbox, grab all the mail, check for atttachments, then based on a code in the subject line, drop the baby into the right sandbox.

My issue is I can't make heads or tails out of the <rant on> extremely poor PODs <rant off>. Nothing in the documentation is clear at all. I've spent the past 3 days and not 1 single line of code that can be used.

I believe that the "best" module to use is MIME::Tools; however I can't seem to generate anything for more than the first mail message - the others just sit there untouched. How do I rip through a mailbox using this module?

Another question - can I delete the message from the mailbox after I am finished with it?

I've added seriously to the grey hair on my head over this one! In the time I spent looking for a module and then getting it to work I could have hacked this myself. grrrrrr.

Any and all help is more that greatly appreciated. Much Thanks in advance for any help anyone can be.

Replies are listed 'Best First'.
Re: UNIX Mail Box and Mail Parsing
by crouchingpenguin (Priest) on May 21, 2003 at 21:44 UTC

    Have you looked at Mail::Box ? It does all you want and more.

    #!/usr/bin/perl use strict; use warnings; use Mail::Box::Manager; my $manager = Mail::Box::Manager->new(); # source folder my $source_folder = $manager->open( folder => '/path/to/folder' ) or die $!; # two destinations my $destination_1 = $manager->open( folder => '/path/to/destination/1', create => 1, access => 'rw', ) or die $!, my $destination_2 = $manager->open( folder => '/path/to/destination/2', create => 1, access => 'rw', ) or die $!; foreach my $msg ( $source_folder->messages() ){ if( $msg->isMultipart() ){ # msg has attachments } if( $msg->subject() =~ m/some_flag/ ){ # do something with msg # $msg->copyTo($destination_1); # or $manager->copyMessage($destination_2, $msg ); } } $manager->closeAllFolders(); 1;

    It has great documentation as well!


    cp
    ----
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
      I'm sold ! I didn't look at that module - but you are right - using this will cure my issues and provide the solution that I want to provide.

      Much thanks and I'm very glad to be part of perlmonks.com - I hope that one day - I too will be able to provide such valuable knowledge to those that need it.

      -- draconis

Re: UNIX Mail Box and Mail Parsing
by artist (Parson) on May 21, 2003 at 21:01 UTC
    Hi draconis

    Have you tried Mail::MboxParser ?

    Here is the sample code from the module

    my $mb = Mail::MboxParser->new('some_mailbox', decode => 'ALL'); # ----------- # slurping for my $msg ($mb->get_messages) { print $msg->header->{subject}, "\n"; $msg->store_all_attachments('/tmp'); } # iterating while (my $msg = $mb->next_message) { print $msg->header->{subject}, "\n"; # ... }
    artist
      I did look at that; however, I need to be able to delete the mail after it is processed. From what I understand - Mail::MboxParser is read-only to the mailbox.

      Am I mistaken?

        Okay !

        I can use this - it's read only but so what ! I can FLOCK the file before I do anything, extract the attachments, unlink the file and then remove the FLOCK.

        Thanks !