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

hello monks, I wish I could delete a specific message (really too big) in a mail box. how can one do that with perl. I've tried edit in a while (<>) loop but I don't know when the message end (there is attachment). it's a mbox on linux system and the problem is that there is too big message for users to download. Thanks for your expertise.

Replies are listed 'Best First'.
Re: mail box cleanup
by Fletch (Bishop) on Jan 22, 2004 at 22:22 UTC
    die "Whaaaaa?" if $! eq POSIX::EQUESTIONTOOVAGUE;

    Define specific message in a mail box. A UNIX mbox format file? An IMAP mailbox? Have you looked at Mail::Box or Mail::Internet?

      as you recommend I look Mail::Box , but there is problems with test failed when installing. its complex for me.

        Your question is a little confusing. Is it a mbox folder on a local machine or a folder on a remote machine?

        In case of the former, you may not want to process each message line by line, to be fast. The messages are separated by lines starting with 'From '. But how do you know which messages to delete? Using MailBox, you could write

        script: while(my $msg = Mail::Message->read(\*STDIN)) { $msg->write if $want_to_keep_it; } ./script <large >smaller

        That's not the best way to process folders, but most memory preserving.

        In case of a remote folder, you can open it using MailBox. It is a lazy implementation: only the messages you touch will be dowloaded. If you only touch the header, then only the header is downloaded. Even for large folders, memory consumption may stay in limits.

        Sometimes, you should process a file in batches: download and process a few, than reopen the folder and process another few. In any case, if you have huge sets of data, your code become more complicated.

        Concluding: as author of MailBox, I didn't receive any complaints about installation or test problems from you. That means that the (maybe) misfunction of the module is your problem! Make it my problem!

Re: mail box cleanup
by CountZero (Bishop) on Jan 22, 2004 at 22:40 UTC
    If you access your mail-box through a mail-reader / mail server, there are functions to delete a specific mail without downloading it. No need to use Perl for that.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: mail box cleanup
by duff (Parson) on Jan 22, 2004 at 22:58 UTC

    Hmm... I don't know why exactly you're not using something like mutt to delete the message manually, but I'll guess that's because you've got a bunch of users all with some spam that's huge. As a sysadmin I wouldn't delete anything from a users mailbox without their permission first though because there's a large chance you could screw something up :)

    Shop safety warning aside, a tool that might prove useful is formail (particularly with the -s option). It's probably even already installed on your linux box as it comes with many distributions. You can then use perl to just decide which messages to keep and which to throw away.

      there is a lot of users
      I have their permission
      its not spam its a wrong sending to everyone. So I thing the case is an accident not a everyday use.
Re: mail box cleanup
by zentara (Cardinal) on Jan 23, 2004 at 17:14 UTC
    Tell your user to get something like this: Poppy

    It will let him log on to his mail server, get the header info, and he can delete the big file without downloading it.