imap_fetch_overview

(PHP 3 >= 3.0.4, PHP 4, PHP 5)

imap_fetch_overview --  Read an overview of the information in the headers of the given message

Description

array imap_fetch_overview ( resource imap_stream, string sequence [, int options] )

This function fetches mail headers for the given sequence and returns an overview of their contents. sequence will contain a sequence of message indices or UIDs, if flags contains FT_UID. The returned value is an array of objects describing one message header each:

  • subject - the messages subject

  • from - who sent it

  • to - recipient

  • date - when was it sent

  • message_id - Message-ID

  • references - is a reference to this message id

  • in_reply_to - is a reply to this message id

  • size - size in bytes

  • uid - UID the message has in the mailbox

  • msgno - message sequence number in the mailbox

  • recent - this message is flagged as recent

  • flagged - this message is flagged

  • answered - this message is flagged as answered

  • deleted - this message is flagged for deletion

  • seen - this message is flagged as already read

  • draft - this message is flagged as being a draft

例子 1. imap_fetch_overview() example

<?php
$mbox
= imap_open("{your.imap.host:143}", "username", "password")
     or die(
"can't connect: " . imap_last_error());

$overview = imap_fetch_overview($mbox, "2,4:6", 0);

if (
is_array($overview)) {
    foreach (
$overview as $val) {
        echo
"$val->msgno - $val->date - $val->subject\n";
    }
}

imap_close($mbox);
?>


add a note add a note User Contributed Notes
borg at diekippe dot de
26-Jul-2005 05:21
$overview    = imap_fetch_overview($mailbox,"1:$MN",0);
   $size        = sizeof($overview);
   sort($overview); // <---- to sort the mails
  
       foreach($overview as $key){
           $uid    = $key->msgno;
           $part    = imap_fetchstructure($mailbox,$uid);

   if(sizeof($part->parts) >0){
       $anhang = "<img src='../template_bilder/cms_admin/anhang.gif' border='0'>";
   }else{
       $anhang = "<img src='../template_bilder/cms_admin/keinanhang.gif' border='0'>";
   }

   if($key->seen == "0"  && $key->recent == "0"){
       $bold = "<b>";
       $close_bold = "</b>";
       $imagemail = '<table border="0" cellpadding="2" cellspacing="0"><tr><td valign="top" width="10">'.$anhang.'</td><td valign="top"  align="right"><img src="../template_bilder/cms_admin/mail0neu.gif" border="0"></td></tr></table>';
   }else{
       $bold        = "";
       $close_bold = "";
       $imagemail    = '<table border="0" cellpadding="2" cellspacing="0"><tr><td valign="top"  width="10">'.$anhang.'</td><td valign="top"  align="right"><img src="../template_bilder/cms_admin/gelesen.gif" border="0"></td></tr></table>';
}
aaron at gwmicro dot com
25-Jan-2005 10:08
In response to jeff at coded's statement that imap_fetch_overview() will only retrieve the overview for the main INBOX folder, I find that to be false. I can retrieve overviews of the main INBOX folder, as well as any sub-folder.

In other words, I can get an overview from:

$mbox = imap_open("{localhost}INBOX, $u, $p);

as well as from

$mbox = imap_open("{localhost}INBOX.Trash, $u, $p);
csabay at szv dot tsf dot hu
23-Sep-2004 07:06
imap_fetch_overview destroys the object to be returned by imap_headerinfo:

imap_headerinfo returns an object containing several arrays e.g. to[]. These arrays will be stumped to 1 of length.
jeff at coded dot com
18-Sep-2004 02:15
It appears that imap_fetch_overview() will only retrieve the overview for the main INBOX folder.  Correct me if I am wrong but as far as I can tell, there is no way to fetch an overview of any sub-folders (i.e. INBOX.Sent-Mail).
Group-Office Developer
10-Apr-2004 08:21
About the sequence sorting again.
I've found that there's a significant speed improvement by preparing the sequence and then fetch them at once with large mailboxes. On small mailboxes wyou will not notice a speed difference.

But then there's the sorting problem. I've spent all night fiuring out how to do this myself. I found the solution!

Prepare an array of messages with imap_sort. Create a second array that maps the message numbers to the actuall place in the fetched array.
You know it will be in numeric order so you can make an array map with the sort() function sorting it from high to low.
Then you can proces the array fetched with imap_fetch_overview with the array_map and you will have them sorted. If you need an example look in the Group-Office code classes/imap.class.inc in a version later then 2.04.
mails at slueoend dot ch
15-Dec-2003 08:15
Not that this function and all other imap_fetch*-functions will download the whole message and not just the header information.
se at designlinks dot net
02-Jul-2003 09:42
To further explain 'warrenfalk's comment, the following sequences are exactly the same, and always returns messages with lowest UID to highest UID:

1:5
5:1
1,2,3,4,5
5,4,3,2,1
3,2,5,1,4

(always returned as 1,2,3,4,5)
bill dot mccoy at pictureiq dot com
25-Apr-2003 08:30
trionon's point also is not supported by my measurements, which indicate that a loop of single-message imap_fetch_overview calls performs equivalently to 1 call with a sequence, within 1-2% anyway. The implementation of imap_fetch_overview (at least in PHP 4.3.1) hits the UW C client library for each message individually in a loop so this isn't too surprising, the extra work of a bit of PHP interpretation is swamped by the IMAP server interaction which is the same whether you call imap_fetch_overview 100 times or once with "1:100".
php dot net at warrenfalk dot com
19-Feb-2003 01:35
regardless of trionon's comments, the "sequence" parameter of imap_fetch_overview() only seems to specify *which* messages to get, not the *sequence* of the returned messages, so using imap_sort and imploding to a string to use for "sequence" is utterly useless since the c-client library from UW appears to use the "sequence" to mark the messages, then returns the messages in their stored (not sorted) order (which is, btw, somewhat unpredictable)

I was going to submit a bug report on this one, but the documentation here doesn't actually say that "sequence" means the sequence that the overviews are returned.  I'm thinking maybe a feature should be requested.  The question is: Is this the job of PHP or the job of UW-IMAP?  I think the former should add a parameter allowing the developer to optionally apply the sequence literally
suurenbroekNoSpam at ov35 dot NoSpam dot nl
14-Oct-2002 07:34
Note that these object-variables only exist when they are actually in the mail.

This means that if a mail has no subject, the property $val->subject will not exist.

Calling $val->subject will generate an notice:
Notice: Undefined property: subject in /home/html/inc/Mbox.php on line xxx

Use this to check it:
if (array_key_exists( "subject", get_object_vars($val)))
   $subj=$val->subject;
else
   $subj="";
trionon at mail dot ru
26-Feb-2002 02:13
This performance hint is useful if you need to print the result of imap_sort():

It's faster to prepare large string with UIDs and then to call imap_fetchoverview once than calling imap_fetchoverview in a loop.
steve at stevenchalker dot net
10-Nov-2001 12:26
I think if you go like a list, you can list your "hotmail" messages.
<?php
$mbox
=imap_open("{imap.server.com}","user","pass");
$MC=imap_check($mbox);
$MN=$MC->Nmsgs;
$overview=imap_fetch_overview($mbox,"1:$MN",0);
$size=sizeof($overview);
for(
$i=$size-1;$i>=0;$i--){
$val=$overview[$i];
$msg=$val->msgno;
$from=$val->from;
$date=$val->date;
$subj=$val->subject;
echo
"#$msg: From:'$from' Date:'$date' Subject:'$subj'<BR>";
imap_close($mbox);
?>