PHP 8.3.21 Released!

imap_headerinfo

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

imap_headerinfoLee la cabecera del mensaje

Descripción

imap_headerinfo(
    IMAP\Connection $imap,
    int $message_num,
    int $from_length = 0,
    int $subject_length = 0
): stdClass|false

Recupera la información sobre un número de mensaje dado leyendo sus cabeceras.

Parámetros

imap

An IMAP\Connection instance.

message_num

El número del mensaje

from_length

Número de caracteres para la propiedad fetchfrom. Debe ser mayor o igual a 0.

subject_length

Número de caracteres para la propiedad fetchsubject. Debe ser mayor o igual a 0.

defaulthost

Valores devueltos

Devuelve false en caso de errores o, en caso de éxito, la información en un objeto que contiene las siguientes propiedades:

  • "toaddress" : toda la línea de cabecera to: hasta 1024 caracteres
  • "to" : un array de objetos de la línea to: con las siguientes propiedades: personal, adl, mailbox, y host
  • "fromaddress" : toda la línea de cabecera from: hasta 1024 caracteres
  • "from" : un array de objetos de la línea From: con las siguientes propiedades: personal, adl, mailbox, y host
  • "ccaddress" : toda la línea de cabecera cc: hasta 1024 caracteres
  • "cc" : un array de objetos de la línea cc: con las siguientes propiedades: personal, adl, mailbox, y host
  • "bccaddress" : toda la línea de cabecera bcc: hasta 1024 caracteres
  • "bcc" : un array de objetos de la línea Bcc: con las siguientes propiedades: personal, adl, mailbox, y host
  • "reply_toaddress" : toda la línea de cabecera Reply_to: hasta 1024 caracteres
  • "reply_to" : un array de objetos de la línea Reply_to: con las siguientes propiedades: personal, adl, mailbox, y host
  • "senderaddress" : toda la línea de cabecera Sender: hasta 1024 caracteres
  • "sender" : un array de objetos de la línea Sender: con las siguientes propiedades: personal, adl, mailbox, y host
  • "return_pathaddress" : toda la línea de cabecera Return-path: hasta 1024 caracteres
  • "return_path" : un array de objetos de la línea Return-path: con las siguientes propiedades: personal, adl, mailbox, y host
  • remail -
  • "date" : La fecha del mensaje, tal como se encuentra en las cabeceras
  • "Date" : Idéntico a "date"
  • "subject" : El asunto del mensaje
  • "Subject" : Idéntico a "subject"
  • "in_reply_to" :
  • "message_id" :
  • "newsgroups" :
  • "followup_to" :
  • "references" :
  • "Recent" : R si el mensaje es reciente y leído, N si el mensaje es reciente y no leído, " " si el mensaje no es reciente.
  • "Unseen" : U si el mensaje es no leído Y no reciente, " " si el mensaje es no leído y reciente
  • "Flagged" : F si el mensaje contiene un flag, " " en caso contrario
  • "Answered" : A si se ha respondido a este mensaje, " " en caso contrario
  • "Deleted" : D si el mensaje está eliminado, " " en caso contrario
  • "Draft" : X si el mensaje es un borrador, " " en caso contrario
  • "Msgno" : El número del mensaje
  • "MailDate" :
  • "Size" : El tamaño del mensaje
  • "udate" : Fecha de envío del mensaje, en forma de fecha Unix
  • "fetchfrom" : Línea "from" formateada para caber en from_length caracteres
  • "fetchsubject" : Línea "subject" formateada para caber en subject_length caracteres

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.
8.0.0 El parámetro defaulthost sin uso ha sido eliminado.

Ver también

add a note

User Contributed Notes 5 notes

up
24
andyltm
14 years ago
When I was testing imap_headerinfo() with an e-mail that had multiple recipients (multiple e-mails in to to: and/or cc: field), I noticed that imap_headerinfo() was failing hard for me on PHP 5.2.10-2ubuntu6.4 with Suhosin-Patch 0.9.7 (cli).

Rather than providing me an array with each and every e-mail address listed in the to and/or cc fields, it was only providing me the first listed. This was disappointing.

[to] => Array
(
[0] => stdClass Object
(
[mailbox] => game
[host] => blunts.com
)
)

Luckily, there was a cool workaround to this problem:

imap_rfc822_parse_headers(imap_fetchheader( string ); which subsequentally worked like a nice little pet would:

[to] => Array
(
[0] => stdClass Object
(
[mailbox] => game
[host] => blunts.com
)
[1] => stdClass Object
(
[mailbox] => dutch
[host] => masters.com
)
)

TL;DR:
So in other words, instead of imap_headerinfo() use imap_rfc822_parse_headers(imap_fetchheader()).

Hope this helps anyone else that runs into this issue from now into the future. This post was suggested by people in #PHP on FreeNode IRC.
up
9
jahservant 13 at gmail dot com
15 years ago
I'm not entirely sure why this is, but if you loop through all of the messages in a mailbox, calling imap_header() each time, you can significantly increase performance by calling imap_headers() first.

Compare this:
<?php
$imap
= imap_open("{my.server.com:143}INBOX", "user", "pass");
$n_msgs = imap_num_msg($imap);
$s = microtime(true);
for (
$i=0; $i<$n_msgs; $i++) {
$header = imap_header($imap, $i);
}
$e = microtime(true);
echo (
$e - $s);
imap_close($imap);
?>

With this:
<?php
$imap
= imap_open("{my.server.com:143}INBOX", "user", "pass");
$n_msgs = imap_num_msg($imap);
/****** adding this line: ******/
imap_headers($imap)
/***************************/
$s = microtime(true);
for (
$i=0; $i<$n_msgs; $i++) {
$header = imap_header($imap, $i);
}
$e = microtime(true);
echo (
$e - $s);
imap_close($imap);
?>

The performance difference, as I have tested on several boxes, connecting to several different servers, is that the second code snippet ALWAYS takes 1/2 the time, if not less.

Perhaps it is because imap_headers() retrieves all of the messages on one connection, whereas imap_header() has to make a new fetch request for each message?? I'm not sure WHY it is faster if imap_headers() is called first, but I do know that it is, so I thought I'd pass on the knowledge. If anyone knows why this is, please let me know....
up
3
php at our-software dot com
8 years ago
Please Note : imap_headerinfo only returns a subset of the headers, rather than the entire thing.

Among other things, this means it only shows the first recipient from the "to" section of the email, rather than all recipients.

If you're not seeing something you expected to, you'll be better off using

$hdr_raw = imap_fetchheader($mbox, $mailid);
$hdr = imap_rfc822_parse_headers($hdr_raw);

then you'll see the full set of headers, and be able to do more with it.
up
5
scott at fuzzygroup dot com
22 years ago
If you want to extract values from to, from, or other header elements, they are an object and you need to loop over them i.e.

$header = imap_header($mbox, $message_id);
$from = $header->from;
foreach ($from as $id => $object) {
$fromname = $object->personal;
$fromaddress = $object->mailbox . "@" . $object->host;
}

Would give you two variables for the friendly from and the smtp from address

Thanks to www.natrak.net for help with this
up
1
Murray
9 years ago
An email without a subject line will not generate the 'subject' property.

Before using the 'subject' property you should test for it.
if (property_exists($header, 'subject')) echo $header->subject;
To Top