PHP Doku:: Liefert einen Auszug aus den Kopfdaten von Nachrichten - function.imap-fetch-overview.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzE-Mail-relevante ErweiterungenIMAP, POP3 and NNTPIMAP Funktionenimap_fetch_overview

Ein Service von Reinhard Neidl - Webprogrammierung.

IMAP Funktionen

<<imap_expunge

imap_fetchbody>>

imap_fetch_overview

(PHP 4, PHP 5)

imap_fetch_overviewLiefert einen Auszug aus den Kopfdaten von Nachrichten

Beschreibung

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

Die Funktion fragt die Kopfdaten der mit sequence() angegebenen Nachrichten ab und liefert einen Auszug daraus zurück.

Parameter-Liste

imap_stream

Eine von imap_open() zurückgegebene IMAP-Verbindung.

sequence

Beschreibung der gewünschten Nachrichten. Mehrere Nachrichten können durch Komma getrennt angegeben werden (X,Y), Nachrichtenbereiche durch einen Doppelpunkt zwischen erster und letzter Nachricht (X:Y).

options

Wenn dieser Parameter auf FT_UID gesetzt wird werden die einzelnen Einträge der Nachrichtenliste sequence als UIDs und nicht als Nachrichtennummern interpretiert.

Rückgabewerte

Gibt ein Array von Objekten, die jeweils einen Nachrichtenkopf beschreiben, zurück. Das Objekt hat eine Eigenschaft nur dann, wenn sie auch existiert. Die möglichen Eigenschaften sind:

  • subject - die Betreffzeile der Nachricht
  • from - der Absender
  • to - der Empfänger
  • date - Sendezeitpunkt der Nachricht
  • message_id - die Message-ID der Nachricht
  • references - die Nachricht bezieht sich auf eine andere Nachricht mit dieser Message-ID
  • in_reply_to - die Nachricht ist eine Antwort auf eine andere Nachricht mit dieser Message-ID
  • size - Größe der Nachricht in Bytes
  • uid - die UID der Nachricht innerhalb des Postfachs
  • msgno - die aktuelle Nachrichtennummer innerhalb des Postfachs
  • recent - diese Nachricht ist als kürzlich eingetroffen markiert
  • flagged - diese Nachricht ist als wichtig markiert
  • answered - diese Nachricht ist als beantwortet markiert
  • deleted - diese Nachricht ist zur Löschung vorgemerkt
  • seen - diese Nachricht ist als bereits gelesen markiert
  • draft - diese Nachricht ist als Entwurf markiert

Beispiele

Beispiel #1 imap_fetch_overview()-Beispiel

<?php
$mbox 
imap_open("{imap.example.org:143}INBOX""username""password")
     or die(
"can't connect: " imap_last_error());

$MC imap_check($mbox);

// Hole eine Übersicht aller Nachrichten in der INBOX
$result imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
foreach (
$result as $overview) {
    echo 
"#{$overview->msgno} ({$overview->date}) - From: {$overview->from}
    
{$overview->subject}\n";
}
imap_close($mbox);
?>

Siehe auch


11 BenutzerBeiträge:
- Beiträge aktualisieren...
will
20.06.2009 18:43
This seems to retrieve incomplete values for headers, instead of getting the entire value, it gets the first. for example, this is a valid To: header

To: foo1@example.com,foo2@example.com
     foo3@example.com

but imap_fetch_overview will only return:
foo1@example.com
Ben
26.01.2009 2:12
Just a quick note, in case anyone else has this trouble.

Remeber that imap_fetch_overview returns an array! So even if you call it for only one message, you'll still need to pick out the first element.

<?php

// fetch overview:
$overview = imap_fetch_overview($handle, $uid, FT_UID);

// echo out, e.g., from field:
echo htmlspecialchars($overview[0]->from);
//                             ^^^ note the index!

?>

It may seem like a stupid error, but I spent a good half hour wondering why it wasn't working until I var_dump'ed $overview.
dognose
16.01.2009 18:15
That's right:

calling imap_fetch_overview() once is faster, than calling it
in a loop.

But if you want to get a "day sorted" list, you need to call it in a loop, cause if you enter a "string" of UIDs, they are always sorted by UID, not by the order you entered it.

Heres a little example how to get a date-sorted list:

