PHP 电子邮件标头?哑剧错误或其他东西..帮助理解


PHP Email headers? Mime mistakes or something else...help to understand?

我是php的大三学生。现在我尝试通过 php mail() 发送一些附件,但是当我要阅读邮件时,它仅以文本形式显示(见下文)。附件内容也是空白的。我使用在互联网上找到的邮件表格。我认为错误出在/r/n 或/n 之类的事情上。对不起床英语。如果被问到,我可以解释更多,等待你的帮助!很抱歉代码发布太多,但我认为有必要理解......

表格

<form action="mailme.php"  method="post" enctype="multipart/form-data">
-+-+-  other inputs and select -+-+-
<input name="upload[]" type="file">
<input name="upload[]" type="file">
...other file attachments like that above
</form>

.PHP

Retrive variables and then 
$headers ="From: ".$SenderName." <".$from.">'n";
// boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
// headers for attachment 
    $headers .= "'nMIME-Version: 1.0'n" . "Content-Type: multipart/mixed;'n" .   "
                 boundary='"{$mime_boundary}'""; 
// multipart boundary 
    $message = "This is a multi-part message in MIME format.'n'n" . "--{$mime_boundary}'n"
               . "Content-Type: text/html; charset='"iso-8859-1'"'n" . 
                 "Content-Transfer-Encoding: 7bit'n'n" . $message . "'n'n"; 
    $message .= "--{$mime_boundary}'n";
// preparing attachments
    for($x=0;$x<count($files);$x++){
            $file = fopen($files[$x],"rb");
            $data = fread($file,filesize($files[$x]));
            fclose($file);
            $data = chunk_split(base64_encode($data));
            $message .= "Content-Type: {'"application/octet-stream'"};'n" .  
                        " name='"$files[$x]'"'n" . 
            "Content-Disposition: attachment;'n" . " filename='"$files[$x]'"'n" . 
            "Content-Transfer-Encoding: base64'n'n" . $data . "'n'n";
            $message .= "--{$mime_boundary}'n";
    }
    // send
    $success = @mail($to, $subject, $message, $headers); 
    if ($success) { 
            echo "<p>mail sent to $to!</p>"; 
    } else { 
            print_r($files);
echo "<p>mail could not be sent!</p>".$from.'<br>'.$message.'<br>'.$to.'<br>'.$subject.'<br>'.$SenderName; 
    } 

输出

MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_x553132b5cf2c4c84ecdbd3285b8b590dx"
This is a multi-part message in MIME format.
--==Multipart_Boundary_x553132b5cf2c4c84ecdbd3285b8b590dx
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Message <b>with<b> some <a href="html">html</a>!
--==Multipart_Boundary_x9351bff6284cb1fc967281cfede5762cx
Content-Type: {"application/octet-stream"};
name=""
Content-Disposition: attachment;
filename=""
Content-Transfer-Encoding: base64
.....BLANK CONTENT! LIKE NO IMAGE WAS ATTACHED......    

--==Multipart_Boundary_x553132b5cf2c4c84ecdbd3285b8b590dx

使用var_dump($files);来显示它包含的内容。它应该包含每个上传文件的数组,包括名称、类型、大小、tmp_name和错误(如果发生错误)

所以你的循环应该看起来像这样:

for($x=0;$x<count($files);$x++){
        $file = fopen($files[$x]['tmp_name'],"rb");
        //                      ^ path to the uploaded file
        $data = fread($file,filesize($files[$x]['tmp_name']));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {'"application/octet-stream'"};'n" .  
                    " name='"$files[$x]['name']'"'n" . 
        //                              ^ filename
        "Content-Disposition: attachment;'n" . " filename='"$files[$x]['name']'"'n" . 
        "Content-Transfer-Encoding: base64'n'n" . $data . "'n'n";
        $message .= "--{$mime_boundary}'n";
}

您也可以将$files[$x]['type']用于内容类型,但我不相信上传浏览器提供的内容。