title: PHP AJAX email file attachment
body: <p>I am not able to attach file in PHP email. The following works fine when the file is not attached.</p>
  
  <p>On attaching file, error is thrown <code>Error! Please try again later.'</code></p>
  
  <p>Error log <code>PHP Warning:  mail(): Multiple or malformed newlines found in additional_header in /home/..../mail.php on line 177</code></p>
  
  <pre><code>if (!isset($_SERVER['X-Requested-With']) &amp;&amp; !isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
      die();
  }
  
  $tmp = date('r');
  $message = "&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;body&gt;";
  $message .= "&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;{$val['txtName']}&lt;/p&gt;";
  $message .= "&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;{$val['txtEmail']}&lt;/p&gt;";
  $message .= "&lt;p&gt;&lt;strong&gt; &lt;/strong&gt;{$val['txtSubject']}&lt;/p&gt;";
  $message .= "&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;{$val['txtPhone']}&lt;/p&gt;";
  $message .= "&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;{$val['txtText']}&lt;/p&gt;&lt;br&gt;";
  $message .= "&lt;/body&gt;&lt;/html&gt;";
  
          if (isset($_FILES)) {    
  
          $filename = $_FILES["txtFile"]["name"];
          $uid = md5(uniqid(time()));
  
          $headers = "From: " . $val['txtName'] . " &lt;" . $val['txtEmail'] . "&gt;\r\n";
          $headers .= "Reply-To: " . $val['txtName'] . " &lt;" . $val['txtEmail'] . "&gt;\r\n";
          $headers .= "MIME-Version: 1.0\r\n";
          $headers .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n";
          $headers .= "This is a multi-part message in MIME format.\r\n";
          $headers .= "--" . $uid . "\r\n";
  
          $headers .= "Content-type:text/html; charset=utf-8\n";
          $headers .= "Content-Transfer-Encoding: 7bit\r\n";
          $headers .= $message . "\r\n";
          $headers .= "--" . $uid . "\r\n";
  
          $content = chunk_split(base64_encode(file_get_contents($_FILES["txtFile"]["tmp_name"])));
          $headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n";
          $headers .= "Content-Transfer-Encoding: base64\r\n";
          $headers .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n";
          $headers .= $content . "\r\n";
          $headers .= "--" . $uid . "--";
  
      } else {
  
          $headers = "From: {$val['txtName']} &lt;" . $val['txtEmail'] . "&gt;\r\n";
          $headers .= "Reply-To: {$val['txtName']} &lt;" . $val['txtEmail'] . "&gt;\r\n";
          $headers .= "MIME-Version: 1.0\r\n";
          $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  
      }
  
      if (mail('info@mail.com', $val['txtSubject'], $message, $headers)) {
          die(json_encode(array(
              'error' =&gt; '0',
              'title' =&gt; 'Success! Your message has been sent.'
          )));
      } else {
      die(json_encode(array(
          'error' =&gt; '1',
          'title' =&gt; 'Error! Please try again later.'
      )));
  }
  </code></pre>
  
  <p>Tried another header set (but same problem):</p>
  
  <pre><code>$file = fopen($_FILES["txtFile"]["tmp_name"], "r");
  if ($file == false) {
  
      die(json_encode(array(
          'code' =&gt; 'error',
          'title' =&gt; 'Error! Could not read the file.'
      )));
  
  }
  
  $size = $_FILES["txtFile"]["size"];
  $content = fread($file, $size);
  
  
  $encoded_content = chunk_split(base64_encode($content));
  
  
  $num = md5(time());
  
  
  $header = "From: " . $val['txtName'] . " &lt;" . $val['txtEmail'] . "&gt;\r\n";
  $header .= "Reply-To: " . $val['txtName'] . " &lt;" . $val['txtEmail'] . "&gt;\r\n";
  $header .= "MIME-Version: 1.0\r\n";
  $header .= "Content-Type: multipart/mixed; ";
  $header .= "boundary=$num\r\n";
  $header .= "--$num\r\n";
  
  
  $header .= "Content-Type: text/plain\r\n";
  $header .= "Content-Transfer-Encoding:8bit\r\n\n";
  $header .= "$message\r\n";
  $header .= "--$num\r\n";
  
  
  
    $header .= "Content-Type:  multipart/mixed; ";
    $header .= "name=\"$filename\"\r\n";
    $header .= "Content-Transfer-Encoding:base64\r\n";
    $header .= "Content-Disposition:attachment; ";
    $header .= "filename=\"$filename\"\r\n\n";
    $header .= "$encoded_content\r\n";
    $header .= "--$num--";
  </code></pre>
  
  <p>Please let me know where I am making mistake.</p>