title: Sending attachment (.docx) with PHP mail();
body: <p>while sending a mail using PHP <code>mail()</code> without attachment working fine.</p>
  
  <p>But while sending with attachment <code>sending Failed</code>
  please help me.
  Whats wrong in my code?</p>
  
  <p>Thanks in advance.
  My HTML code is shown below</p>
  
  <p>index.html</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Mail&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form method="POST" action="mail.php" enctype="multipart/form-data"&gt;
              &lt;input type="text" name="fromName"&gt;
              &lt;input type="email" name="fromEmail"&gt;
              &lt;input type="file" name="fileAttach"&gt;
              &lt;input type="submit" name="submit"&gt;
  
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The following is my Mail code</p>
  
  <p>mail.php</p>
  
  <pre><code>&lt;?php
  if (isset($_POST['submit'])) {
     /* $mailto = $_POST["mailTo"];*/
     $mailto = "to@gmail.com";
      $from_mail = $_POST["fromEmail"];
      $replyto = $_POST["fromEmail"];
      $from_name = $_POST["fromName"];
      /*$message = $_POST["message"];*/
      $message="This is message part";
      /*$subject = $_POST["subject"];*/
      $subject ="this is subject part";
  
      $filename = $_FILES["fileAttach"]["name"];
      $content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
  
      $uid = md5(uniqid(time()));
      $name = basename($file);
      $header = "From: " . $from_name . " &lt;" . $from_mail . "&gt;\r\n";
      $header .= "Reply-To: " . $replyto . "\r\n";
  
      $header .= "MIME-Version: 1.0\r\n";
      $header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
      $header .= "This is a multi-part message in MIME format.\r\n";
      $header .= "--" . $uid . "\r\n";
  
  // You add html "Content-type: text/html; charset=utf-8\n" or for Text "Content-type:text/plain; charset=iso-8859-1\r\n" by I.khan
      $header .= "Content-type:text/html; charset=utf-8\n";
      $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  
  // User Message you can add HTML if You Selected HTML content
      $header .= "&lt;div style='color: red'&gt;" . $message . "&lt;/div&gt;\r\n\r\n";
  
      $header .= "--" . $uid . "\r\n";
      $header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n"; // use different content types here
      $header .= "Content-Transfer-Encoding: base64\r\n";
      $header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n"; // For Attachment
      $header .= $content . "\r\n\r\n";
      $header .= "--" . $uid . "--";
      if (mail($mailto, $subject, $message, $header)) {
          echo "&lt;script&gt;alert('Success');&lt;/script&gt;"; // or use booleans here
      } else {
          //echo mysqli_error();
          echo "&lt;script&gt;alert('Failed');&lt;/script&gt;";
  
      }
  }
  </code></pre>
  
  <p>Is there anything to add or change in the code?</p>