imap_header

(PHP 3, PHP 4, PHP 5)

imap_header -- 别名 imap_headerinfo()

说明

本函数是该函数的别名: imap_headerinfo().


add a note add a note User Contributed Notes
chorkt
28-Jun-2003 12:34
i was having a hard time with the notes on the page but this works... It's simply getting out the e-mail address from the header

for ($i = 1; $i <= imap_num_msg($mbox); $i++)
{
  $header = imap_headerinfo($mbox, $i, 80, 80);
  $fromaddress[$i] = $header->from[0]->host;
  $fromname[$i] = $header->from[0]->mailbox;
  $from[$i]= $fromname[$i]."@".$fromaddress[$i];

  $subject[$i] = $header->fetchsubject;
  print "<br>".$from[$i]." ".$subject[$i]." \n  ";

}
burkard.schemmel [AT] web.de
19-May-2003 04:50
I've tried several times to get the imap_header to work. Here is the solution. And, it's the only correct one desperate all other comments on this page!

$mFrom = $oHeader->from;
$FROM = $mFrom[0]->mailbox."@".$mFrom[0]->host;
$NAME = $mFrom[0]->personal;

Relating object is $oHeader while $FROM will contain the mail address and $NAME the sender's name (if set).

Greetings from Bavaria ;-)
keeper at odi dot com dot br
02-Mar-2003 04:36
For those who are trying to decode the header take a loke ate the function imap_mime_header_decode()

Bye

Dotrinador
DokFLeed dokfleed at dokfleed dot com
28-Jan-2003 03:55
After getting lost for days,i gathered all what i could find plus
the codes i tweaked according to the manual , here they go,
all formatted and ready to use ,
this will grab all the messages in a mail box, and read them.
You will need to replace Username & Password by yours, ( i
dont know what the "adl" or why isnt it showing ( codes i
gathered , their credits goes to whoever they belong)
 
==============================
Start of Code
==============================
//It opens up an IMAP connection to the mailserver specified
//by $MAILSERVER, passing in your username and password.
//It then get a list of all the messgae headers and in a loop,
//prints them all out. Besides printing them out, click the link
//to read it , passing the message number to the function
//viewmailz
 
function readmailz(){
 
$MAILSERVER="{mail.host.com:143}";
or $MAILSERVER="{localhost:143}";
$PHP_AUTH_USER = "USERNAME";
$PHP_AUTH_PW = "PASSWORD";
$mbox=imap_open($MAILSERVER,
       $PHP_AUTH_USER,
       $PHP_AUTH_PW);
echo "Number of Total Emails: "
     .imap_num_msg($mbox);
echo "  Number of Recent Emails: "
     .imap_num_recent($mbox)."<BR><BR>";
$headers=imap_headers($mbox);
 
for($x=0; $x < count($headers); $x++) {
   $idx=($x+1);
   echo $idx."  <a href=\"$PHP_SELF?num=$idx\">
                 $headers[$x]
                   </a><br>";
 
}
imap_close($mbox);
}
 
//viewmailz opens up the IMAP connection the same as
// above, and gets the mail message's header information and
//prints it out.
//It then reads in the body of the mail message and prints
//that out to the screen.
function viewmailz($num){
$MAILSERVER="{mail.host.com:143}";
or $MAILSERVER="{localhost:143}";
$PHP_AUTH_USER = "USERNAME";
$PHP_AUTH_PW = "PASSWORD";

$mbox=imap_open($MAILSERVER,
               $PHP_AUTH_USER,
               $PHP_AUTH_PW);
$header=imap_headerinfo($mbox,
                       $num,
                       80,80);
$from = $header->from;
$udate=$header->udate;
$date=Date("F j, Y, g:i a", $udate);
echo "Message Number: ". $num."<br>";
$subject= $header->fetchsubject;
if (is_array($from)){
while(list($key, $val) = each($from)) {
echo "From Address:  ";
echo $fromaddr=sprintf("%s@%s",
                       $from[0]->mailbox,
                       $from[0]->host).
                       "<BR>";
echo "Personal    :  ".
     $from[0]->personal.
     "<br>";
echo "Adl        :  ".
     $from[0]->adl.
     "<br>";
echo "Mailbox    :  ".
     $from[0]->mailbox.
     "<br>";
echo "Host        :  ".
     $from[0]->host.
     "<br>";
echo "Subject    :  ".
     $subject.
     "<br>";
echo "Date        :  ".
     $date.
     "<br>";
echo "To Address  :  ".
     $header->toaddress.
     "<br><BR><BR>";
}
}

echo imap_body($mbox,$num).
     "<br>END OF BODY<br>";
 
imap_close($mbox);
}
 
==========================
END OF Code
==========================
gungp720 at yahoo dot com
20-Nov-2002 05:02
To convert date format from mail header to dd/mm/yyyy H:i use this :
<?
$tgl1
= strtotime($msg->date); //convert to timestamp
$tgl2 = date("d/m/Y H:i",$tgl1); //format date from timestamp
echo $tgl2;
?>
hope this help :)
shader76
04-Oct-2002 08:49
if you need to grab other fields that are not parsed out by imap_header then use
imap_fetchheader to retrieve the entire email header and do the work yourself.
15-May-2002 08:08
I just wanted to state what all these variables were producing considering it isnt documented anywhere that I have been able to find on this site:

