title: How to download file with $.ajax and get raw content of attachment email, using PHP
body: <p>i have a database where are stored all email in raw mode.</p>
  
  <p>I extract data with good library <a href="https://github.com/php-mime-mail-parser/php-mime-mail-parser" rel="nofollow noreferrer">https://github.com/php-mime-mail-parser/php-mime-mail-parser</a>, in particular attachments.</p>
  
  <p>Now, from html page i have:</p>
  
  <pre><code> $.ajax({ 
          async: false,
          type: "POST",
          url: "/mail/get/attachment",
          data: {
              'idMail'    : idMail,
              'filename'  : filename
          },
          success: function(data){
  
              cl(data);
  
          },
          error: function(data){       
  
          }
          }).done(function (data) {
  
      });
  </code></pre>
  
  <p>this call my method php:</p>
  
  <pre><code>    $parser = new PhpMimeMailParser\Parser();
      $parser-&gt;setText($mail['rawContent']);
      $attachments = $parser-&gt;getAttachments();
  
      foreach ($attachments as $attachment) {
          if ($attachment-&gt;getFilename() == $filename){
  
              $file = $attachment-&gt;getContent();
              $size = strlen($file);
  
              @ob_start('');
  
  
              header('Content-Description: File Transfer');
              header('Content-Type: application/octet-stream');
              header('Content-Type: application/download');
              header('Content-Description: File Transfer');
              header('Content-Transfer-Encoding', 'binary');
              header('Content-Disposition: attachment; filename="'.basename($filename).'"');
              header('Content-Length: '.$size);
              header('Expires: 0');
              header('Cache-Control: must-revalidate');
              header('Pragma: public');
  
              ob_clean();
              flush();
              readfile($file);
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/GLGKX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GLGKX.png" alt="Request and Response of script"></a></p>
  
  <p>As evidenced by the response, everything seems correct but none response show.</p>
  
  <ul>
  <li><p>The filesize of attachment is correct...i have only string for calculate it. I don't want download on filesystem.</p></li>
  <li><p>If i change the ajax client request from "async: false" into "async : true" the chrome console give me the error "net::ERR_CONTENT_LENGTH_MISMATCH".</p></li>
  </ul>
  
  <p>Where i wrong ?</p>
  
  <p>I don't want to get to the point of having to download the messages on the server every time and then use a simple url to recover it since I already have everything in the database.</p>
  
  <p>What solution can i use?</p>
  
  <p>Thanks!</p>
  
  ## UPDATE
  
  <pre><code>ob_clean();
  echo $file;
  flush();
  </code></pre>
  
  <p>This procedure return  this in console but the download not start.</p>
  
  <p><a href="https://i.stack.imgur.com/q2sWJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q2sWJ.png" alt="enter image description here"></a></p>