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

This isn't really specifically "Perl" help. My issues is that I'm automating some stuff. I'm using Net::Pop3 which works like a dandy other than the fact that once I start deleting messages from the server, my script breaks (currently it remembers the last one it read, then starts from that +1). It it up to message 200, and I delete 150-200, then get another 30 or so mails, it won't read them b/c it starts reading at 201.

Looking at the mails, the only thing I can think of that will definitely work (considering I selectively delete emails) is to just check the date & time of each mail. I would have to read each mail to do this, which seems horribly inefficient... or at least will be once there's enough mails on the server.

Is my approach to this just way off?

Replies are listed 'Best First'.
Re: Help w/ approach using Net::Pop3
by bart (Canon) on Jul 28, 2007 at 09:13 UTC
    POP3 provides a special (temporary?) id that stays unique and unchanged at least during the time of the session, even when you delete messages: the uidl. You can use that as the basis for a better tracking system for which messages you've read: with two uidl hashes, you have both the old and the new message number, and by inverting one hash
    %msgnum = reverse %uidl;
    you can map
    old message number -> uidl -> new message number
    or, in Perl:
    $new_msgnum = $msgnum2{$uidl1{$msgnum}};

    As I currently don't have a pop3 mailbox available for testing purposes, I can't really test it and the code is just pseudocode, just giving a general outline, and not real working code.

    But what I don't get... as long as the connection isn't closed, the messages aren't renumbered, or are they? That's how I remember it.

Re: Help w/ approach using Net::Pop3
by GrandFather (Saint) on Jul 28, 2007 at 08:17 UTC

    Pop3 servers allow access to messages by index ranging from 1 to n where there are n messages available. When messages are actually deleted (the deletes may be pending) the remaining messages will be reindexed.

    Options are: remember how many messages remain on the server after deletions and start at the index following that number, read the message headers only and check the message id's to determine if the messages are new or not (assuming you keep the id's for messages that have been dealt with) or (as you suggest) check the date and times of messages.


    DWIM is Perl's answer to Gödel
Re: Help w/ approach using Net::Pop3
by zentara (Cardinal) on Jul 28, 2007 at 11:34 UTC
    See how I did it at Tk-POP3-previewer. You select those to be deleted, and do the actual deletion before disconnecting.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Hi guys

      I have been out drinking so can't really dig into this now, but I do appreciate the direction. I will check this out in the morning when I should be a bit more sober.

      Bart: Right now, the session closes (the job is run via cron X times per day). I guess I should look into leaving it open and run like a daemon.
Re: Help w/ approach using Net::Pop3
by Ninthwave (Chaplain) on Jul 28, 2007 at 18:12 UTC
    If deleting multiple messages run from the highest to lowest to avoid reindexing problems.
    "No matter where you go, there you are." BB