PHP Doku:: Liefert den Kopf einer Nachricht - function.imap-fetchheader.html

Verlauf / Chronik / History: (1) anzeigen

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

Ein Service von Reinhard Neidl - Webprogrammierung.

IMAP Funktionen

<<imap_fetchbody

imap_fetchstructure>>

imap_fetchheader

(PHP 4, PHP 5)

imap_fetchheaderLiefert den Kopf einer Nachricht

Beschreibung

string imap_fetchheader ( resource $imap_stream , int $msg_number [, int $options = 0 ] )

Diese Funktion liefert den kompletten ungefilterten Kopf der angegebenen Nachricht gemäß » RFC2822.

Parameter-Liste

imap_stream

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

msg_number

Die Nachrichtennummer

options

Folgende Flags können in options gesetzt werden:

  • FT_UID - msgno ist eine UID
  • FT_INTERNAL - Rückgaben im internen Format ohne jede Umwandlung von Zeilenenden
  • FT_PREFETCHTEXT Der Körper der Nachricht wird zusammen mit dem Kopf zum Client übertragen. Dies vermeidet einen weitern Zugriff auf den IMAP-Server wenn die gesamte Nachricht bearbeitet werden soll (z.B. zum Abspeichern in einer lokalen Datei)

Rückgabewerte

Gibt den Kopf der angegebenen Nachricht als Zeichenkette zurück.

Siehe auch


5 BenutzerBeiträge:
- Beiträge aktualisieren...
rgagnon24 dot nospam at gmail dot com
24.06.2010 18:52
Interesting that imap_headerinfo() does not allow a UID for the $msg_number field like all other fetching functions seem to allow.

If you want to use a UID to fetch the headers, use this two-step process:

<?php
/*
 * assumes $mbox is your stream, and $uid is set
 * properly. Proper error checking is up to you.
 */
$hText = imap_fetchbody($mbox, $uid, '0', FT_UID);
$headers = imap_rfc822_parse_headers($hText);
?>

The result is the same as the output of imap_headerinfo(), but you get to use the UID.
Jille at nomorecrap dot quis dot cx
7.04.2008 0:15
<?PHP
$headers
=imap_fetchheader($imap, $msgid);
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $headers, $matches);
?>

Works quite well for splitting headers,
$matches will contain 3 arrays:
$matches[0] are the full-lines (To: Jille@devnull.quis.cx\r\n)
$matches[1] will be the header (To)
$matches[2] will be the value (Jille@devnull.quis.cx)

In multi-line values the 'multilining is not stripped!'
this could be achieved with something like:
<?PHP
preg_replace
('/\r\n\s+/m', '', $matches[2]);
?>
onofabio at gmail dot com
7.03.2008 12:18
This function ignore that some header value have multiple lines...

<?php
   
// connecting to imap mailserver
   
$connection = @imap_open("{localhost:143/imap}INBOX", "your_username", "your_password");

   
// get imap_fetch header and put single lines into array
   
$header = explode("\n", imap_fetchheader($connection, 1));

   
// browse array for additional headers
   
if (is_array($header) && count($header)) {
       
$head = array();
        foreach(
$header as $line) {
           
// is line with additional header?
           
if (eregi("^X-", $line)) {
               
// separate name and value
               
eregi("^([^:]*): (.*)", $line, $arg);
               
$head[$arg[1]] = $arg[2];
            }
        }
    }

   
// now are all contained additional headers in array $head
?>

I write this simple function....

$mbox = imap_open("{localhost:143/imap}INBOX", "your_username", "your_password");
$mid=1 // Message id

// Get headers
$header = imap_fetchheader($mbox, $mid);

// Split on \n
$h_array=split("\n",$header);

foreach ( $h_array as $h ) {

    // Check if row start with a char
    if ( preg_match("/^[A-Z]/i", $h )) {

        $tmp = split(":",$h);
    $header_name = $tmp[0];
    $header_value = $tmp[1];
               
    $headers[$header_name] = $header_value;
       
    } else {
        // Append row to previous field
    $headers[$header_name] = $header_value . $h;
    }

}
Max Geiger
9.03.2007 0:59
If you call the function with the bitmask-parameter FT_PREFETCHTEXT the /Seen flag of the message will be set.
dj_doktor at upskirt dot cz
3.04.2005 16:17
I spend lot of time find out how I can detect messages with
different priorities. When I read users notes I remember
function imap_fetchheader which show header of message
and additional headers too. Because I'm so lazy to work with
regular expressions I required my frend for writing code
- thanx Znouza.
And there is... :)

<?php
   
// connecting to imap mailserver
   
$connection = @imap_open("{localhost:143/imap}INBOX", "your_username", "your_password");

   
// get imap_fetch header and put single lines into array
   
$header = explode("\n", imap_fetchheader($connection, 1));

   
// browse array for additional headers
   
if (is_array($header) && count($header)) {
       
$head = array();
        foreach(
$header as $line) {
           
// is line with additional header?
           
if (eregi("^X-", $line)) {
               
// separate name and value
               
eregi("^([^:]*): (.*)", $line, $arg);
               
$head[$arg[1]] = $arg[2];
            }
        }
    }

   
// now are all contained additional headers in array $head
?>



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",...)