簡體   English   中英

問題,突然發送.docx作為郵件附件

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

我有一個自定義的PHP表單,編碼約3年前。 它的目的是通過電子郵件發送包括docx文件在內的所有附件,並像魅力一樣工作。 剛剛從今年開始,客戶注意到用戶抱怨發送表單時出錯,允許他們上傳簡歷。 故障排除發現它只發生在SOME .docx文件中。 我們有大量的.docx文件已上傳並通過電子郵件發送。 所以它是:1。.docx編碼的變化或者我不熟悉的東西2.用戶必須以某種方式破壞他們的.docx文件。

我搜索了代碼.docx文件改變的方式的任何證據,什么也沒找到。 我的代碼似乎是上傳多個文件的最佳實踐,甚至是.docx文件。 為了確保我發布我的send-mail.php文件,並詢問是否有人看到允許所有列出的文件格式的東西,以及一些.docx發送​​FINE,但是一些.docx文件正在阻塞腳本並在“如果”失敗(好){“行,表示發送郵件時出錯。 提前感謝您的幫助。

更新:似乎它不能處理以“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 = "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

很難說沒有任何測試文件/案例,但我的猜測是錯誤的Content-Type,因為你只依賴$ file ['type']。

瀏覽器中的硬編碼列表非常有限。 通常,瀏覽器發送的MIME類型將是操作系統報告的類型。 這就是為什么,如問題中所述,瀏覽器報告的MIME類型不可靠如何通過瀏覽器確定上傳文件的mime類型?

嘗試將其更改為“通用”

應用/八位字節流

您的代碼似乎很完美,但建議您執行某些操作以確保腳本正常運行。

請更改讀取文件的內容,因為它不會改變任何內容,只會改變內存使用情況,因為:

$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')));

所以它會減少內存使用和處理文件

第二個變化:

$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電子郵件非常重要。

如果上面的事情沒有解決問題,那么請讓我知道建議其他事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM