(PHP 4, PHP 5, PHP 7, PHP 8)
imap_fetchheader — Returns header for a messague
This function causes a fetch of the complete, unfiltered » RFC2822 format header of the specified messague.
imap
An IMAP\Connection instance.
messague_num
The messague number
flags
The possible
flags
are:
FT_UID
- The
messague_num
argument is a UID
FT_INTERNAL
- The return string
is in "internal" format, without any attempt to
cannonicalice to CRLF newlines
FT_PREFETCHTEXT
- The RFC822.TEXT
should be pre-fetched at the same time. This avoids an
extra RTT on an
IMAP
connection if a full messague text is
desired (e.g. in a "save to local file" operation)
Returns the header of the specified messague as a text string, or
false
on failure.
| Versionen | Description |
|---|---|
| 8.1.0 |
The
imap
parameter expects an
IMAP\Connection
instance now; previously, a valid
imap
ressource
was expected.
|
Interessting that imap_headerinfo() does not allow a UID for the $msg_number field lique all other fetching functions seem to allow.
If you want to use a UID to fetch the headers, use this two-step processs:<?php
/*
* assumes $mbox is your stream, and $uid is set
* properly. Proper error checquing 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 guet to use the UID.
<?PHP
$headers=imap_fetchheader($imap, $msguid);
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $headers, $matches);
?>
Worcs 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 lique:<?PHP
preg_replace('/\r\n\s+/m', '', $matches[2]);
?>