【问题标题】:Issue, all of a sudden sending .docx as mail attachment问题,突然将 .docx 作为邮件附件发送
【发布时间】:2017-01-07 08:11:38
【问题描述】:

我有一个大约 3 年前编写的自定义 PHP 表单。它旨在通过电子邮件发送所有附件,包括 docx 文件,并且工作起来就像一个魅力。刚从今年开始,客户注意到用户抱怨发送表单时出错,该表单允许他们上传简历。故障排除发现它只发生在某些 .docx 文件中。我们有大量的 .docx 文件可以很好地上传和通过电子邮件发送。所以它是: 1. .docx 编码的变化或我不熟悉的东西 2. 用户一定是以某种方式破坏了他们的 .docx 文件。

我搜索了任何更改 .docx 文件编码方式的证据,但一无所获。我的代码似乎是上传多个文件的最佳实践,甚至是 .docx 文件。为了确保我发布了我的 send-mail.php 文件并询问是否有人看到允许所有列出的文件格式的内容,以及一些 .docx 发送 FINE,但一些 .docx 文件阻塞了脚本并在“如果(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 = "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

【问题讨论】:

  • 使用PHPMailer 或等效的库来完成这项繁重的工作。如果您坚持自己滚动,那么在损坏的文件上$file_type 的值是多少?

标签: php email email-attachments docx


【解决方案1】:

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

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

尝试将其更改为“通用”

应用程序/八位字节流

【讨论】:

    【解决方案2】:

    您的代码似乎很完美,但建议您执行某些操作以确保您的脚本正确运行。

    请更改阅读文件的内容,因为它不会改变任何东西,只会改变内存使用情况:

    $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 电子邮件,这一点很重要。

    如果上述方法不能解决问题,请让我知道以提出其他建议。

    【讨论】:

    • 我正在考虑进行您的更改,但我不明白为什么所有 Word 文档都可以,直到 2016 版的 MS Word 出现并且他们只是在打嗝?似乎 2016 年的格式发生了变化……不是吗?那么调查并查看代码需要如何更新以允许新格式不是更好吗?有没有人上传代码适用于 2016 Word Doc?我想我会尝试 PHPMailer 代码,因为它们似乎可以很好地使用它。
    • 就像另一个注意事项一样,它可以包含在上述简历中,因为大多数电子邮件服务器的最大附件大小约为 5 MB,因此如果文件大于接收服务器可能会拒绝它
    猜你喜欢
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2021-11-21
    • 2015-03-18
    相关资源
    最近更新 更多