See Also

User Contributed Notes 10 notes

When I was testing imap_headerinfo() with an e-mail that had multiple recipients (multiple e-mails in to to: and/or cc: field), I noticed that imap_headerinfo() was failing hard for me on PHP 5.2.10-2ubuntu6.4 with Suhosin-Patch 0.9.7 (cli).

Rather than providing me an array with each and every e-mail address listed in the to and/or cc fields, it was only providing me the first listed. This was disappointing.

I'm not entirely sure why this is, but if you loop through all of the messages in a mailbox, calling imap_header() each time, you can significantly increase performance by calling imap_headers() first.

The performance difference, as I have tested on several boxes, connecting to several different servers, is that the second code snippet ALWAYS takes 1/2 the time, if not less.

Perhaps it is because imap_headers() retrieves all of the messages on one connection, whereas imap_header() has to make a new fetch request for each message?? I'm not sure WHY it is faster if imap_headers() is called first, but I do know that it is, so I thought I'd pass on the knowledge. If anyone knows why this is, please let me know....

I did some testing on using imap_headerinfo() function and I am very confused with the results.

On small mailboxes, getting data for 30 messages takes about 0.5 secs. On mailboxes with approximately 500 messages it takes about 7 secs to retrieve data for the same number of messages.

Why would the size of mailbox had anything to do with the time needed to retrieve the header of the single email message? Mailboxes are on the same account.

Next, If you measure every call to imap_headerinfo() function the result are even stranger! First and then every 22th call to the imap_headerinfo() function takes 10000 times more that the others. Example: the first call takes about 0.39 secs, then other 20 calls take about 0.0001 secs, then 22th call takes about 0.47 secs, then other 20 calls about 0.00004, and so on.

After some more research there is something else that came up.

If you use:

$message_header[$i] = imap_headerinfo($mbox, $i + 1);

it takes about 0.4 secs for every 22th call and about 0.0001 sec for other calls.

However, you would expect the same results with:

$message_header[$i] = imap_headerinfo($mbox, 30 - $i);

But, in this case it takes about 0.2 secs for every call!

The only difference here is that in the second example headers are retrieved in the reversed message order (from the 30th to the 1st) and for some reason it greatly affects the time needed for the operation. Strange, or is it just me?

When i try to access the same mailbox through pop3 and through imap i get different update infomations.

1. Using imap header->udate is not the same as strtotime($header->date)<?php$mBox = imap_open("{host:143/imap/novalidate-cert}INBOX}", $username, $password); // open as imap$header = imap_header($mBox, 1); // get first mails headerecho $header->udate;echo strtotime($header->date);?>

1. Using pop3header->udate is the same as strtotime($header->date)<?php$mBox = imap_open("{host:110/pop3/novalidate-cert}INBOX}", $username, $password); // open as pop3$header = imap_header($mBox, 1); // get first mails headerecho $header->udate;echo strtotime($header->date);?>

I do not know why this is so, but for this reason, i use strtotime($header->date) together with other values to create my own message-UID because this seems to work independent from the protocol.

I typically use UID's to identify messages, and recently discovered that the headers I had been pulling using this function and a message-number didn't match the UID's. Instead of worrying about it, I just began using imap_fetchheader() and imap_ rfc822_ parse_ headers() on its output. The only significant difference I immediately noticed was that there is no "udate" property, so I assigned one with the result of strtotime() on the 'date' property.