Zafar,
MIME mail with PHP is pretty tricky. About the only way to do it is to loop through the message structure with a recursive function.
I personally use a recursive function from within a class of mail functions that creates an array of objects out of the sections, like this:
function _fetch_structure($structure, $section_num = 0) {
if (!$section_num && !$structure->parts) {
$section_num = 1;
}
$this->structure[$section_num] = new SECTION($structure);
if ($structure->parts) {
$count = count($structure->parts);
if ($this->structure[$section_num]->type == "multipart") { $section_num = ereg_replace(".[0-9]$", "", $section_num); }
for ($i = 0, $j = 1; $i < $count; $i++, $j++) {
$this_section_num = ($section_num)? "$section_num.$j" : $j;
$this->_fetch_structure($structure->parts[$i], $this_section_num);
}
}
}
class SECTION {
var $structure;
var $type;
var $encoding;
var $subtype;
var $bytes;
var $size;
var $disposition;
var $filename;
var $charset;
function SECTION($structure) {
$mime[0]="text";
$mime[1]="multipart";
$mime[2]="message";
$mime[3]="application";
$mime[4]="audio";
$mime[5]="image";
$mime[6]="video";
$mime[7]="other";
$encoding[0]="7bit";
$encoding[1]="8bit";
$encoding[2]="binary";
$encoding[3]="base64";
$encoding[4]="quoted-printable";
$encoding[5]="other";
$this->type = $mime[$structure->type];
$this->encoding = $encoding[$structure->encoding];
$this->subtype = strtolower($structure->subtype);
if ($structure->bytes) {
$this->bytes = $structure->bytes;
$this->size = format_size($structure->bytes);
}
if ($structure->ifdisposition) { $this->disposition = strtolower($structure->disposition); }
if ($structure->ifparameters) {
while (list($key,$val) = each($structure->parameters)) {
if (strtoupper($val->attribute) == "NAME" || strtoupper($val->attribute) == "FILENAME") {
$this->filename = $this->_header_decode($val->value);
}
if (strtoupper($val->attribute) == "CHARSET") {
$this->charset = $val->value;
}
}
}
if ($structure->ifdparameters) {
while (list($key,$val) = each($structure->dparameters)) {
if ( !$this->filename && (strtoupper($val->attribute) == "NAME" || strtoupper($val->attribute) == "FILENAME") ) {
$this->filename = $this->_header_decode($val->value);
}
if (!$this->charset && strtoupper($val->attribute) == "CHARSET") {
$this->charset = $val->value;
}
}
}
}
function _header_decode($str) {
$str = imap_mime_header_decode($str); // this function returns an array of an object? what's up with that?
if (is_array($str)) {
foreach($str as $s) {
$str = $s->text;
}
}
return $str;
}
}
From there, you can use the objects to loop through the sections and either present each one inline using the correct encoding, or, if the section is an attachment, provide a link from the main message that will feed the individual section to the browser "raw," and with the correct headers.
In my opinion, coding an efficient and well-organized imap client for PHP requires extensive use of objects. I've found that even some people with experience coding in PHP can find it pretty confusing.
I hope this helps get you on the right track.