<?php
/*Sort all msgs by Arrival Date. Newest = 0, oldest = Array Count*/
/*returns an array, containing the UIDs of messages*/
$SortedArray = imap_sort($Handle,SORTARRIVAL,1,SE_UID);

For (
$i = $Start; $i< $Start + $Limit; $i++){
     
/*Read UID from Sorted Array*/
     
$UID = $SortedArray[$i];
     
     
/*Get Detailed MSG Infos*/
     
$Overview = imap_fetch_overview($Handle, $UID, FT_UID);

      [...]
}
?>
xhack at web dot de
20.03.2007 16:05
you want a sortet list of mails, but you get always the same order? your c-client seems to cause this. i realized this with linux libc6-2.3.6 and php-5.2.0

try this little code:

# here you try to sort by your criteria
$sort=@imap_sort($mbox,SORTDATE,1,SE_UID);
 
# here the order of the messages to fetch should be recognised, but it isn't

$tmp=@imap_fetch_overview($mbox,implode(',',$sort),FT_UID);
# so try to work around simply:
for($i=0;$i<count($tmp);$i++)
{ $liste[$i]=$tmp[array_search($tmp[$i]->uid,$sort)]; }

so now you can use the sort by date, address, and so on.
raja at aonic dot net
14.03.2007 20:22
Here is a function to get messages from IMAP and sort them for pagination.

<?php
   
/**
     * Return array of IMAP messages for pagination
     *
     * @param   int     $page       page number to get
     * @param   int     $per_page   number of results per page
     * @param   array   $sort       array('subject', 'asc') etc
     *
     * @return  mixed   array containing imap_fetch_overview, pages, and total rows if successful, false if an error occurred
     * @author  Raja K
     */
   
public function listMessages($page = 1, $per_page = 25, $sort = null) {
       
$limit = ($per_page * $page);
       
$start = ($limit - $per_page) + 1;
       
$start = ($start < 1) ? 1 : $start;
       
$limit = (($limit - $start) != ($per_page-1)) ? ($start + ($per_page-1)) : $limit;
       
$info = imap_check($this->_imap_stream);
       
$limit = ($info->Nmsgs < $limit) ? $info->Nmsgs : $limit;
 
        if(
true === is_array($sort)) {
           
$sorting = array(
                       
'direction' => array(   'asc' => 0,
                                               
'desc' => 1),
 
                       
'by'        => array(   'date' => SORTDATE,
                                               
'arrival' => SORTARRIVAL,
                                               
'from' => SORTFROM,
                                               
'subject' => SORTSUBJECT,
                                               
'size' => SORTSIZE));
           
$by = (true === is_int($by = $sorting['by'][$sort[0]]))
                            ?
$by
                           
: $sorting['by']['date'];
           
$direction = (true === is_int($direction = $sorting['direction'][$sort[1]]))
                            ?
$direction
                           
: $sorting['direction']['desc'];
 
           
$sorted = imap_sort($this->_imap_stream, $by, $direction);
 
           
$msgs = array_chunk($sorted, $per_page);
           
$msgs = $msgs[$page-1];
        }
        else
           
$msgs = range($start, $limit); //just to keep it consistent
 
       
$result = imap_fetch_overview($this->_imap_stream, implode($msgs, ','), 0);
        if(
false === is_array($result)) return false;
 
       
//sorting!
       
if(true === is_array($sorted)) {
           
$tmp_result = array();
            foreach(
$result as $r)
               
$tmp_result[$r->msgno] = $r;
 
           
$result = array();
            foreach(
$msgs as $msgno) {
               
$result[] = $tmp_result[$msgno];
            }
        }
 
       
$return = array('res' => $result,
                       
'start' => $start,
                       
'limit' => $limit,
                       
'sorting' => array('by' => $sort[0], 'direction' => $sort[1]),
                       
'total' => imap_num_msg($this->_imap_stream));
       
$return['pages'] = ceil($return['total'] / $per_page);
        return
$return;
    }
?>
Group-Office Developer
10.04.2004 2: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.12.2003 1: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
2.07.2003 15: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)
suurenbroekNoSpam at ov35 dot NoSpam dot nl
14.10.2002 13: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
25.02.2002 19: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.11.2001 5: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);
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)