2011-01-25 89 views
0

亲爱的朋友们, 我有文件附件的问题,我正在使用电子邮件功能附件为我的项目,我附带file.xls与该邮件,但在邮件我不能得到该文件的值,其显示0K。 但在本地该文件是有一些DATAS,但它不工作时运行的电子邮件功能,附件有0K大小,任何人都可以帮我请... 我的代码如下....发送带附件的电子邮件(问题是文件大小为0k)

$filename = "test.xls"; 
    $path = $_SERVER['DOCUMENT_ROOT']."/path/to/Documents/"; 
    $file = $path.$filename; 

    $to = "[email protected]"; 
    $subject = 'Repairs List Report'; 

    $message = "Please find the attachment file for repairs list of Last week..."; //strip_tags($_POST['message']); 
    $attachment = chunk_split(base64_encode(file_get_contents($file))); 
    //$filename = $_FILES['file']['name']; 

    $boundary =md5(date('r', time())); 

    $headers = "From: [email protected]\r\nReply-To: [email protected]"; 
    $headers .= "MIME-Version: 1.0"; 

    $message="This is a multi-part message in MIME format. --_1_$boundary Content-Type: multipart/alternative; boundary=\"_2_$boundary\" --_2_$boundary Content-Type: text/plain; charset=\"iso-8859-1\" Content-Transfer-Encoding: 7bit $message --_2_$boundary-- --_1_$boundary Content-Type: application/vnd.ms-excel; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --_1_$boundary--"; 
    mail($to, $subject, $message, $headers); 
+0

你能告诉我们你使用的代码吗?另外,你的意思是“收到时的附件是0 kb”?这并不完全清楚。 – Piskvor 2011-01-25 13:39:31

回答

0

要做的第一件事就是输出$ message,看它是否确实生成了一个有效的MIME格式的电子邮件。我们不能肯定,只是看你的代码..但有一两件事:

$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
$headers.= "MIME-Version: 1.0"; 

应该是:

$headers = "From: [email protected]\r\nReply-To: [email protected]\r\n"; 
$headers.= "MIME-Version: 1.0"; 

那么你忘了把所有的换行符在您的消息:

$body ="This is a multi-part message in MIME format. (preamble)\r\n"; 
$body.="--_1_$boundary\r\n"; 
$body.="Content-Type: multipart/alternative; boundary=\"_2_$boundary\"\r\n"; 
$body.="--_2_$boundary\r\n"; 
$body.="Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; 
$body.="Content-Transfer-Encoding: 7bit\r\n"; 
$body.="\r\n"; 
$body.="$message\r\n"; 
$body.="--_2_$boundary--\r\n"; 
$body.="--_1_$boundary\r\n"; 
$body.="Content-Type: application/vnd.ms-excel; name=\"$filename\"\r\n"; 
$body.="Content-Transfer-Encoding: base64\r\n"; 
$body.="Content-Disposition: attachment\r\n"; 
$body.="\r\n"; 
$body.="$attachment\r\n"; 
$body.="--_1_$boundary--"; 

mail($to, $subject, $body, $headers); 

我不确定我是否错过了任何东西,但是您可以从此开始。最后,您可以使用我制作的课程omime,它可以简化制作MIME电子邮件。

相关问题