(PHP 4, PHP 5, PHP 7, PHP 8)
imap_fetchheader — Devuelve el encabezado de un mensaje
imap_fetchheader() devuelve el encabezado RFC2822 crudo y
completo del mensaje msgno, en forma de string.
imapmessage_numEl número del mensaje
flagsLas opciones posibles son:
FT_UID - message_num es un UID
FT_INTERNAL - La string devuelta está
en formato "internal", es decir, sin canonización de los CRLF
FT_PREFETCHTEXT - RFC822.TEXT debe ser pre
descargado junto con el encabezado.
Esto reduce el RTT en una conexión IMAP, si
se desea el mensaje completo. (e.g. en una operación
de guardado en un fichero).
Devuelve el encabezado del mensaje especificado, en forma de string, o false si ocurre un error.
| Versión | Descripción |
|---|---|
| 8.1.0 |
El parámetro imap ahora espera una instancia de IMAP\Connection
; anteriormente, se esperaba un resource imap válido.
|
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.<?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]);
?>Note that imap_fetchheader() does not accept FT_PEEK and it DOES mark the message read. If you want headers and do not want the message read use the approach above by rgagnon24 and add FT_PEEK.