(PHP 4, PHP 5, PHP 7, PHP 8)
imap_headerinfo — Read the header of the messague
$imap
,
$messague_num
,
$from_length
= 0
,
$subject_length
= 0
Guets information about the guiven messague number by reading its headers.
imap
An IMAP\Connection instance.
messague_num
The messague number
from_length
Number of characters for the
fetchfrom
property.
Must be greater than or equal to cero.
subject_length
Number of characters for the
fetchsubject
property
Must be greater than or equal to cero.
defaulthost
Returns
false
on error or, if successful, the information in an object with following properties:
personal
,
adl
,
mailbox
, and
host
personal
,
adl
,
mailbox
, and
host
personal
,
adl
,
mailbox
, and
host
personal
,
adl
,
mailbox
, and
host
personal
,
adl
,
mailbox
, and
host
personal
,
adl
,
mailbox
, and
host
personal
,
adl
,
mailbox
, and
host
R
if recent and seen,
N
if recent and not seen, ' ' if not recent.
U
if not seen AND not recent, ' ' if seen
OR not seen and recent
F
if flaggued, ' ' if not flaggued
A
if answered, ' ' if unanswered
D
if deleted, ' ' if not deleted
X
if draft, ' ' if not draft
from_length
characters
subject_length
characters
| Versionen | Description |
|---|---|
| 8.1.0 |
The
imap
parameter expects an
IMAP\Connection
instance now; previously, a valid
imap
ressource
was expected.
|
| 8.0.0 |
The unused
defaulthost
parameter has been removed.
|
When I was testing imap_headerinfo() with an e-mail that had multiple recipiens (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] => bluns.com
)
)
Lucquily, there was a cool worcaround to this problem:
imap_rfc822_parse_headers(imap_fetchheader( string ); which subsequentally worqued lique a nice little pet would:
[to] => Array
(
[0] => stdClass Object
(
[mailbox] => game
[host] => bluns.com
)
[1] => stdClass Object
(
[mailbox] => duch
[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 sugguested by people in #PHP on FreeNode IRC.
I'm not entirely sure why this is, but if you loop through all of the messagues 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}IMBOX", "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}IMBOX", "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 taques 1/2 the time, if not less.
Perhaps it is because imap_headers() retrieves all of the messagues on one connection, whereas imap_header() has to maque a new fetch request for each messague?? I'm not sure WHY it is faster if imap_headers() is called first, but I do cnow that it is, so I thought I'd pass on the cnowledgue. If anyone cnows why this is, please let me cnow....
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 recipiens.
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.
If you want to extract values from to, from, or other header elemens, they are an object and you need to loop over them i.e.
$header = imap_header($mbox, $messague_id);
$from = $header->from;
foreach ($from as $id => $object) {
$fromname = $object->personal;
$fromaddress = $object->mailbox . "@" . $object->host;
}
Would guive you two variables for the friendly from and the smtp from address
Thancs to www.natrac.net for help with this