downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

imap_headerinfo> <imap_getsubscribed
[edit] Last updated: Fri, 26 Apr 2013

view this page in

imap_header

(PHP 4, PHP 5)

imap_headerAlias of imap_headerinfo()

Description

This function is an alias of: imap_headerinfo().



imap_headerinfo> <imap_getsubscribed
[edit] Last updated: Fri, 26 Apr 2013
 
add a note add a note User Contributed Notes imap_header - [12 notes]
up
1
Anang Syarifudin A
5 years ago
My quick and dirty code to decode utf-8 subject :

function decode_utf8($str) {
   preg_match_all("/=\?UTF-8\?B\?([^\?]+)\?=/i",$str, $arr);
       
   for ($i=0;$i<count($arr[1]);$i++){
     $str=ereg_replace(ereg_replace("\?","\?",
              $arr[0][$i]),base64_decode($arr[1][$i]),$str);
   }
        return $str;
}

$subject = decode_utf8($header->subject);
up
0
jose dot yapur at usmp dot pe
3 years ago
Some example of how to use it:

<?php
    $imap
= imap_open("{mail.example.com:143}INBOX", "username", "password");
   
$message_count = imap_num_msg($imap);

    for (
$i = 1; $i <= $message_count; ++$i) {
       
$header = imap_header($imap, $i);
       
$body = trim(substr(imap_body($imap, $i), 0, 100));
       
$prettydate = date("jS F Y", $header->udate);

        if (isset(
$header->from[0]->personal)) {
           
$personal = $header->from[0]->personal;
        } else {
           
$personal = $header->from[0]->mailbox;
        }

       
$email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
        echo
"On $prettydate, $email said \"$body\".\n";
    }

   
imap_close($imap);
?>
up
0
stepotronic at nimbleminds dot net
5 years ago
At jeremy at caramiel dot com:
You are wrong, since it will just work if the encoding of the characters is also used in the current context.
There is a reason why it comes with the charset :)

There are way better solutions with imap_mime_header_decode
up
0
jeremy at caramiel dot com
5 years ago
Here is a clean way to decode encoded imap headers that will work in all cases :

function fix_text($str)
{
    $subject = '';
    $subject_array = imap_mime_header_decode($str);

    foreach ($subject_array AS $obj)
        $subject .= rtrim($obj->text, "\t");

    return $subject;
}
up
0
khigashi dot oang at gmail dot com
6 years ago
A simple way to fix encoded subject/from header problem and strip whitespace:

<?php
function fix_text($var){
if(
ereg("=\?.{0,}\?[Bb]\?",$var)){
$var = split("=\?.{0,}\?[Bb]\?",$var);

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

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

Ex.

<?php
//For =?iso-8859-1?Q Problem

echo $title;
//show: =?iso-8859-1?Q?Boletim:_Motiva=E7=E3o
//,_Gest=E3o_&_Vendas_-_Gilcl=E9r_Regi?=
//=?iso-8859-1?Q?na?=

echo fix_text($title);
//show: na?
?>
up
0
shader76
10 years ago
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.
up
0
Anonymous
10 years ago
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.
up
0
hnesland at samsen dot com
11 years ago
A way to print your own specified date:

$date = date('d F Y (G:i:s)',
strtotime(strip_tags($headerinfo->udate)));
up
0
jwilson at ecominteractive dot net
11 years ago
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";
up
0
alpha_lam at yahoo dot com dot hk
12 years ago
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);
}

?>
up
0
G dot li at open dot ac dot uk
12 years ago
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.
up
-1
gungp720 at yahoo dot com
10 years ago
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 :)

 
show source | credits | stats | sitemap | contact | advertising | mirror sites