2011-05-28 214 views
0

我在我的服務器上設置了電子郵件攔截。PHP電子郵件攔截不發送帶附件的電子郵件

以下是我的電子郵件轉發服務器上

[email protected], 「/家庭/服務器/ php_pipe_mail.php」

以下設置是我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(); 

?> 

現在上面的腳本工作得很好。

我可以去取郵件標題,正文和附件,可以將它們在數據庫中存儲沒有任何問題。

當不帶附件的電子郵件來一切工作正常,將電子郵件發送到電子郵件地址,我攔截。

但下面不工作。

當附件的郵件來比電子郵件的內容被存儲在數據庫中,但電子郵件沒有提供電子郵件地址,我攔截和我在反彈電子郵件收到以下錯誤消息。

您發送無法交付給一個或多個其 收件人的消息。這是一個永久性錯誤。下面的地址(ES)失敗:以

管| /home/server/php_pipe_mail.php 通過[email protected]產生

誰能幫助我關於此事。

謝謝。

+0

考慮使用http://swiftmailer.org/ – 2011-05-28 10:55:01

+0

丹尼斯,你能給我一些關於它如何幫助我解決我的問題的想法。代碼中有什麼不對嗎? – amar4u 2011-05-28 10:58:27

回答

0

難道說,當附件存在,你的腳本是呼應的東西嗎?我之前遇到過管道電子郵件問題,並且看到故障消息返回給發件人,並且它們是由於管道腳本產生某種輸出。也許你的error_reporting(E_ALL);允許腳本產生一個輸出 - 試試