2012-10-08 79 views
1

我試圖使用PHP郵件()函數發送帶附件的電子郵件。郵件得到傳遞,但在Web郵件文件名中顯示爲「untitle」。隨着Outlook的Avast啓發式塊完全阻止電子郵件。PHP郵件()附件文件名顯示爲「untitle」

請指導我解決這個問題。

這裏是我使用

$recieverAdd = '[email protected]'; 
$subject = 'Attachment'; 
$attDescription="emailing lessons"; 
$filename ='example.pdf'; 
$file = '/home/user/example.pdf'; 
$file_size = filesize($file); 
$handle = fopen($file, "r"); 
$content = fread($handle, $file_size); 
fclose($handle); 
$content = chunk_split(base64_encode($content)); 
$uid = md5(uniqid(time())); 
$name = basename($file); 

$from_name = $uname; 
$from_mail = '[email protected]'; 
$replyto ='[email protected]'; 
$header = "From: $from_name";  
$header .= "\nMIME-Version: 1.0\n" . 
    "Content-Type: multipart/mixed;\n" . 
    " boundary=\"{$uid}\""; 

$message = "<p><b>Hello </b></p>"; 
$messageBody = "--{$uid}\n" . "Content-Type: text/html; charset=\"iso-8859-1    \"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$messageBody .= "--{$uid}\n". "Content-Type: application/octet-stream;\n". 
     "name=\"{$filename1}\"\n". 
     "Content-Description: {$filename}\n" . 
     "Content-Disposition: attachment;\n". 
     "filename=\"{$filename}\"; size={$file_size};\n" . 
     "Content-Transfer-Encoding: base64\n\n". $content."\n\n". "--{$uid}--\n"; 

mail($recieverAdd,$subject,$messageBody,$header) 
+4

什麼是'$ filename1'做的名字嗎? – Rikesh

+0

這是錯字。實際上它是代碼 – Rohini

+0

中的$ filename,所有這些與神祕的郵件頭語法混淆是不必要的。只需使用更好的郵件類而不是php的內置郵件()函數。我推薦使用[phpMailer](http://code.google.com/a/apache-extras.org/p/phpmailer/)。它使用起來非常容易,而且更不用擔心有問題。 – SDC

回答

2
"Content-Type: application/octet-stream;\n". 
"name=\"{$filename1}\"\n". 

"Content-Disposition: attachment;\n". 
"filename=\"{$filename}\"; size={$file_size};\n" . 

name=...filename=...部件這些標題被認爲是獨立的(無效)標題行,因爲你Content-Type: application/octet-stream;Content-Disposition: attachment;後輸出一個新行代碼,但在這些換行符後面沒有空格。爲了將一個標題分成多行,每個延續行必須以空格開頭。

+0

感謝lanzz和其他人的回覆。這非常有幫助。我整天都在用這段代碼敲我的頭。 – Rohini

1

我想這可能對您有所幫助,

請找到下面的例子:

<?php  

    $my_file = "somefile.zip"; 
    $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/"; 
    $my_name = "Olaf Lederer"; 
    $my_mail = "[email protected]"; 
    $my_replyto = "[email protected]"; 
    $my_subject = "This is a mail with attachment."; 
    $my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf"; 
    mail_attachment($my_file, $my_path, "[email protected]", $my_mail, $my_name, $my_replyto, $my_subject, $my_message); 

?> 

<?php 
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { 
    $file = $path.$filename; 
    $file_size = filesize($file); 
    $handle = fopen($file, "r"); 
    $content = fread($handle, $file_size); 
    fclose($handle); 
    $content = chunk_split(base64_encode($content)); 
    $uid = md5(uniqid(time())); 
    $name = basename($file); 
    $header = "From: ".$from_name." <".$from_mail.">\r\n"; 
    $header .= "Reply-To: ".$replyto."\r\n"; 
    $header .= "MIME-Version: 1.0\r\n"; 
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
    $header .= "This is a multi-part message in MIME format.\r\n"; 
    $header .= "--".$uid."\r\n"; 
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; 
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
    $header .= $message."\r\n\r\n"; 
    $header .= "--".$uid."\r\n"; 
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here 
    $header .= "Content-Transfer-Encoding: base64\r\n"; 
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; 
    $header .= $content."\r\n\r\n"; 
    $header .= "--".$uid."--"; 
    if (mail($mailto, $subject, "", $header)) { 
     echo "mail send ... OK"; // or use booleans here 
    } else { 
     echo "mail send ... ERROR!"; 
    } 
} 
?>