Re: Parsing a stdClass Object created with mimeDecode

From: Date: Fri, 12 May 2006 11:12:14 +0000
Subject: Re: Parsing a stdClass Object created with mimeDecode
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-236056@lists.php.net to get a copy of this message
Graham Anderson wrote:
Hi I am trying to get the body text from a number of different emails using mimeDecode [Pear] Unfortunately, it appears that the body text could be in any number of locations in the stdClass Object depending on which email is processed. At this point, I can only access the node by looking at the print_r ($structure) directly :(
sounds like you have a simpleXML object or something similar - with the freaking overloading madness that break my head every time I work with it. BUT if it is a StdObject try casting it to an array. $myArr = (array) $myObj; that should allow you to recursively traverse $myArr using a fucntion that employs a foreach() loop (and calls it self for every value
Out of the three different emails I parsed, I got three different locations for the 'body' node $structure->parts[0]->parts[0]->parts[0]->body; $structure->body; $structure->parts[0]->body; Question: Is there a way to generically traverse the entire object generated from Mail_mimeDecode and search for the correct node ? In my case, these conditions need to be met . if (trim($part->ctype_primary)== "text" && trim($part- >ctype_secondary) == "plain") { $body = $part->body; echo "the body text is: $body"; } } I am a bit new to stdClass Object and Mail_mimeDecode so any help is appreciated This script was taken from a previous example on http:// www.troywolf.com/articles/php/class_xml/ The script currently is:
    include('Mail/mimeDecode.php');
    $filename = "email_multi.txt";
    $message  = fread(fopen($filename, 'r'), filesize($filename));
    header('Content-Type: text/plain');
    header('Content-Disposition: inline; filename="stuff.txt"');
    $params = array(
                    'input'          => $message,
                    'crlf'           => "\r\n",
                    'include_bodies' => TRUE,
                    'decode_headers' => TRUE,
                    'decode_bodies'  => TRUE
                    );
    $structure = Mail_mimeDecode::decode($params);
// This only works if you know specifically where there node/Object lives :( foreach($structure->parts[0]->parts[0]->parts as $part) { //for debugging echo $part->ctype_primary."|".$part->ctype_secondary."\n\r"; // if these conditions are met, we found the right node if (trim($part->ctype_primary)== "text" && trim($part- >ctype_secondary) == "plain") { $body = $part->body; echo "body is: $body"; } } ?>


« previous php.general (#236056) next »