Question for a "super-man"!! (pop3, mails...)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi everybody,

I just would like to download the new mails I received on my POP3 server
(and keep a copy on the server)
I use PHP 4.3 and IMAP functions (they are compatible with both POP3 and
IMAP servers).

When I use OUTLOOK, no problem, in less than 5 seconds, it  answers me:
" you have XX new messages"

But, I can't manage to do the same thing within the same time in PHP!!!

I believe it's a IMAP functions problem.

First, I thought that each new mail was the last message returned by the
IMAP functions.
For exemple, when I connect to the POP3 servers, I get the total number of
Let's say I have 600 messages, the 600th messages is supposed to be the last
So, here is the process:
1- X=total number of messages
2- I download the Xth message
3- Is it a NEW message?
    YES-> download it, X=X-1, then go to step 2
    NO -> Go to step 4
4- End

So, if I have already downloaded the 600th message, obviously, i know I have
already dowloaded ALL the mails, and I can say immediately "no new message".

BUT, this is wrong with some servers (like
They do not sort the emails like this!!
If I send a new mail to myself, it will not be the last message!!! It will
appear for example in the middle of the list!!!!!!!!!!!!!!!!!!!!!!

So the only solution is:
1- Download the whole list of emails!!!!!!!!
2- compare EACH mail on the server with the locals mails!!
3- If the mail on the server is not on my local machine, then I download it

Suppose I have no new mail, I read 600 mails for nothing!!
So it takes more than 1 minute just to say: "no mail"!

Do you have another idea?

Some important points with POP3:
- no uid!! Even the field MESSAGE_ID is not usedul. Sometimes it is empty!!
- udate is sometimes empty!! (0)
- date can have several formats
- when you use the SORT imap function (or search) it download the whole
list!! So it makes a lot of time!!

How can OUTLOOK know the number of new mail in less than 5 seconds (even
with OVH)????
Can I do the same thing with PHP scripts?????

thanks for your help


Re: Question for a "super-man"!! (pop3, mails...)

WebRod wrote:
Quoted text here. Click to load it

Nope, this ain't the case. First thing to remember with POP3 is that
you can *never* assume the messages are in the same order on two
subsequent connections - the standard doesn't specify the order in any

Quoted text here. Click to load it

Yes. And there's actually more than one way to accomplish that :)

Quoted text here. Click to load it
Quoted text here. Click to load it

As I said, you can't count on this.

Quoted text here. Click to load it

This isn't actually the case. POP3 has extended command UIDL, which
returns you a list of unique identifiers (just like LIST returns the
sizes of messages). After this, you only need to check the UID with
stored UIDs of the messages you've already downloaded. This is quite
fast even with thousands of messages.

The downside is that not all servers provide UIDL-command since it's
not required by the standard. If this is the case, the only solution
is to download message headers (using command like TOP) only and
comparing them to headers already received. This takes somewhat longer
(but not as long as it'd take to download the whole messages) and
isn't as reliable as using UIDs.

Quoted text here. Click to load it

For example (O=Outlook, S=Server, indented for readability):

S: +OK Welcome
O: USER example
S: +OK
O: PASS example
S: +OK
S: +OK List follows
   1 abcdef
   2 abcdeg
   3 abcdeh
   4 abcdei
   5 abcdej

Re: Question for a "super-man"!! (pop3, mails...)

many thanks for all your messages.
Actually, irelaised that is should not try to do that with imap functions.

I now use POP PEAR functions and it does exactly what you are saying.

So many thanks.
The problem with this function is that the mails are not formatted.
I get some text like this:
Anyway, i use mb_decode_mimeheader and the problem is solved.
A last one, sometimes i get "_" instead of spaces!!
Before or even after i use mb_decode_mimeheader.

Any idea?

many thanks!

Quoted text here. Click to load it

Re: Question for a "super-man"!! (pop3, mails...)

One more thing... the user contribution at isn't too good, put
it gives good pointers on how to proceed. To be honest, I don't
believe it works to begin with - fgets shouldn't be used on a protocol
like POP3 where we can't know beforehand how much data we should
receive :(

Markku Uttula

Re: Question for a "super-man"!! (pop3, mails...)

Markku Uttula wrote:
Quoted text here. Click to load it

Oops - my bad... fgets only does reading up to newline. Sorry to bash
the user contribution.

Markku Uttula

Site Timeline