简体   繁体   English

问题,突然发送.docx作为邮件附件

[英]Issue, all of a sudden sending .docx as mail attachment

I have a custom PHP form that was coded about 3 yeas ago. 我有一个自定义的PHP表单,编码约3年前。 It was built to email all atttachments including docx files and worked like a charm. 它的目的是通过电子邮件发送包括docx文件在内的所有附件,并像魅力一样工作。 Just starting this year, client is noticing that users are complaining of errors sending form which allows them to upload their resume. 刚刚从今年开始,客户注意到用户抱怨发送表单时出错,允许他们上传简历。 Troubleshooting found that it only happens with SOME .docx files. 故障排除发现它只发生在SOME .docx文件中。 We have a ton of .docx files that were uploaded and emailed fine. 我们有大量的.docx文件已上传并通过电子邮件发送。 So it is either: 1. a change in the .docx encoding or something im not familiar with 2. Users must be corrupting their .docx files somehow. 所以它是:1。.docx编码的变化或者我不熟悉的东西2.用户必须以某种方式破坏他们的.docx文件。

I searched for any evidence of the way to code .docx files changed and found nothing. 我搜索了代码.docx文件改变的方式的任何证据,什么也没找到。 My code appears to be the best practice for uploading multiple files, even .docx files. 我的代码似乎是上传多个文件的最佳实践,甚至是.docx文件。 To make sure I am posting my send-mail.php file and asking if anyone sees something that would allow all listed file formats, and some .docx to send FINE, but some .docx files are choking the script and failing at the "If (OK) {" line, meaning error sending mail. 为了确保我发布我的send-mail.php文件,并询问是否有人看到允许所有列出的文件格式的东西,以及一些.docx发送​​FINE,但是一些.docx文件正在阻塞脚本并在“如果”失败(好){“行,表示发送邮件时出错。 Thanks ahead of time for any help. 提前感谢您的帮助。

UPDATE: Seems like it is not working on docs saved in "Word 2016" format. 更新:似乎它不能处理以“Word 2016”格式保存的文档。 So what would I have to do to my code below to make it work,also, with Word 2016 files? 那么,对于我的代码,我还需要做些什么才能使它与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 = "xxxx@gmail.com";
  $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

Hard to say without any test file/case, but my guess is wrong Content-Type, as you rely only on $file['type']. 很难说没有任何测试文件/案例,但我的猜测是错误的Content-Type,因为你只依赖$ file ['type']。

The hard-coded lists in the browsers are pretty limited. 浏览器中的硬编码列表非常有限。 Often, the MIME type sent by the browser will be the one reported by the OS. 通常,浏览器发送的MIME类型将是操作系统报告的类型。 And this is exactly why, as stated in the question, the MIME type reported by the browser is unreliable How is mime type of an uploaded file determined by browser? 这就是为什么,如问题中所述,浏览器报告的MIME类型不可靠如何通过浏览器确定上传文件的mime类型?

Try changing it to "universal" 尝试将其更改为“通用”

application/octet-stream 应用/八位字节流

Your code seems to be perfect but would recommend to do certain things to ensure that your script is running correctly. 您的代码似乎很完美,但建议您执行某些操作以确保脚本正常运行。

Kindly change the reading file conents as it will not change any thing but change the memory usage only as from : 请更改读取文件的内容,因为它不会改变任何内容,只会改变内存使用情况,因为:

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

to

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

so it will decrease the memory usage and handling files 所以它会减少内存使用和处理文件

Second Change from: 第二个变化:

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

to

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

and

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

Third thing you have to confirm the setting of your email sending attachment size. 第三件事你必须确认你的电子邮件发送附件大小的设置。 As it is important that some systems don't allow sending PHP email more than 100 KB or 500 KB. 因为一些系统不允许发送超过100 KB或500 KB的PHP电子邮件非常重要。

If above thing doesn't resolve the issue then kindly let me know to suggest other things. 如果上面的事情没有解决问题,那么请让我知道建议其他事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM