imap_headerinfo

(PHP 3, PHP 4, PHP 5)

imap_headerinfo -- Read the header of the message

Description

object imap_headerinfo ( resource imap_stream, int msg_number [, int fromlength [, int subjectlength [, string defaulthost]]] )

This function returns an object of various header elements.


       remail, date, Date, subject, Subject, in_reply_to, message_id,
       newsgroups, followup_to, references

message flags:
   Recent -  'R' if recent and seen, 
             'N' if recent and not seen, 
             ' ' if not recent
   Unseen -  'U' if not seen AND not recent, 
             ' ' if seen OR not seen and recent
   Answered -'A' if answered, 
             ' ' if unanswered
   Deleted - 'D' if deleted, 
             ' ' if not deleted
   Draft -   'X' if draft, 
             ' ' if not draft
   Flagged - 'F' if flagged, 
             ' ' if not flagged

NOTE that the Recent/Unseen behavior is a little odd. If you want to
know if a message is Unseen, you must check for

Unseen == 'U' || Recent == 'N'

toaddress (full to: line, up to 1024 characters)

to[] (returns an array of objects from the To line, containing):
   personal
   adl
   mailbox
   host

fromaddress (full from: line, up to 1024 characters)

from[] (returns an array of objects from the From line, containing):
   personal
   adl
   mailbox
   host

ccaddress (full cc: line, up to 1024 characters)
cc[] (returns an array of objects from the Cc line, containing):
   personal
   adl
   mailbox
   host

bccaddress (full bcc line, up to 1024 characters)
bcc[] (returns an array of objects from the Bcc line, containing):
   personal
   adl
   mailbox
   host

reply_toaddress (full reply_to: line, up to 1024 characters)
reply_to[] (returns an array of objects from the Reply_to line,
containing):
   personal
   adl
   mailbox
   host

senderaddress (full sender: line, up to 1024 characters)
sender[] (returns an array of objects from the sender line, containing):
   personal
   adl
   mailbox
   host

return_path (full return-path: line, up to 1024 characters)
return_path[] (returns an array of objects from the return_path line,
containing):
   personal
   adl
   mailbox
   host

udate (mail message date in unix time)

fetchfrom (from line formatted to fit fromlength 
characters)

fetchsubject (subject line formatted to fit subjectlength characters)
      


add a note add a note User Contributed Notes
mwwaygoo AT hotmail DOT com
02-Feb-2006 10:58
Thanks to scott AT fuzzygroup DOT com for the object thing. So to make it a bit easier in the future I wrote a function to convert the mixed array/object to a straight array structure, as its easier to work with.

<?php
function object_to_array($the_object)
{
  
$the_array=array();
   if(!
is_scalar($the_object))
   {
       foreach(
$the_object as $id => $object)
       {
           if(
is_scalar($object))
           {
              
$the_array[$id]=$object;
           }
           else
           {
              
$the_array[$id]=object_to_array($object);
           }
       }
       return
$the_array;
   }
   else
   {
       return
$the_object;
   }
}

$array_header=object_to_array($header);

$from=$array_header['from']['0']['personal'];
$from_email=$array_header['from']['0']['mailbox'] . "@" . $array_header['from']['0']['host'];

?>
jh at junetz dot de
14-Sep-2004 10:33
"toaddress (full to: line, up to 1024 characters)"

Read that carefully. It means that this function does not unfold long header fields. I.e. the follwing example:

To: a@b.com,
   c@d.org

will result in only a@b.com appearing in "toaddress" (and "to", as a result). Other functions have to be used to retrieve the full To field value in all cases.

I guess the above also applies to CC and BCC fields, respectively.
james at cridland dot net
10-Nov-2003 09:35
The difference between 'date' and 'udate' seems to be rather more than just the way they're formatted.

'date' is the date that was written in the headers by the sender's mail client, and probably bears little to do with reality. It's dependent on your sender knowing what the correct time is; it could be out by a few minutes, days, months or even years.

'udate' is the real date that the e-mail hit your IMAP server.

Use 'udate' if you want to do neat stuff like work out how much e-mail you get sent on a daily basis - or, as I do, how much spam I get. Don't try it on 'date' unless you like knowing that you've got negative numbers of e-mails every now and again!
vhornik at seznam dot cz
06-Mar-2003 12:50
Note that msg_number is really just that = message sequence number. You can't use UID here as with other IMAP functions like imap_fetchheader() or imap_fetchstructure() with the FT_UID option.
jamie at coverallcrew dot com
04-Dec-2002 12:12
I was unable to get the message subject by calling imap_headerinfo AFTER calling imap_body. I flipped them (which makes more sense anyway in our usual understanding of e-mail messages) and it worked!

// so this code doesn't work:

//------------------------------------------------
for ($i = 1; $i <= imap_num_msg($mbox); $i++)
{
   $messageBody = imap_body($mbox, $i);
   $header = imap_headerinfo($mbox, $i, 80, 80);
   $subject= $header->fetchsubject;
  }

imap_close($mbox);
//------------------------------------------------

// but this code does:

//------------------------------------------------
for ($i = 1; $i <= imap_num_msg($mbox); $i++)
{
   $header = imap_headerinfo($mbox, $i, 80, 80);
   $subject= $header->fetchsubject;
  
   $messageBody = imap_body($mbox, $i);
  }
 
imap_close($mbox);
//------------------------------------------------

Hope this saves some headaches! Jamie
scott at fuzzygroup dot com
12-Nov-2002 01:13
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
jsmith at one dot net
13-Sep-2002 08:39
This is probably trivial to most people, but it wasn't obvious to me. When you call something like $header->from it returns an array of objects, but the array only contains one element. To call one of the from objects in the array, I used: $personal = $header->from[0]->personal;
to get the 'personal' name. Hope this helps.
jacky at jackyhung dot net
27-Jan-2002 04:32
There have something new header message went you open a nntp server

date.Date.subject.Subject.message_id
newsgroups.references.fromaddress
from.reply_toaddress
reply_to.senderaddress.sender
Recent,Unseen.Flagged.Answered
Deleted.Draft.Msgno,MailDate,Size,udate
robsiemb at NOSPAM dot crusoe dot net
15-May-2001 04:45
That's because the RECENT flag is different from the UNSEEN flag.  RECENT is not user-setable, and is dependent on wether a mailbox has been opened (or something?) since the message arrived.  Once a message is noted as RECENT to a client, the RECENT flag is cleared.  UNSEEN is user-setable however and what is typically known as the new message flag.
sjsobol at JustThe dot net
11-May-2001 12:58
I'm running php4.0.5 and C-client from UW IMAPD 4.7c2, but
I'm not running the UW IMAPD server, I'm running Cyrus. Cyrus
doesn't set Recent to N. To check whether a message is new I simply
check whether Unseen is set to "U".
mairsil at ii dot nl
31-Jan-2001 02:33
Well, duh. $header->fetchsubject prints a subjectline trimmed down to [subjectlength] characters.
So with $header = imap_headerinfo($mbox,$msgnum) it yields nothing.
Try it with $header = imap_headerinfo($mbox,$msgnum,80,80) and be amazed.
All comes down to RTFM as always...
cvincent at project802 dot net
29-Dec-2000 06:33
I tried to set $header = imap_headerinfo($mbox,$msgnum) and print $header->fetchsubject but it doesnt print anything, instead try $header->subject.
brichardson at lineone dot net
08-Dec-2000 12:00
Some buggy IMAP servers (e.g. the GroupWise Internet Agent) don't support this function - in that case use imap_fetch_overview instead.