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

Does anyone know of a way to only retrieve new e-mail messages if you're not deleting them from the server after you get them? Is there some unique identifier that I could use? I'm going to be writing a script, but it wont be running all the time, just once every few minutes.

Thanks, Dan

Replies are listed 'Best First'.
Re: POP3, only retrieving new messages
by THRAK (Monk) on Mar 30, 2001 at 20:39 UTC
    If you are using Net::POP3 you could use the uidl method to get a get a hash of the messages (the hash keys) and their unique identifiers (the hash values). Create a list in a file or DB table to store these unique ID. Each time you run your script have it get the ID's from the server and then compare these to those in your saved list. Foreach message ID that is not already in your save list, push it onto an array (@get_these). Now loop though this array and use the get method to pull only those you haven't already. After they messages have been retrieved (or in the same foreach loop) write them to your save list. You can read more about Net::Pop3 here.

    -THRAK
    www.polarlava.com
      Yes, that's what I did when I was writing my email app (just learning, with Pike/GTK :). I think that is how the other clients do it also.

      Without a module, it would connect, list messages, and foreach message, do a top [id] 0 (to just get the headers), then parse out the Message-ID: field. I then kept a list (in Pike, a multiset was easiest) of all msgid's I've seen already, and only download a message (retr) that I hadn't seen.

      Of course, the module would do most of the for you. You would just have to keep a list of the msgid's.

      The previous message said the same thing, I realize, but I wanted to reiterate/confirm it.

      Anyway...

      riffraff