Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php电子邮件附件发送一个空文件_Php_Email_Attachment - Fatal编程技术网

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

php电子邮件附件发送一个空文件,php,email,attachment,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 boundar

我正在尝试通过电子邮件发送附件,但是,即使正确的文件保存在服务器中,附加到电子邮件的文件也是空的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;
}
但我不知道会是什么

我知道我可以使用库,但为了学习,我想看看我的代码有什么问题

我们将非常感谢您的帮助


Sonia

感谢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\u name而不是$file,因为$file

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

查看消息源,您是否在那里看到base64编码的数据?我非常喜欢在双引号中嵌入变量,但是你必须注意这样的问题:$mime\u boundary===Multipart\u boundary\u x$semi\u randx;尝试使用大括号或连接运算符。另外,按照@Cheery的建议粘贴您的消息文本。
$message .= "Content-Disposition: attachment;\r\n filename=\"" . $file_name . "\"\r\n\r\n";