I've set email interception on my small server.

following is my email forwarder set on server

testemail@my.server.com,"/home/server/php_pipe_mail.php"

following is my code for php_pipe_mail.php

#!/usr/bin/php -q
<?php 

require_once('mimeDecode.php');
include('sql-connect.php');
error_reporting(E_ALL);


ob_start();


$raw_email = '';



if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin \n";
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}



$raw_email = preg_replace('/ +/', ' ', $raw_email);


var_dump($raw_email);


$buf = ob_get_contents();

$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$params['input'] = $buf;
$params['crlf'] = "\r\n"; 

//Creating temp file on server 
$myFile = "amail.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $buf);
fclose($fh);


//Generating mail structure in object format
$structure = Mail_mimeDecode::decode($params); 
$attachment = array();

$mail_date= date( 'Y-m-d H:i:s', strtotime($structure->headers['date']) );
$from = $structure->headers['from'];
$to = $structure->headers['to'];
$subject = htmlentities($structure->headers['subject'],ENT_QUOTES);
if($structure->ctype_primary == "multipart")
{
    $body_text = $structure->parts[0]->parts[0]->body;
$body_html = $structure->parts[0]->parts[1]->body;

$x = 0;
//fetch attachment
foreach ($structure->parts as $part) {
    // only save if an attachment
    if (isset($part->disposition) and ($part->disposition=='attachment')) {
        $attachment[$x]["filename"] = $part->d_parameters['filename'];
        $attachment[$x]["content_type"] = $part->ctype_primary . "/" .          $part->ctype_secondary;
        $attachment[$x]["body"] = addslashes($part->body);
        $x++;
    }
}
}
else 
{
$body_text = $structure->parts[0]->body;
$body_html = $structure->parts[1]->body;
}


$qry1 = "insert into mail_buffer(mail_date,mail_from,     mail_to,mail_subject,mail_text_body,mail_html_body) Values('". $mail_date ."','".$from."','".$to."','".$subject."','".$body_text."','".$body_html."')";

mysql_query($qry1) or die(mysql_error($con));

$last_id = mysql_insert_id();

if(count($attachment) > 0)
{
for($i=0; $i < count($attachment); $i++)
{
    $qry = "insert into mail_attachment(email_id,content_type, file_name,body) Values('". $last_id ."','".$attachment[$i]['content_type']."','".$attachment[$i]['filename']."','".$attachment[$i]['body']."')";
    mysql_query($qry) or die(mysql_error($con));
}
}



mysql_close($con);

ob_end_clean();

?>

Now above script works perfectly fine.

I can fetch message header, body and accessories and may store them in database with no problems.

When email without accessories come everything works fine and email is shipped to current email address I'm intercepting.

But following isn't working.

When email with accessories comes than email submissions are being saved in database but email isn't delivering to current email address I'm intercepting and i'm getting following error message in recover email.

A note that you simply sent couldn't be shipped to a number of of their readers. This can be a permanent error. The next address(es) unsuccessful:

pipe to /home/server/php_pipe_mail.php produced by testemail@my.server.com

Can anybody assist me to concerning the matter.

Thanks.

Can it be that, when an attachment exists, your script is echoing something? I've had problems piping emails before, and seen failure messages came back to senders, and they've been because of the piping script creating some type of output. Maybe your error_reporting(E_ALL); is permitting the script to create an output - try error_reporting(0);