w e l c o m e . t o . t r o g ' s . h a u s
image display:




Viewing projects/OEAttachStrip/OEAttachStrip.phps

<?php
/*
 * A super-hacky script to quickly rip apart an Outlook Express-esque plain text .eml email, 
 * pull out the attachments, spit them out along with a plain-text version of the email. 
 * It is really simple and probably won't work for you, so don't bother. 
 */
require_once "Mail/mimeDecode.php";
require_once 
"Mail/mime.php";

// Put in your output directory here. 
$outputdir "C:/Documents and Settings/YOURDIR/Desktop";

if (isset(
$_SERVER['argv'][1]))
    
$filename $_SERVER['argv'][1];
else
    die(
"No filename supplied\n");

$input file_get_contents($filename);

$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$params['input'] = $input;

$structure Mail_mimeDecode::decode($params);

//print_r($structure);

$i=0;
// Loop through all the parts and look for attachments
foreach ($structure->parts as $part)
{
    if (isset(
$part->disposition) && ($part->disposition=='attachment') )
    {
        
// At the moment we only look in these few fields to see if we can find a stored filename (only light testing with a handful of sample emails was done)
        
if (isset($part->ctype_parameters['filename']))
            
$outfile $part->ctype_parameters['filename'];
        else if (isset(
$part->d_parameters['filename']))
            
$outfile $part->d_parameters['filename'];
        else
            
$outfile "file$i.unknown";
        
        
$outfile $outputdir."/".$outfile;
        
        print 
"* Saving attachment as $outfile\n";    

        
$fp fopen($outfile'w');
        
fwrite($fp$part->body);
        
fclose($fp);
    }
    
$i++;
}
// Create the revised, rewritten email file 
$outputemail $outputdir."/noattach-".basename($filename);
$fp fopen($outputemail"w");

foreach(
$structure->headers as $header=>$val)
{
    if (
is_array($val))
    {
        foreach (
$val as $subval)
            
fputs($fpucfirst($header).": $subval\r\n");
    }
    else
    {
        
// If the header has the multipart stuff in it, let's leave it out
        
if ($header == "content-type")
            continue;        

        
fputs($fp,ucfirst($header).": $val\r\n");
    }
}
fputs($fp"\r\n");

// As above, lazily look in a couple of likely places for the email body.
// FIXME: this should be rewritten to loop through the parts until a matching body is found and each body segment re-written. I only care about one/plain text email copy so don't mind being lazy
if (isset($structure->parts[0]->body))
    
$body $structure->parts[0]->body;
else if (isset(
$structure->parts[0]->parts[0]->body))
    
$body $structure->parts[0]->parts[0]->body;
else
    
$body $structure->parts[0]->parts[0]->parts[0]->body;
    
if (
trim($body) != "")
    
fputs($fp$body);    
else
{
    
// If we can't find the body, fail horribly
    
print "*** ERROR: No body!\n";
    
unlink($outputemail);
}
fclose($fp);

?>


index of root >> projects >> OEAttachStrip >> OEAttachStrip.phps >>


OEAttachStrip.phps
2,690



Last modified: