PHP Conference Kansai 2025

imap_fetchheader

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

imap_fetchheaderDevuelve el encabezado de un mensaje

Descripción

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

imap_fetchheader() devuelve el encabezado RFC2822 crudo y completo del mensaje msgno, en forma de string.

Parámetros

imap

An IMAP\Connection instance.

message_num

El número del mensaje

flags

Las 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).

Valores devueltos

Devuelve el encabezado del mensaje especificado, en forma de string, o false en caso de error.

Historial de cambios

Versión Descripción
8.1.0 The imap parameter expects an IMAP\Connection instance now; previously, a valid imap recurso was expected.

Ver también

add a note

User Contributed Notes 2 notes

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