PHP电子邮件标题? Mime错误或其他什么......有助于理解?

时间:2014-04-03 13:28:13

标签: php html html5 email mime-types

我是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

1 个答案:

答案 0 :(得分:0)

使用var_dump($files);显示其中包含的内容。每个上传的文件should contain an array,包含名称,类型,大小,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']作为内容类型,但我不相信上传浏览器提供的内容。