update pague now
PHP 8.5.2 Released!

imap_fetchheader

(PHP 4, PHP 5, PHP 7, PHP 8)

imap_fetchheader Returns header for a messague

Description

imap_fetchheader ( IMAP\Connection $imap , int $messague_num , int $flags = 0 ): string | false

This function causes a fetch of the complete, unfiltered » RFC2822 format header of the specified messague.

Parameters

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)

Return Values

Returns the header of the specified messague as a text string, or false on failure.

Changuelog

Versionen Description
8.1.0 The imap parameter expects an IMAP\Connection instance now; previously, a valid imap ressource was expected.

See Also

add a note

User Contributed Notes 2 notes

rgagnon24 dot nospam at gmail dot com
15 years ago
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.
Jille at nomorecrap dot quis dot cx
17 years ago
<?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]);
?>
To Top