2016-08-30 53 views
4

我有一个约3年前编码的自定义PHP表单。它的目的是通过电子邮件发送所有附件,包括docx文件,并像魅力一样工作。从今年开始,客户注意到用户抱怨发送错误的形式允许他们上传简历。疑难解答发现它只发生在SOME .docx文件中。我们有很多.docx文件上传和通过电子邮件罚款。所以它可能是: 1.docx编码的改变或者我不熟悉的东西 2.用户必须以某种方式破坏他们的.docx文件。问题,突然发送.docx作为邮件附件

我搜索了代码.docx文件更改方式的任何证据。我的代码似乎是上传多个文件,甚至是.docx文件的最佳做法。为了确保我发布了我的send-mail.php文件,并询问是否有人看到允许所有列出的文件格式的内容,并且某些.docx发送​​了FINE,但是一些.docx文件阻塞了该脚本,并在“If (OK){“行,表示发送邮件的错误。提前感谢您的帮助。

更新:似乎它不适用于以“Word 2016”格式保存的文档。那么,为了使其与Word 2016文件一起工作,我还需要做些什么才能使其工作?

if(isset($_FILES)) { 

    // define allowed extensions 
    $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt",""); 
    $files = array(); 

    // loop through all the files 
    foreach($_FILES as $name=>$file) { 

    // define some variables 
    $file_name = $file['name']; 
    $temp_name = $file['tmp_name']; 
    $file_type = $file['type']; 

    // check if this file type is allowed 
    $path_parts = pathinfo($file_name); 
    $ext = $path_parts['extension']; 
    if(!in_array($ext,$allowedExtensions)) { 
     die("Your file type is not allowed. Must be only pdf, txt, doc, docx, gif , jpeg, jpg, png, or rtf. Use backspace to go back."); 
    } 

    // move this file to the server YOU HAVE TO DO THIS 
    $server_file = "/home/content/25/9264325/html/wp-content/uploads/files/$path_parts[basename]"; 
    move_uploaded_file($temp_name,$server_file); 

    // add this file to the array of files 
    array_push($files,$server_file); 
    } 

    // define some mail variables 

    $to = "[email protected]"; 
    $from = $email; 
    $subject ="NEW EMPLOYMENT APPLICATION"; 
    $headers = "From: Cxxxxxxs \r\nReply-To: ".$from; 

    // define our boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // tell the header about the boundary 
    $headers .= "\r\nMIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: multipart/mixed;\r\n"; 
    $headers .= " boundary=\"{$mime_boundary}\"\r\n\r\n"; 

    // part 1: define the plain HTML email 
    $message ="\r\n\r\n--{$mime_boundary}\r\n"; 
    $message .="Content-Type: text/html; charset=\"iso-8859-1\"\r\n"; 
    $message .="Content-Transfer-Encoding: base64\r\n\r\n" . $msg . "\r\n\r\n"; 


    // part 2: loop and define mail attachments if thee is a file 


      foreach($files as $file) { 
      $aFile = fopen($file,"rb"); 
      $data = fread($aFile,filesize($file)); 
      fclose($aFile); 
      $data = chunk_split(base64_encode($data)); 
      $message .= "\r\n--{$mime_boundary}\r\n"; 
      $message .= "Content-Type: {$file_type};\r\n"; 
      $message .= " name=\"{$file_name}\"\r\n"; 
      $message .= "Content-Transfer-Encoding: base64\r\n"; 
      $message .= "Content-Disposition: attachment;\r\n"; 
      $message .= "filename=\"{$file_name}\"\r\n"; 
      $message .= $data . "\r\n"; 
      $message .= "--{$mime_boundary}--\r\n"; 
      } 

    // send the email 
    $ok = mail($to, $subject, $message, $headers); 
    if ($ok) { 
    header('Location: http://www.xxxxx.com/thank-you/'); 
      } else { 
       echo "<p>mail could not be sent!</p>"; 
      } 
      die(); 
}// if isset files 
+2

使用['PHPMailer'](https://github.com/PHPMailer/PHPMailer)或相当的库来完成这个繁重的工作。如果你坚持要自己滚动,那么损坏文件上'$ file_type'的值是多少? – bishop

回答

1

您的代码看起来很完美,但会推荐做某些事情以确保您的脚本正常运行。

请改变读取文件conents,因为它不会改变任何事情,但改变了内存使用仅来自:

$aFile = fopen($file,"rb"); 
$data = fread($aFile,filesize($file)); 
fclose($aFile); 
$data = chunk_split(base64_encode($data)); 

$attachments = chunk_split(base64_encode(file_get_contents('filename.ext'))); 

所以它会减少内存使用和处理files

第二次更改自:

$ok = mail($to, $subject, $message, $headers); 

$ok = @mail($to, $subject, $message, $headers); 

if ($ok) { 
}else{ 
    echo $ok; //It will give you the exact error reason here 
} 

第三件事情,你要确认你的电子邮件发送附件大小的设置。由于重要的是某些系统不允许发送超过100 KB或500 KB的PHP电子邮件。

如果上面的东西没有解决问题,那么请让我知道建议其他的事情。

+0

我正在考虑做你的修改,但我不明白为什么所有的Word文档都会好的MS Word的UNTILL 2016版本出来了,他们只是hickupping?似乎2016年的格式是什么改变了...不是?因此,调查并查看代码是如何更新以便允许新格式的话,会不会更好?有没有人上传2016年Word Doc的代码?我想我只会尝试PHPMailer的代码,因为它们似乎很好用。 –

+0

正如另一个注意事项,它可能包含在上述简历中的图像,因为大多数电子邮件服务器的最大附件大小约为5 MB,因此如果该文件大于收件服务器可能拒绝的大小 –

4

很难说没有任何测试文件/案例,但我的猜测是错误的Content-Type,因为您只依赖于$ file ['type']。

浏览器中的硬编码列表相当有限。通常,浏览器发送的MIME类型将是操作系统报告的MIME类型。这就是为什么,在这个问题指出,浏览器上报的MIME类型是不可靠的 How is mime type of an uploaded file determined by browser?

尝试将其更改为“通用”

应用程序/八位字节流