PHP Classes

Nested bodyparts?

Recommend this page to a friend!

      PHP MIME Email Message Parser  >  All threads  >  Nested bodyparts?  >  (Un) Subscribe thread alerts  
Subject:Nested bodyparts?
Summary:How do you handle nested bodyparts?
Messages:6
Author:Edward Hibbert
Date:2012-07-22 14:50:25
Update:2012-07-23 22:15:57
 

  1. Nested bodyparts?   Reply   Report abuse  
Picture of Edward Hibbert Edward Hibbert - 2012-07-22 14:50:25
I am trying to find the text part of a message. My code looks a bit like this:

if ($mime->Decode($parameters, $decoded))
{

foreach ($decoded[0]['Parts'] as $part)
{
if (strpos($part['Headers']['content-type:'], "text/plain") !== false)
{
$textbody = $part['Body'];
break;
}
}
}

This doesn't work when the text/plain is embedded within a multipart/alternative. Is there a recommended way to recurse through nested bodyparts?

  2. Re: Nested bodyparts?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-07-22 23:37:48 - In reply to message 1 from Edward Hibbert
When a message part contains other parts, there is an array entry with key name Parts that details the contained parts.

Anyway, the Analyze function can do that analysis for you. Is there any reason why you are not using the Analyze function?

  3. Re: Nested bodyparts?   Reply   Report abuse  
Picture of Edward Hibbert Edward Hibbert - 2012-07-23 08:07:52 - In reply to message 2 from Manuel Lemos
Thanks for your reply.

Do you have sample code for use of Parts?

I use the Analyze function but not for getting bodyparts? I've looked at the doc at http://corporate.psionline.com/parser/mime_parser_class.html#9.2.4 and it's not clear to me how it would handle multipart/alternative (for example whether it would return an HTML or a text if both are present).

  4. Re: Nested bodyparts?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-07-23 09:12:38 - In reply to message 3 from Edward Hibbert
Analyze returns both parts as long as Decode returns all body parts, so you cannot use the SkipBody option.

  5. Re: Nested bodyparts?   Reply   Report abuse  
Picture of Edward Hibbert Edward Hibbert - 2012-07-23 19:48:52 - In reply to message 4 from Manuel Lemos
I found it easier to use the Decode. Code here if anyone needs it.

function getTextBP($logh, $parts)
{
$textbody = '';

foreach ($parts as $part)
{
fwrite($logh , "Process bodypart\n");
fwrite($logh , "ctype " . $part['Headers']['content-type:'] . "\n");

if (strpos($part['Headers']['content-type:'], "multipart/alternative") !== false)
{
return(getTextBP($logh, $part['Parts']));
}
else if (strpos($part['Headers']['content-type:'], "text/plain") !== false)
{
fwrite($logh, "Got text/plain\n");
$textbody = $part['Body'];
break;
}
}

return($textbody);
}

$textbody = getTextBP($logh, $decoded['0']['Parts']);

  6. Re: Nested bodyparts?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-07-23 22:15:57 - In reply to message 5 from Edward Hibbert
Well, that is what the Analyze function does, except that it also returns the HTML body as the main body part according to the e-mail standards.