the object that imap_mailboxmsgs returns has the following fields for the pop3 driver:

array(
     Unread =>
     Deleted =>
     Nmsgs => 
     Size=>     
     Date=>   
     Driver=> 
     Mailbox=>
     Recent=>
)

imap_header returns the following array for the pop3 driver:

stdClass Object (
   date=>
   Date=>
   subject=>
   Subject=>
   toaddress=>
   to=> array stdClass Object (
       mailbox=>
       host=>
     )
   fromaddress=>
   from=> array stdClass Object (
       personal=>
       mailbox=>
       host=>
   )
   reply_toaddress=>
   reply_to=> array stdClass Object (
       personal=>
       mailbox=>
       host=>
   )
   senderaddress=>
   sender => array stdClass Object (
       personal=>
       mailbox=>
       host=>
   )
   Recent=>
   Unseen=>
   Flagged=>
   Answered=>
   Deleted=>
   Draft=>
   Msgno=>
   MailDate=>
   Size=>
   udate=>
)

Also, if you are using a pop3 server and you are having difficulties deleting messages, you will need to call imap_expunge immediately after your call to imap_delete. (this was posted somewhere else..  thanks for that!!)

feel free to drop me a line if this helped you.  Happy coding.
hnesland at samsen dot com
05-Feb-2002 01:36
A way to print your own specified date:

$date = date('d F Y (G:i:s)',
strtotime(strip_tags($headerinfo->udate)));
jwilson at ecominteractive dot net
07-Sep-2001 11:20
It is explained else where on the site but something I struggled with.

To find the size of a message use:

$sizefeed1 = imap_fetchstructure($feed,$messagenumber);
$sizefeed2 = $sizefeed1->bytes;
$size = "$sizefeed2 bytes";
alpha_lam at yahoo dot com dot hk
11-Feb-2001 12:38
I've got a problem when facing some encoded subject or from header like:
=?big5?B?uXG4o7ZXpEg3MTEw?= or
=?iso-8859-1?Q?A7=EB=B2=BC=B6=7D=A9l=21?=
this problem only appears when the poster is using non-Misco$oft reader, and i lastly find the way to solve.
For the =?big5?B?, the encode method is base64, so you can use base64_decode to decode it to the original one, you can use the following to check and decode:

<?php

if(ereg("=\?.{0,}\?[Bb]\?",$strHead)){
 
$arrHead=split("=\?.{0,}\?[Bb]\?",$strHead);
  while(list(
$key,$value)=each($arrHead)){
   if(
ereg("\?=",$value)){
    
$arrTemp=split("\?=",$value);
    
$arrTemp[0]=base64_decode($arrTemp[0]);
    
$arrHead[$key]=join("",$arrTemp);
   }
  }
 
$strHead=join("",$arrHead);
}

?>

For =?iso-8859-1?Q?, it uses quoted printable to encode, you can use quoted_printable_decode to decode, example:

<?php

if(ereg("=\?.{0,}\?Q\?",$strHead)){
 
$strHead=quoted_printable_decode($strHead);
 
$strHead=ereg_replace("=\?.{0,}\?[Qq]\?","",$strHead);
 
$strHead=ereg_replace("\?=","",$strHead);
}

?>
lint at phpcult dot com
03-Aug-2000 09:51
here's something even easier:
<br>
<pre>
$header = imap_header($mail_connection, msgnum);
$from = htmlspecialchars($header->fromaddress);
echo $from;
</pre>
mayorga at tadecom dot com
03-Aug-2000 02:44
If you want to retrieve From address, not From name, use this:
$from=$header->from; $fromaddr=sprintf("%s@%s",$from[0]->mailbox,$from[0]->host);
G dot li at open dot ac dot uk
06-Jul-2000 06:26
Yes, the ref. is quit confusing (to me at least). It will be wise (after spent many hours) to test the type of variable first before it disappointe you. I find $header->From retrurns String, $header->from returns array and its elements are object. Therefore if you want get sender's name for example,
you need: $from = $header->from;
  
       if (is_array($from)){
      
       while(list($key, $val) = each($from)) {
      
       echo $from[0]->personal;
       echo $from[0]->adl;
       echo $from[0]->mailbox;
       echo $from[0]->host;

       }
       }
you will find $header_from is an one element array and this element is an object. Hope anyone else will not wast time here to figure out why retrun is empty.
ed at lexingrad dot net
18-Apr-2000 07:06
Use something like this to display the number
of new messages:

<pre>
/*$new_messages = 0;
       for( $i=0 ; $i < $nummsg ; $i++ ) {
             $msg = imap_header($mailbox, $msg_array[$i]);
         if ($msg->Unseen == "U"|| $msg->Recent == "N") {
         $new_messages = $new_messages + 1;
               }
       }
       echo "<br>  $new_messages  NEW Messages";*/
</pre>
uncommented of course  : ~)
hpkevertje at hotmail dot com
27-Mar-2000 10:31
To use the from[] and to[] functions, try this:
<PRE>$from=header->from;</PRE>
<PRE>$fromadr=$from->presonal;</PRE>
aragorn at afrodita dot phys,msu dot su
25-Mar-2000 11:34
Message size can be obtained by using this statement:
<PRE>
$str = imap_fetchstructure($mbox,$msgn);
</PRE>
size in bytes is <B>$str->bytes</B>.