title: Add attachment through PHPMailer
body: <p>I have this following piece of code of PHPMailer. The problem is, that the file uploads to the server successfully but the attachment is not sent in the mail. The attachment code seems right to the best of my knowledge. Please review the code and let me know where have I gone wrong.</p>
  
  <p><strong>Form</strong></p>
  
  <pre><code>&lt;form name="contactform" method="post" action="send1.php" enctype="multipart/form-data"&gt;
  &lt;table width="100%" border="0"&gt;
  &lt;tr&gt;
   &lt;td id="ta"&gt;
   &lt;label for="title"&gt;Title *&lt;/label&gt;
   &lt;/td&gt;
   &lt;td id="ta"&gt;
   &lt;select name="title"&gt;
   &lt;option value="0"&gt;Title&lt;/option&gt;
   &lt;option value="1"&gt;Mr.&lt;/option&gt;
   &lt;option value="2"&gt;Ms.&lt;/option&gt;
   &lt;option value="3"&gt;Mrs.&lt;/option&gt;
   &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td id="ta"&gt;
    &lt;label for="first_name"&gt;First Name *&lt;/label&gt;
   &lt;/td&gt;
   &lt;td id="ta"&gt;
    &lt;input  type="text" name="first_name" maxlength="50" size="30" required="required"&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td id="ta"&gt;
    &lt;label for="last_name"&gt;Last Name *&lt;/label&gt;
   &lt;/td&gt;
   &lt;td  id="ta"&gt;
    &lt;input  type="text" name="last_name" maxlength="50" size="30" required="required"&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td id="ta"&gt;
    &lt;label for="email"&gt;Email Address *&lt;/label&gt;
   &lt;/td&gt;
   &lt;td  id="ta"&gt;
    &lt;input  type="text" name="email" maxlength="80" size="30" required="required"&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td id="ta"&gt;
    &lt;label for="telephone"&gt;Telephone Number *&lt;/label&gt;
   &lt;/td&gt;
   &lt;td  id="ta"&gt;
    &lt;input  type="text" name="telephone" maxlength="30" size="30" required="required"&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td id="ta"&gt;
    &lt;label for="comments"&gt;Details&lt;/label&gt;
   &lt;/td&gt;
   &lt;td  id="ta"&gt;
    &lt;textarea  name="comments" maxlength="100000" cols="25" rows="6"&gt;&lt;/textarea&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td id="ta"&gt;
      &lt;label for="file"&gt;Or upload a file (only word, excel or pdf)&lt;/label&gt;
  &lt;/td&gt;
  &lt;td  id="ta"&gt;
  &lt;input type="file" name="file"&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td colspan="2" style="text-align:center" id="ta"&gt;
    &lt;input type="submit" value="Submit"&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>send1.php</strong></p>
  
  <pre><code>&lt;?php
  
  require('PHPMailer/class.phpmailer.php');
  
  if(isset($_POST['email'])) {
  
      // EDIT THE 2 LINES BELOW AS REQUIRED
      //$email_to = "hidden";
      //$email_subject = "Request for Portfolio check up from ".$first_name." ".$last_name;
  
      $title = array('Title', 'Mr.', 'Ms.', 'Mrs.');
      $selected_key = $_POST['title'];
      $selected_val = $title[$_POST['title']]; 
  
      $first_name = $_POST['first_name']; // required
      $last_name = $_POST['last_name']; // required
      $email_from = $_POST['email']; // required
      $telephone = $_POST['telephone']; // not required
      $comments = $_POST['comments']; // required
  
    if(($selected_key==0))
      echo "&lt;script&gt; alert('Please enter your title')&lt;/script&gt;";
      function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
      }
       $email_message = "";
      $email_message .="Title: ".$selected_val."\n";
      $email_message .= "First Name: ".clean_string($first_name)."\n";
      $email_message .= "Last Name: ".clean_string($last_name)."\n";
      $email_message .= "Email: ".clean_string($email_from)."\n";
      $email_message .= "Telephone: ".clean_string($telephone)."\n";
      $email_message .= "Comments: ".clean_string($comments)."\n";
  
      $allowedExts = array("doc", "docx", "xls", "xlsx", "pdf");
  $temp = explode(".", $_FILES["file"]["name"]);
  $extension = end($temp);
  if ((($_FILES["file"]["type"] == "application/pdf")
  || ($_FILES["file"]["type"] == "application/msword")
  || ($_FILES["file"]["type"] == "application/excel")
  || ($_FILES["file"]["type"] == "application/vnd.ms-excel")
  || ($_FILES["file"]["type"] == "application/x-excel")
  || ($_FILES["file"]["type"] == "application/x-msexcel")
  || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
  || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
  
  &amp;&amp; in_array($extension, $allowedExts))
    {
    if ($_FILES["file"]["error"] &gt; 0)
      {
      echo "&lt;script&gt;alert('Error: " . $_FILES["file"]["error"] ."')&lt;/script&gt;";
      }
    else
      {
          $d='upload/';
          $de=$d . basename($_FILES['file']['name']);
      move_uploaded_file($_FILES["file"]["tmp_name"], $de);
  $fileName = $_FILES['file']['name'];
      $filePath = $_FILES['file']['tmp_name'];
       //add only if the file is an upload
       }
    }
  else
    {
    echo "&lt;script&gt;alert('Invalid file')&lt;/script&gt;";
    }
  
  // create email headers
  $headers = 'From: '.$email_from."\r\n".
  'Reply-To: '.$email_from."\r\n" .
  'X-Mailer: PHP/' . phpversion();
  //Create a new PHPMailer instance
  $mail = new PHPMailer();
  //Tell PHPMailer to use SMTP
  $mail-&gt;IsSMTP();
  //Enable SMTP debugging
  // 0 = off (for production use)
  // 1 = client messages
  // 2 = client and server messages
  $mail-&gt;SMTPDebug  = 0;
  //Ask for HTML-friendly debug output
  $mail-&gt;Debugoutput = 'html';
  //Set the hostname of the mail server
  $mail-&gt;Host       = "hidden";
  //Set the SMTP port number - likely to be 25, 465 or 587
  $mail-&gt;Port       = 25;
  //Whether to use SMTP authentication
  $mail-&gt;SMTPAuth   = true;
  //Username to use for SMTP authentication
  $mail-&gt;Username   = "hidden";
  //Password to use for SMTP authentication
  $mail-&gt;Password   = "hidden";
  //Set who the message is to be sent from
  $mail-&gt;SetFrom($email_from, $first_name.' '.$last_name);
  //Set an alternative reply-to address
  //$mail-&gt;AddReplyTo('replyto@example.com','First Last');
  //Set who the message is to be sent to
  $mail-&gt;AddAddress('hidden', 'hidden');
  //Set the subject line
  $mail-&gt;Subject = 'Request for Profile Check up';
  //Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
  $mail-&gt;MsgHTML($email_message);
  //Replace the plain text body with one created manually
  $mail-&gt;AltBody = 'This is a plain-text message body';
  //Attach an image file
  //$mail-&gt;AddAttachment($file);
  $mail-&gt;AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
  //Send the message, check for errors
  if(!$mail-&gt;Send()) {
    echo "&lt;script&gt;alert('Mailer Error: " . $mail-&gt;ErrorInfo."')&lt;/script&gt;";
  } else {
    echo "&lt;script&gt;alert('Your request has been submitted. We will contact you soon.')&lt;/script&gt;";
    Header('Location: main.php');
  }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>EDIT</strong> The mail sends successfully with all the details too. Just that the attachment won't send.</p>
  
  <p><strong>EDIT 2: SOLVED</strong> Changed <code>$mail-&gt;MsgHTML</code> to <code>$mail-&gt;Body</code> and it worked!</p>