note 12485 modified in function.imap-fetchstructure by felipe

From: Date: Wed, 23 Jul 2008 12:58:28 +0000
Subject: note 12485 modified in function.imap-fetchstructure by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142594@lists.php.net to get a copy of this message
Parsing the recursive object returned from imap_fetchstructure is easy. However trying to figure out the name of the section to pass to imap_fetchbody() isn't quite so straight forward because the naming conventions for the MIME sections are odd. If you copy and paste the following function you can pass an UID of an IMAP message and it will print out a hierarchy of the message and label each section. The only required parameters are $mbox and $em_uid. The other parameters are used when the function needs to call itself. Two functions mime_encoding() and mime_type() are not included. All they do is return string for the integer represention of a mime type/encoding. <?php function imap_dbody($mbox, $em_uid, $mimeobj, $depth = 0, $section = 0) { // If this is the first run of the function then grab a object of the MIME structure of the message if($section == 0) $mimeobj = imap_fetchstructure($mbox, $em_uid, FT_UID); for($y = 0; $y < $depth; $y++) echo("       "); echo(mime_type($mimeobj->type) . "/{$mimeobj->subtype}, "); echo(mime_encoding($mimeobj->encoding) . " (<B>$section</B>)<BR>"); for($x = 0; $x < count($mimeobj->parts); $x++) { // If we are in the root of the object increment by whole integers if($section == 0) $nsection = $x + 1; // If we are in the object and the first sub-object of our object isn't multipart // then increment the postfix by ".1" otherwise we are multipart or a message // and leave the section id alone to be handled by the next code block else if(($pos = strrpos($section, ".")) && $mimeobj->parts[0]->type != TYPEMULTIPART) $nsection = substr($section, 0, $pos) . "." . ($x + 1); else $nsection = $section; // If there are more parts to the part about to be processed reference it as a header with ".0" // but only if the child of this child isn't MULTIPART if(count($mimeobj->parts[$x]->parts)) { // Funny really, if a mime section is a inline message that has a multipart body you reference the message // mime section with "2" the inline message header with "2.0" and the subsections with "2.x" // However if the mime section is a inline message with only 1 part then you reference the // mime section in the message with 2.0 and the inline message body with 2.1 if(!($mimeobj->parts[$x]->type == TYPEMESSAGE && $mimeobj->parts[$x]->parts[0]->type == TYPEMULTIPART)) $nsection .= ".0"; else $nsection .= ""; } imap_dbody($mbox, $em_uid, $mimeobj->parts[$x], $depth + 1, $nsection); } // If after processing the entire MIME object the $x variable is still zero then we didn't // process a multipart mime message, it's just normal email so say so here. if($x == 0 && $section == 0) { echo(mime_type($mimeobj->type) . "/{$mimeobj->subtype}, "); echo(mime_encoding($mimeobj->encoding) . " (<B>1</B>) (<B>NOT MIME MULTIPART</B>)<BR>"); } } ?> --was-- Parsing the recursive object returned from imap_fetchstructure is easy. However trying to figure out the name of the section to pass to imap_fetchbody() isn't quite so straight forward because the naming conventions for the MIME sections are odd. If you copy and paste the following function you can pass an UID of an IMAP message and it will print out a hierarchy of the message and label each section. The only required parameters are $mbox and $em_uid. The other parameters are used when the function needs to call itself. Two functions mime_encoding() and mime_type() are not included. All they do is return string for the integer represention of a mime type/encoding. function imap_dbody($mbox, $em_uid, $mimeobj, $depth = 0, $section = 0) { // If this is the first run of the function then grab a object of the MIME structure of the message if($section == 0) $mimeobj = imap_fetchstructure($mbox, $em_uid, FT_UID); for($y = 0; $y < $depth; $y++) echo("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"); echo(mime_type($mimeobj->type) . "/{$mimeobj->subtype}, "); echo(mime_encoding($mimeobj->encoding) . " (<B>$section</B>)<BR>"); for($x = 0; $x < count($mimeobj->parts); $x++) { // If we are in the root of the object increment by whole integers if($section == 0) $nsection = $x + 1; // If we are in the object and the first sub-object of our object isn't multipart // then increment the postfix by ".1" otherwise we are multipart or a message // and leave the section id alone to be handled by the next code block else if(($pos = strrpos($section, ".")) && $mimeobj->parts[0]->type != TYPEMULTIPART) $nsection = substr($section, 0, $pos) . "." . ($x + 1); else $nsection = $section; // If there are more parts to the part about to be processed reference it as a header with ".0" // but only if the child of this child isn't MULTIPART if(count($mimeobj->parts[$x]->parts)) { // Funny really, if a mime section is a inline message that has a multipart body you reference the message // mime section with "2" the inline message header with "2.0" and the subsections with "2.x" // However if the mime section is a inline message with only 1 part then you reference the // mime section in the message with 2.0 and the inline message body with 2.1 if(!($mimeobj->parts[$x]->type == TYPEMESSAGE && $mimeobj->parts[$x]->parts[0]->type == TYPEMULTIPART)) $nsection .= ".0"; else $nsection .= ""; } imap_dbody($mbox, $em_uid, $mimeobj->parts[$x], $depth + 1, $nsection); } // If after processing the entire MIME object the $x variable is still zero then we didn't // process a multipart mime message, it's just normal email so say so here. if($x == 0 && $section == 0) { echo(mime_type($mimeobj->type) . "/{$mimeobj->subtype}, "); echo(mime_encoding($mimeobj->encoding) . " (<B>1</B>) (<B>NOT MIME MULTIPART</B>)<BR>"); } } http://php.net/manual/en/function.imap-fetchstructure.php

« previous php.notes (#142594) next »