PHP Classes

Extract the Body Text

Recommend this page to a friend!

      PHP MIME Email Message Parser  >  All threads  >  Extract the Body Text  >  (Un) Subscribe thread alerts  
Subject:Extract the Body Text
Summary:How do I get the Body text
Messages:6
Author:Mark A. Rupert
Date:2012-01-10 21:44:21
Update:2012-01-14 00:33:05
 

  1. Extract the Body Text   Reply   Report abuse  
Picture of Mark A. Rupert Mark A. Rupert - 2012-01-10 21:44:21
I have been working for a few hours testing the parser and have been unsuccessful in getting the text from the body. I am using your test code and am passing it a string. It parses successfully but if I understand a successful call to "$mime->Analyze($decoded[$message], $results)" should yield the message body in $results['Body']. Do I have the right? I have made sure I have 'SkipBody'=>0. The body seems not to be present. Any ideas? My code is below. THanks, Mark

$mime=new mime_parser_class;

/*
* Set to 0 for parsing a single message file
* Set to 1 for parsing multiple messages in a single file in the mbox format
*/
$mime->mbox = 0;

/*
* Set to 0 for not decoding the message bodies
*/
$mime->decode_bodies = 1;

/*
* Set to 0 to make syntax errors make the decoding fail
*/
$mime->ignore_syntax_errors = 1;

/*
* Set to 0 to avoid keeping track of the lines of the message data
*/
$mime->track_lines = 1;

/*
* Set to 1 to make message parts be saved with original file names
* when the SaveBody parameter is used.
*/
$mime->use_part_file_names = 0;

$parameters=array(
'Data'=>$mytempstr,

/* Read a message from a string instead of a file */
/* 'Data'=>'My message data string', */

/* Save the message body parts to a directory */
/* 'SaveBody'=>'/tmp', */

/* Do not retrieve or save message body parts */
'SkipBody'=>0,
);

/*
* The following lines are for testing purposes.
* Remove these lines when adapting this example to real applications.
*/
if(defined('__TEST'))
{
if(IsSet($__test_options['parameters']))
$parameters=$__test_options['parameters'];
if(IsSet($__test_options['mbox']))
$mime->mbox=$__test_options['mbox'];
if(IsSet($__test_options['decode_bodies']))
$mime->decode_bodies=$__test_options['decode_bodies'];
if(IsSet($__test_options['use_part_file_names']))
$mime->use_part_file_names=$__test_options['use_part_file_names'];
}

if(!$mime->Decode($parameters, $decoded))
{
echo 'MIME message decoding error: '.$mime->error.' at position '.$mime->error_position;
if($mime->track_lines
&& $mime->GetPositionLine($mime->error_position, $line, $column))
echo ' line '.$line.' column '.$column;
echo "\n";
}
else
{
echo 'MIME message decoding successful.'."\n";
echo (count($decoded)==1 ? '1 message was found.' : count($decoded).' messages were found.'),"\n";
for($message = 0; $message < count($decoded); $message++)
{
echo 'Message ',($message+1),':',"\n";
var_dump($decoded[$message]);
if($mime->decode_bodies)
{
if($mime->Analyze($decoded[$message], $results))
var_dump($results);
else
echo 'MIME message analyse error: '.$mime->error."\n";
}
}
for($warning = 0, Reset($mime->warnings); $warning < count($mime->warnings); Next($mime->warnings), $warning++)
{
$w = Key($mime->warnings);
echo 'Warning: ', $mime->warnings[$w], ' at position ', $w;
if($mime->track_lines
&& $mime->GetPositionLine($w, $line, $column))
echo ' line '.$line.' column '.$column;
echo "\n";
}
}

  2. Re: Extract the Body Text   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-01-11 01:14:33 - In reply to message 1 from Mark A. Rupert
You need to remove the SkipBody parameter completely as the class only checks for its presence.

  3. Re: Extract the Body Text   Reply   Report abuse  
Picture of Mark A. Rupert Mark A. Rupert - 2012-01-11 13:18:59 - In reply to message 2 from Manuel Lemos
Thanks,

That made the Body show up in the $decoded[$message] variable but it does not seem to be decoded. I expected it to show up with the text of the email after the $mime->Analyse() call. Am I missing something. Below is a dump of my results. Thanks, Mark

MIME message decoding successful. 1 message was found. Message 1: array(6) { ["Headers"]=> array(0) { } ["Parts"]=> array(0) { } ["Position"]=> int(0) ["Body"]=> string(806) "This is a multipart message in MIME format. ------=_NextPart_000_00FE_01CCD038.D26B38D0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Wednesday Morn 2 ------=_NextPart_000_00FE_01CCD038.D26B38D0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
Wednesday Morn = 2

------=_NextPart_000_00FE_01CCD038.D26B38D0-- " ["BodyPart"]=> int(1) ["BodyLength"]=> int(806) } array(3) { ["Type"]=> string(4) "text" ["Description"]=> string(12) "Text message" ["Data"]=> string(806) "This is a multipart message in MIME format. ------=_NextPart_000_00FE_01CCD038.D26B38D0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Wednesday Morn 2 ------=_NextPart_000_00FE_01CCD038.D26B38D0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
Wednesday Morn = 2

------=_NextPart_000_00FE_01CCD038.D26B38D0-- "

  4. Re: Extract the Body Text   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-01-11 19:39:27 - In reply to message 3 from Mark A. Rupert
Maybe you are not passing the message data to the class correctly. Can you upload the message data somewhere so I can examine it?

  5. Re: Extract the Body Text   Reply   Report abuse  
Picture of Mark A. Rupert Mark A. Rupert - 2012-01-12 16:19:13 - In reply to message 4 from Manuel Lemos
Yep, that was it. I was passing it the results of getBody() instead of getMsg(). Once I passed getMsg() I was able to find the data in $results['Data']

Thanks for your help and you code!!!

Mark

  6. Re: Extract the Body Text   Reply   Report abuse  
Picture of Nguy?n V?n ???c Nguy?n V?n ???c - 2012-01-14 00:33:05 - In reply to message 1 from Mark A. Rupert
$regex = '#\<span class=quangle(.+?)\>(.+?)\<\/span\>#s';
function get_content_tag($url,$regex )
{
$content = Get_content($url);
preg_match_all($regex, $content, $matches);
$Content = preg_replace("/&#?[a-z0-9]{2,8};/i","",$matches[0][0]);
return strip_tags($Content);
}

i use this code to get content inside tag