imap_headerinfo

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

imap_headerinfoLiefert Informationen über die Kopfdaten einer Nachricht

Beschreibung

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

Liest die Kopfdaten der Nachricht mit der Nummer message_num und gibt daraus gewonnene Informationen zurück.

Parameter-Liste

imap

Eine IMAP\Connection-Instanz.

message_num

Die Nummer der Nachricht

from_length

Die maximale Anzahl Zeichen für die Eigenschaft fetchfrom (muss größer oder gleich Null sein).

subject_length

Die maximale Anzahl Zeichen für die Eigenschaft fetchsubject (muss größer oder gleich Null sein).

defaulthost

Rückgabewerte

Gibt bei einem Fehler false zurück oder bei Erfolg die Informationen in einem Objekt mit folgenden Eigenschaften:

  • toaddress - der Inhalt der "To:"-Zeile (max. 1024 Zeichen)
  • to - ein Array von Objekten aus der "To:"-Zeile mit den folgenden Eigenschaften: personal, adl, mailbox und host
  • fromaddress - der Inhalt der "From:"-Zeile (max. 1024 Zeichen)
  • from - ein Array von Objekten aus der "From:"-Zeile mit den folgenden Eigenschaften: personal, adl, mailbox und host
  • ccaddress - der Inhalt der "Cc:"-Zeile (max. 1024 Zeichen)
  • cc - ein Array von Objekten aus der "Cc:"-Zeile mit den folgenden Eigenschaften: personal, adl, mailbox und host
  • bccaddress - der Inhalt der "Bcc:"-Zeile (max. 1024 Zeichen)
  • bcc - ein Array von Objekten aus der "Bcc:"-Zeile mit den folgenden Eigenschaften: personal, adl, mailbox und host
  • reply_toaddress - der Inhalt der "Reply-To:"-Zeile (max. 1024 Zeichen)
  • reply_to - ein Array von Objekten aus der "Reply-To:"-Zeile mit den folgenden Eigenschaften: personal, adl, mailbox und host
  • senderaddress - der Inhalt der "Sender:"-Zeile (max. 1024 Zeichen)
  • sender - ein Array von Objekten aus der "Sender:"-Zeile mit den folgenden Eigenschaften: personal, adl, mailbox und host
  • return_pathaddress - der Inhalt der "Return-Path:"-Zeile (max. 1024 Zeichen)
  • return_path - ein Array von Objekten aus der "Return-Path:"-Zeile mit den folgenden Eigenschaften: personal, adl, mailbox und host
  • remail -
  • date - das Sendedatum der Nachricht laut Kopfdaten
  • Date - enthält die gleichen Daten wie 'date'
  • subject - die Betreffzeile der Nachricht
  • Subject - enthält die gleichen Daten wie 'subject'
  • in_reply_to -
  • message_id -
  • newsgroups -
  • followup_to -
  • references -
  • Recent - R, wenn kürzlich eingetroffen und gelesen, N, wenn kürzlich eingetroffen und ungelesen, ' ', wenn nicht kürzlich eingetroffen.
  • Unseen - U, wenn nicht kürzlich eingetroffen UND ungelesen, ' ', wenn gelesen ODER ungelesen und kürzlich eingetroffen.
  • Flagged - F, wenn als wichtig markiert, sonst ' '
  • Answered - A, wenn beantwortet, sonst ' '
  • Deleted - D, wenn zum Löschen vorgemerkt, sonst ' '
  • Draft - X, wenn als Entwurf markiert, sonst ' '
  • Msgno - die Nummer der Nachricht
  • MailDate -
  • Size - die Größe der Nachricht in Bytes
  • udate - das Sendedatum als Unix-Timestamp
  • fetchfrom - die "From:"-Zeile limitiert auf from_length Zeichen
  • fetchsubject - die "Subject:"-Zeile, limitiert auf subject_length Zeichen

Changelog

Version Beschreibung
8.1.0 Der Parameter imap erwartet nun eine IMAP\Connection-Instanz; vorher wurde eine gültige imap-Ressource erwartet.
8.0.0 Der nicht verwendete Parameter defaulthost wurde entfernt.

Siehe auch

add a note add a note

User Contributed Notes 10 notes

up
22
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
14 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
7 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
4
scott at fuzzygroup dot com
21 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
ron at NOSPAM dot clicks2hits dot com
16 years ago
Simple little code for checking gmail using headerinfo

<?php /* Created on: 11/3/2008 By Ron Hickey 6tx.net/gmail_mod
Gmail mod for admin panels or you can edit it and convert html results to XML for personal RSS reader */

// enter gmail username below e.g.--> $m_username = "yourusername";
$m_username = "";

// enter gmail password below e.g.--> $m_password = "yourpword";
$m_password = "";

// enter the number of unread messages you want to display from mailbox or
//enter 0 to display all unread messages e.g.--> $m_acs = 0;
$m_acs = 15;

// How far back in time do you want to search for unread messages - one month = 0 , two weeks = 1, one week = 2, three days = 3,
// one day = 4, six hours = 5 or one hour = 6 e.g.--> $m_t = 6;
$m_t = 2;

//----------->Nothing More to edit below
//open mailbox..........please
$m_mail = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $m_username . "@gmail.com", $m_password)

