title: Allow user to attach multiple files but optional
body: <p>I want to allow the user to attach multiple files in my form and those file come to me as an attachment in an email. But I want to keep this as optional i.e. if the user attaches the file, the files come as an attachment and if the user does not attach files, the rest of the form comes in the email. </p>
  
  <p>Can somebody help me with this? </p>
  
  <p>I am new to php so following is my php file.</p>
  
  <pre><code>&lt;?php
  /**
  * Simple example script using PHPMailer with exceptions enabled
  * @package phpmailer
  * @version $Id$
  */
  
  require 'class.phpmailer.php';
  
  try {
  $mail = new PHPMailer(true); //New instance, with exceptions enabled
  
  $to = "saxena@designmaniac.in";
  $mail-&gt;AddAddress($to);
  $mail-&gt;From = $_POST['email'];
  $mail-&gt;FromName = $_POST['name_f'];
  $mail-&gt;Subject = "Admission Form";
  
  $body = "&lt;table&gt;
  &lt;tr&gt;
  &lt;th colspan='2'&gt;Admission Form&lt;/th&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Course :&lt;/td&gt;
  &lt;td&gt;".$_POST['course']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Payment Mode :&lt;/td&gt;
  &lt;td&gt;".$_POST['p_mode']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Payment options :&lt;/td&gt;
  &lt;td&gt;".$_POST['p_option']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;First Name :&lt;/td&gt;
  &lt;td&gt;".$_POST['name_f']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Middle Name : &lt;/td&gt;
  &lt;td&gt;".$_POST['name_m']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Last Name : &lt;/td&gt;
  &lt;td&gt;".$_POST['name_l']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;E-mail : &lt;/td&gt;
  &lt;td&gt;".$_POST['email']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Date of Birth : &lt;/td&gt;
  &lt;td&gt;".$_POST['date']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Gender : &lt;/td&gt;
  &lt;td&gt;".$_POST['gender']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Nationality : &lt;/td&gt;
  &lt;td&gt;".$_POST['nationality']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Address : &lt;/td&gt;
  &lt;td&gt;".$_POST['address_line1']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;City : &lt;/td&gt;
  &lt;td&gt;".$_POST['city']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Zip Code : &lt;/td&gt;
  &lt;td&gt;".$_POST['zip']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;State : &lt;/td&gt;
  &lt;td&gt;".$_POST['state']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Country : &lt;/td&gt;
  &lt;td&gt;".$_POST['country']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Mobile Number : &lt;/td&gt;
  &lt;td&gt;".$_POST['mobile_number']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Father/Mother First Name : &lt;/td&gt;
  &lt;td&gt;".$_POST['fname_f']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Father/Mother Middle Name : &lt;/td&gt;
  &lt;td&gt;".$_POST['fname_m']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Father/Mother Last Name : &lt;/td&gt;
  &lt;td&gt;".$_POST['fname_l']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Father/Mother Mobile No : &lt;/td&gt;
  &lt;td&gt;".$_POST['mobile_number1']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Qualification : &lt;/td&gt;
  &lt;td&gt;".$_POST['qualification']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;University : &lt;/td&gt;
  &lt;td&gt;".$_POST['university']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Year : &lt;/td&gt;
  &lt;td&gt;".$_POST['year']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Qualification : &lt;/td&gt;
  &lt;td&gt;".$_POST['qualification_2']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;University : &lt;/td&gt;
  &lt;td&gt;".$_POST['university_2']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Year : &lt;/td&gt;
  &lt;td&gt;".$_POST['year_2']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Qualification : &lt;/td&gt;
  &lt;td&gt;".$_POST['qualification_3']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;University : &lt;/td&gt;
  &lt;td&gt;".$_POST['university_3']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Year : &lt;/td&gt;
  &lt;td&gt;".$_POST['year_3']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Discipline Information (School) : &lt;/td&gt;
  &lt;td&gt;".$_POST['discipline']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Discipline Information (Violation) : &lt;/td&gt;
  &lt;td&gt;".$_POST['violation']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;12th Mark List : &lt;/td&gt;
  &lt;td&gt;".$_POST['attachment1']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;12th Passing Certificate : &lt;/td&gt;
  &lt;td&gt;".$_POST['attachment2']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;10th Mark List : &lt;/td&gt;
  &lt;td&gt;".$_POST['attachment3']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;10th Passing Certificate : &lt;/td&gt;
  &lt;td&gt;".$_POST['attachment4']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Address Proof : &lt;/td&gt;
  &lt;td&gt;".$_POST['attachment8']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td style='font-weight:bold'&gt;Photographs : &lt;/td&gt;
  &lt;td&gt;".$_POST['attachment9']."&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;table&gt;";
  $body = preg_replace('/\\\\/','', $body); //Strip backslashes
  $mail-&gt;MsgHTML($body);
  
  $mail-&gt;IsSMTP(); // tell the class to use SMTP
  $mail-&gt;SMTPAuth = true; // enable SMTP authentication
  $mail-&gt;Port = 25; // set the SMTP server port
  //$mail-&gt;Host = "saxena@designmaniac.in"; // SMTP server
  //$mail-&gt;Username = "name@domain.com"; // SMTP server username
  //$mail-&gt;Password = "password"; // SMTP server password
  
  $mail-&gt;IsSendmail(); // tell the class to use Sendmail
  $mail-&gt;AddReplyTo("saxena@designmaniac.in");
  $mail-&gt;AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  $mail-&gt;WordWrap = 80; // set word wrap
  
  // Check file size
  if ($_FILES["fileToUpload"]["size"] &gt; 500000) {
      echo "Sorry, your file is too large.";
      $uploadOk = 0;
  }
  // Allow certain file formats
  if($imageFileType != "jpg" &amp;&amp; $imageFileType != "png" &amp;&amp; $imageFileType != "jpeg"
  &amp;&amp; $imageFileType != "gif" &amp;&amp; $imageFileType != "pdf" &amp;&amp; $imageFileType != "docx"
  &amp;&amp; $imageFileType != "doc" ) {
      echo "Sorry, only JPG, JPEG, PNG, GIF, pdf &amp; Word files are allowed.";
      $uploadOk = 0;
  }
  
  if(!empty($_FILES['fileToUpload']['tmp_name']) &amp;&amp; !empty($_FILES['fileToUpload']['name'])) {
  $mail-&gt;AddAttachment($_FILES['fileToUpload']['tmp_name'],
  $_FILES['fileToUpload']['name']);
  }
  $mail-&gt;IsHTML(true); // send as HTML
  $mail-&gt;Send();
  echo 'Thank You. Your form has been submitted';
  } catch (phpmailerException $e) {
  echo $e-&gt;errorMessage();
  }
  
  ?&gt;
  </code></pre>