Here is code to parse and decode all types of messages, including attachments. I've been using something like this for awhile now, so it's pretty robust.
<?
function getmsg($mbox,$mid) {
global $charset,$htmlmsg,$plainmsg,$attachments;
$htmlmsg = $plainmsg = $charset = '';
$attachments = array();
$h = imap_header($mbox,$mid);
$s = imap_fetchstructure($mbox,$mid);
if (!$s->parts) getpart($mbox,$mid,$s,0); else { foreach ($s->parts as $partno0=>$p)
getpart($mbox,$mid,$p,$partno0+1);
}
}
function getpart($mbox,$mid,$p,$partno) {
global $htmlmsg,$plainmsg,$charset,$attachments;
$data = ($partno)?
imap_fetchbody($mbox,$mid,$partno): imap_body($mbox,$mid); if ($p->encoding==4)
$data = quoted_printable_decode($data);
elseif ($p->encoding==3)
$data = base64_decode($data);
$params = array();
if ($p->parameters)
foreach ($p->parameters as $x)
$params[strtolower($x->attribute)] = $x->value;
if ($p->dparameters)
foreach ($p->dparameters as $x)
$params[strtolower($x->attribute)] = $x->value;
if ($params['filename'] || $params['name']) {
$filename = ($params['filename'])? $params['filename'] : $params['name'];
$attachments[$filename] = $data; }
if ($p->type==0 && $data) {
if (strtolower($p->subtype)=='plain')
$plainmsg. = trim($data) ."\n\n";
else
$htmlmsg. = $data ."<br><br>";
$charset = $params['charset']; }
elseif ($p->type==2 && $data) {
$plainmsg. = $data."\n\n";
}
if ($p->parts) {
foreach ($p->parts as $partno0=>$p2)
getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1)); }
}
?>