// or throw a freakin error............you pig
or die("ERROR: " . imap_last_error());

// unix time gone by or is it bye.....its certanly not bi.......or is it? ......I dunno fooker
$m_gunixtp = array(2592000, 1209600, 604800, 259200, 86400, 21600, 3600);

// Date to start search
$m_gdmy = date('d-M-Y', time() - $m_gunixtp[$m_t]);

//search mailbox for unread messages since $m_t date
$m_search=imap_search ($m_mail, 'UNSEEN SINCE ' . $m_gdmy . '');

//If mailbox is empty......Display "No New Messages", else........ You got mail....oh joy
if($m_search < 1){
$m_empty = "No New Messages";}
else {

// Order results starting from newest message
rsort($m_search);

//if m_acs > 0 then limit results
if($m_acs > 0){
array_splice($m_search, $m_acs);
}

//loop it
foreach ($m_search as $what_ever) {

//get imap header info for obj thang
$obj_thang = imap_headerinfo($m_mail, $what_ever);

//Then spit it out below.........if you dont swallow
echo "<body bgcolor=D3D3D3><div align=center><br /><font face=Arial size=2 color=#800000>Message ID# " . $what_ever . "</font>

<table bgcolor=#D3D3D3 width=700 border=1 bordercolor=#000000 cellpadding=0 cellspacing=0>
<tr>
<td><table width=100% border=0>
<tr>
<td><table width=100% border=0>
<tr>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>Date:</font> <font face=Arial size=2 color=#000000>"
. date("F j, Y, g:i a", $obj_thang->udate) . "</font></td>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>From:</font> <font face=Arial size=2 color=#000000>"
. $obj_thang->fromaddress . "</font></td>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>To:</font> <font face=Arial size=2 color=#000000>"
. $obj_thang->toaddress . " </font></td>
</tr>
<tr>
</table>
</td>
</tr><tr><td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>Subject:</font> <font face=Arial size=2 color=#000000>"
. $obj_thang->Subject . "</font></td></tr><tr>
</tr>
</table></td>
</tr>
</table><a href=http://gmail.com target=_blank><font face=Arial size=2 color=#800000>Login to read message</a></font><br /></div></body>"
;

}} echo
"<div align=center><font face=Arial size=4 color=#800000><b>" . $m_empty . "</b></font></div>";
//close mailbox bi by bye
imap_close($m_mail);
?>
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;
up
0
milosh
10 years ago
I did some testing on using imap_headerinfo() function and I am very confused with the results.

On small mailboxes, getting data for 30 messages takes about 0.5 secs. On mailboxes with approximately 500 messages it takes about 7 secs to retrieve data for the same number of messages.

Why would the size of mailbox had anything to do with the time needed to retrieve the header of the single email message? Mailboxes are on the same account.

Next, If you measure every call to imap_headerinfo() function the result are even stranger! First and then every 22th call to the imap_headerinfo() function takes 10000 times more that the others. Example: the first call takes about 0.39 secs, then other 20 calls take about 0.0001 secs, then 22th call takes about 0.47 secs, then other 20 calls about 0.00004, and so on.

After some more research there is something else that came up.

If you use:

$message_header[$i] = imap_headerinfo($mbox, $i + 1);

it takes about 0.4 secs for every 22th call and about 0.0001 sec for other calls.

However, you would expect the same results with:

$message_header[$i] = imap_headerinfo($mbox, 30 - $i);

But, in this case it takes about 0.2 secs for every call!

The only difference here is that in the second example headers are retrieved in the reversed message order (from the 30th to the 1st) and for some reason it greatly affects the time needed for the operation. Strange, or is it just me?
up
0
trinkaus at ttNetz dot de
14 years ago
When i try to access the same mailbox through pop3 and through imap i get different update infomations.

1. Using imap
header->udate is not the same as strtotime($header->date)
<?php
$mBox
= imap_open("{host:143/imap/novalidate-cert}INBOX}", $username, $password); // open as imap
$header = imap_header($mBox, 1); // get first mails header
echo $header->udate;
echo
strtotime($header->date);
?>

1. Using pop3
header->udate is the same as strtotime($header->date)
<?php
$mBox
= imap_open("{host:110/pop3/novalidate-cert}INBOX}", $username, $password); // open as pop3
$header = imap_header($mBox, 1); // get first mails header
echo $header->udate;
echo
strtotime($header->date);
?>

I do not know why this is so, but for this reason, i use strtotime($header->date) together with other values to create my own message-UID because this seems to work independent from the protocol.
up
-3
aeolianmeson at exoyiga dot blitzeclipse dot com
15 years ago
I typically use UID's to identify messages, and recently discovered that the headers I had been pulling using this function and a message-number didn't match the UID's. Instead of worrying about it, I just began using imap_fetchheader() and imap_ rfc822_ parse_ headers() on its output. The only significant difference I immediately noticed was that there is no "udate" property, so I assigned one with the result of strtotime() on the 'date' property.

Dustin
up
-3
php at spacefish dot biz
15 years ago
I had to pass the msgid as array($msgid) instead of an integer. the error "Invalid messageID" is generated if i pass an integer.
To Top