php电子邮件附件发送一个空文件

时间:2014-11-17 23:10:18

标签: php email attachment

我尝试通过电子邮件发送附件,但即使正确的文件保存在服务器中,附在电子邮件中的文件也是空的(0 kb)。

我使用gmail发送电子邮件。

以下是我的代码的相关部分:

if (empty($error)) { 
    //boundary
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x$semi_randx"; 
    //tell the headers about the boundary
    $header .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"$mime_boundary\"";   
    //define the first part of the email, which is the text part
    $message = "\r\n" . "--$mime_boundary\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\r\n" ; 
        //build the body of the 1st part of the email   
        $content_body = "
            Email del formulario de contacto en  ".home_url().": <br />

            //whatever

        ";
        $message .= $content_body . "\r\n";
        $message .= "--$mime_boundary\n";
        //define the second part of the email, which is the atachments  
            //if a file has been uploaded
        if (!empty($_FILES['cv']['name'])){

             // Open the file for a binary read
             $file = fopen($temp_name,'rb');
             // Read the file content into a variable
             $data = fread($file,filesize($temp_name));
             // close the file
             fclose($file);
             // Now we need to encode it and split it into acceptable length lines
             $data = chunk_split(base64_encode($data));

            // Actually build the second part of the email          



 $message .= "Content-Type: \"application/octet-stream\";\r\n name=\"" . $file_name . "\"\n"; 
            $message .= "Content-Transfer-Encoding: base64\n"; 
            $message .= "Content-Disposition: attachment;\r\n filename=\"" . $file . "\"\r\n\r\n"; 
            $message .= $data; //The base64 encoded message 
            $message .= "\n"; 
            $message .= "--$mime_boundary--\n";

         }


        //send th email
        mail( $receive_email, "Email del formulario de contacto en web", $message, $header);
        $msg  = $succesful_text;
}

我的猜测是我在这里做错了什么:

 $message .= "Content-Type: \"application/octet-stream\";\r\n name=\"" . $file_name . "\"\n"; 
        $message .= "Content-Transfer-Encoding: base64\n"; 
        $message .= "Content-Disposition: attachment;\r\n filename=\"" . $file . "\"\r\n\r\n"; 
        $message .= $data; //The base64 encoded message 
        $message .= "\n"; 
        $message .= "--$mime_boundary--\n";

但我不知道它是什么。

我知道我可以使用一个库,但我想知道我的代码只是为了学习而出了什么问题。

任何帮助都将受到高度赞赏。

索尼亚

1 个答案:

答案 0 :(得分:0)

感谢Cherry和miken32的回复。 最后我明白了。

这就是发生的事情,以防将来可以帮助其他人。

由于短信中没有base64编码数据,我认为问题可能不在$ message文本中,而是在文件的实际处理中。

// Open the file for a binary read
         $file = fopen($temp_name,'rb');
         // Read the file content into a variable
         $data = fread($file,filesize($temp_name));
         // close the file
         fclose($file);
         // Now we need to encode it and split it into acceptable length lines
         $data = chunk_split(base64_encode($data));

我意识到临时文件没有被打开。我无法理解为什么,但我通过直接处理uploads文件夹中的文件解决了这个问题。 这是固定代码:

// Open the file for a binary read
             $file = fopen($server_file,'rb');
             // Read the file content into a variable
             $flsz=filesize($server_file);              
             $data = fread($file,$flsz);
             // close the file
             fclose($file);
             // Now we need to encode it and split it into acceptable length lines
             $data = chunk_split(base64_encode($data));

我还必须修改$ message文本中的文件名,使用$ file_name而不是$ file,因为$ file。

$message .= "Content-Disposition: attachment;\r\n filename=\"" . $file_name . "\"\r\n\r\n";