title: HTML form submit how to: File Upload and email
body: <p>I have an HTML form that have several different fields and drop down menus. The form has several different text fields, a couple drop down menus, and a file upload. Once the form is submitted it is sent to me via email. Below is the HTML and the PHP submit. </p>
  
  <p>I can't seem to figure out how to get the file to upload and then be sent to me as an email attachment. I would appreciate any help. Thanks</p>
  
  <p>The html looks like this
  `
  </p>
  
  <pre><code>            &lt;label class="label" for="name"&gt;Name:&lt;/label&gt; &lt;input id="name" class="name" type="text" name="distributorName" /&gt; 
  
              &lt;label class="label" for="distributorId"&gt;Distributor ID:&lt;/label&gt; &lt;input id="name" class="name" type="text" name="distributorId" /&gt; 
  
              &lt;label class="label" for="email"&gt;Distributor Email:&lt;/label&gt; &lt;input id="email" class="name" type="text" name="email" /&gt;
          &lt;h2&gt;Business Information&lt;/h2&gt;
              &lt;label class="label" for="businessName"&gt;Business Name:&lt;/label&gt; 
                  &lt;input id="name" class="name" type="text" name="businessName" /&gt; 
              &lt;label class="label" for="phone"&gt;Business Phone Number:&lt;/label&gt; 
                  &lt;input id="phone" class="name" type="text" name="phone" /&gt; 
              &lt;label class="label" for="street"&gt;Address:&lt;/label&gt; 
                  &lt;input id="street" class="name" type="text" name="street" /&gt; 
              &lt;label class="label" for="city"&gt;City:&lt;/label&gt; &lt;input id="city" class="name" type="text" name="city" /&gt;
  
          &lt;div id="state"&gt;
              &lt;label id="state" class="label" for="state"&gt;State:&lt;/label&gt; 
                  &lt;select class="dropdown" name="state"&gt; 
                  &lt;option value="AL"&gt;AL&lt;/option&gt; 
                  &lt;option value="AK"&gt;AK&lt;/option&gt;
                  &lt;option value="AZ"&gt;AZ&lt;/option&gt; 
                  &lt;option value="AR"&gt;AR&lt;/option&gt;
                  &lt;option value="CA"&gt;CA&lt;/option&gt; 
                  &lt;option value="CO"&gt;CO&lt;/option&gt;
                  &lt;option value="CT"&gt;CT&lt;/option&gt; 
                  &lt;option value="DE"&gt;DE&lt;/option&gt;
                  &lt;option value="FL"&gt;FL&lt;/option&gt; 
                  &lt;option value="GA"&gt;GA&lt;/option&gt;
                  &lt;option value="HI"&gt;HI&lt;/option&gt; 
                  &lt;option value="ID"&gt;ID&lt;/option&gt;
                  &lt;option value="IL"&gt;IL&lt;/option&gt; 
                  &lt;option value="IN"&gt;IN&lt;/option&gt;
                  &lt;option value="IA"&gt;IA&lt;/option&gt; 
                  &lt;option value="KS"&gt;KS&lt;/option&gt;
                  &lt;option value="KY"&gt;KY&lt;/option&gt; 
                  &lt;option value="LA"&gt;LA&lt;/option&gt;
                  &lt;option value="ME"&gt;ME&lt;/option&gt; 
                  &lt;option value="MD"&gt;MD&lt;/option&gt;
                  &lt;option value="MA"&gt;MA&lt;/option&gt; 
                  &lt;option value="MI"&gt;MI&lt;/option&gt;
                  &lt;option value="MN"&gt;MN&lt;/option&gt; 
                  &lt;option value="MS"&gt;MS&lt;/option&gt;
                  &lt;option value="MO"&gt;MO&lt;/option&gt; 
                  &lt;option value="MT"&gt;MT&lt;/option&gt;
                  &lt;option value="NE"&gt;NE&lt;/option&gt; 
                  &lt;option value="NV"&gt;NV&lt;/option&gt;
                  &lt;option value="NH"&gt;NH&lt;/option&gt; 
                  &lt;option value="NJ"&gt;NJ&lt;/option&gt;
                  &lt;option value="NM"&gt;NM&lt;/option&gt; 
                  &lt;option value="NY"&gt;NY&lt;/option&gt;
                  &lt;option value="NC"&gt;NC&lt;/option&gt; 
                  &lt;option value="ND"&gt;ND&lt;/option&gt;
                  &lt;option value="OH"&gt;OH&lt;/option&gt; 
                  &lt;option value="OK"&gt;OK&lt;/option&gt;
                  &lt;option value="OR"&gt;OR&lt;/option&gt; 
                  &lt;option value="PA"&gt;PA&lt;/option&gt;
                  &lt;option value="RI"&gt;RI&lt;/option&gt; 
                  &lt;option value="SC"&gt;SC&lt;/option&gt;
                  &lt;option value="SD"&gt;SD&lt;/option&gt; 
                  &lt;option value="TN"&gt;TN&lt;/option&gt;
                  &lt;option value="TX"&gt;TX&lt;/option&gt; 
                  &lt;option value="UT"&gt;UT&lt;/option&gt;
                  &lt;option value="VT"&gt;VT&lt;/option&gt; 
                  &lt;option value="VA"&gt;VA&lt;/option&gt;
                  &lt;option value="WA"&gt;WA&lt;/option&gt; 
                  &lt;option value="WV"&gt;WV&lt;/option&gt;
                  &lt;option value="WI"&gt;WI&lt;/option&gt; 
                  &lt;option value="WY"&gt;WY&lt;/option&gt;
              &lt;/select&gt;
          &lt;/div&gt;
          &lt;div id="zip"&gt;
              &lt;label id="zip" class="label" for="zip"&gt;ZIP Code:&lt;/label&gt; 
                  &lt;input id="zip" class="name" type="text" name="zip" /&gt;
          &lt;/div&gt;
  
              &lt;label class="nobg" for="uploaded_file"&gt;Company Logo:&lt;/label&gt; 
                  &lt;input class="nobg" type="file" name="uploaded_file" /&gt;
      &lt;/div&gt;
  
      &lt;div id="right"&gt;
         &lt;div style="width:50%; float:left;"&gt;
          &lt;label class="label" for="validOthers"&gt;Valid with other Coupons:&lt;/label&gt; 
              &lt;select class="dropdown" name="validOthers"&gt; 
                  &lt;option value="yes"&gt;Yes&lt;/option&gt; 
                  &lt;option value="no"&gt;No&lt;/option&gt; 
              &lt;/select&gt;
  
          &lt;label class="label" for="oneCoupon"&gt;One Coupon Per Customer:&lt;/label&gt; 
              &lt;select class="dropdown" name="oneCoupon"&gt; 
                  &lt;option value="yes"&gt;Yes&lt;/option&gt; 
                      &lt;option value="no"&gt;No&lt;/option&gt; &lt;/select&gt;
         &lt;/div&gt;
         &lt;div style="width:50%; float:left;"&gt;
          &lt;label class="label" for="outOfarea"&gt;Out of Area Coupon:&lt;/label&gt; 
              &lt;select class="dropdown" name="outOfarea"&gt; 
                  &lt;option value="yes"&gt;Yes&lt;/option&gt; 
                      &lt;option value="no"&gt;No&lt;/option&gt; &lt;/select&gt;
  
              &lt;label class="label" for="businessCategory"&gt;Business Category (Optional):&lt;/label&gt; 
                  &lt;input style="width:200px;" type="text" name="businessCategory" /&gt; 
         &lt;/div&gt;
  
  
          &lt;h2&gt;Offer 1&lt;/h2&gt;
              &lt;label class="label" for="offer1"&gt;Your Offer:&lt;/label&gt; 
                  &lt;input type="text"class="offers" name="offer1" /&gt; 
              &lt;label class="label" for="offer1Days"&gt;Number of days the offer will last for:&lt;/label&gt; 
                  &lt;input class="days" type="text" name="offer1Days" /&gt; 
              &lt;label class="label" for="offer1Quantity"&gt;Number of Coupons:&lt;/label&gt; 
                  &lt;input class="qty" type="text" name="offer1Quantity" /&gt;
  
          &lt;h2&gt;Offer 2&lt;/h2&gt;
              &lt;label class="label" for="offer2"&gt;Your Offer:&lt;/label&gt; 
                  &lt;input type="text" class="offers" name="offer2" /&gt; 
              &lt;label class="label" for="offer2Days"&gt;Number of days the offer will last for:&lt;/label&gt; 
                  &lt;input class="days" type="text" name="offer2Days" /&gt; 
              &lt;label class="label" for="offer2Quantity"&gt;Number of Coupons:&lt;/label&gt; 
                  &lt;input class="qty" type="text" name="offer2Quantity" /&gt;
          &lt;h2&gt;Offer 3&lt;/h2&gt;
              &lt;label class="label" for="offer3"&gt;Your Offer:&lt;/label&gt; 
                  &lt;input type="text" class="offers" name="offer3" /&gt; 
              &lt;label class="label" for="offer3Days"&gt;Number of days the offer will last for:&lt;/label&gt; 
                  &lt;input class="days" type="text" name="offer3Days" /&gt; 
              &lt;label class="label" for="offer3Quantity"&gt;Number of Coupons:&lt;/label&gt; 
                  &lt;input class="qty" type="text" name="offer3Quantity" /&gt; 
  
  &lt;input id="order" class="submit" type="submit" value="Submit" /&gt;
  </code></pre>
  
  <p>`</p>
  
  <p>Here's the PHP submit</p>
  
  <pre><code>if(isset($_POST['email'])) {
  
      $email_to = "joel@joelshawdesigns.com";
      $email_subject = "test form Submission";
  
  
      function died($error) {
          // your error code can go here
          echo "We are very sorry, but there were error(s) found with the form you submitted. ";
          echo "These errors appear below.&lt;br /&gt;&lt;br /&gt;";
          echo $error."&lt;br /&gt;&lt;br /&gt;";
          echo "Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;";
          die();
      }
  
      if(!isset($_POST['distributorName']) ||
          !isset($_POST['distributorId']) ||    
          !isset($_POST['email']) ||
          !isset($_POST['businessName']) ||
          !isset($_POST['phone']) ||                        
          !isset($_POST['street']) ||                        
          !isset($_POST['city']) ||                        
          !isset($_POST['state']) ||                        
          !isset($_POST['zip']) ||                        
          !isset($_POST['validOthers']) ||                        
          !isset($_POST['oneCoupon']) ||
          !isset($_POST['outOfarea']) ||                        
          !isset($_POST['businessCategory']) ||                                
          !isset($_POST['offer1']) ||                        
          !isset($_POST['offer1Days']) ||                        
          !isset($_POST['offer1Quantity']) ||                        
          !isset($_POST['offer2']) ||                        
          !isset($_POST['offer2Days']) ||                        
          !isset($_POST['offer2Quantity']) ||                        
          !isset($_POST['offer3']) ||                        
          !isset($_POST['offer3Days']) ||                        
          !isset($_POST['offer3Quantity']
          )) {
          died('We are sorry, but there appears to be a problem with the form you submitted.');       
      }
  
      $distributorName = $_POST['distributorName']; // not required
      $distributorId = $_POST['distributorId']; // not required
      $email_from = $_POST['email']; // not required
      $businessName = $_POST['businessName']; // not required
      $phone = $_POST['phone']; // not required
      $street = $_POST['street']; // not required
      $city = $_POST['city']; // not required    
      $state = $_POST['state']; // not required
      $zip = $_POST['zip']; // not required
      $validOthers = $_POST['validOthers']; // not required
      $oneCoupon = $_POST['oneCoupon']; // not required
      $outOfarea = $_POST['outOfarea']; // not required
      $businessCategory = $_POST['businessCategory']; // not required    
      $offer1 = $_POST['offer1']; // not required
      $offer1Days = $_POST['offer1Days']; // not required
      $offer1Quantity = $_POST['offer1Quantity']; // not required
      $offer2 = $_POST['offer2']; // not required
      $offer2Days = $_POST['offer2Days']; // not required
      $offer2Quantity = $_POST['offer2Quantity']; // not required
      $offer3 = $_POST['offer3']; // not required
      $offer3Days = $_POST['offer3Days']; // not required
      $offer3Quantity = $_POST['offer3Quantity']; // not required
  
      $error_message = "";
      $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email_from)) {
      $error_message .= 'Hmm...The Email Address you entered does not appear to be valid.&lt;br /&gt;';
    }
      $string_exp = "/^[A-Za-z .'-]+$/";
    if(!preg_match($string_exp,$distributorName)) {
      $error_message .= 'Hmm...The name you entered does not appear to be valid.&lt;br /&gt;';
    }
    if(!preg_match($string_exp,$businessName)) {
      $error_message .= 'The Business Name you entered does not appear to be valid.&lt;br /&gt;';
    }
      $string_exp = "/^[0-9.'-]+$/";  
    if(!preg_match($string_exp,$phone)) {
      $error_message .= 'The Phone Number you entered does not appear to be valid.&lt;br /&gt;';
    }    
  
    if(strlen($error_message) &gt; 0) {
      died($error_message);
    }
      $email_message = "Order From: $distributorName\n\n";
  
  
      function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
      }
  
      $email_message .= "Distributor Name:  ".clean_string($distributorName)."\n";
      $email_message .= "Distributor ID:  ".clean_string($distributorId)."\n";
      $email_message .= "Distributor Email:  ".clean_string($email_from)."\n";
      $email_message .= "Business Name:  ".clean_string($businessName)."\n";
      $email_message .= "Business Telephone:  ".clean_string($phone)."\n";
      $email_message .= "Business Street:  ".clean_string($street)."\n";
      $email_message .= "Business City:  ".clean_string($city)."\n";
      $email_message .= "Business State:  ".clean_string($state)."\n";
      $email_message .= "Business Zip:  ".clean_string($zip)."\n"; 
      $email_message .= "Valid with other coupons:  ".clean_string($validOthers)."\n";
      $email_message .= "One coupon per customer:  ".clean_string($oneCoupon)."\n";
      $email_message .= "Out of Area Coupon:  ".clean_string($outOfarea)."\n";
      $email_message .= "Business Category:  ".clean_string($businessCategory)."\n";    
      $email_message .= "Offer 1:  ".clean_string($offer1)."\n";
      $email_message .= "Offer 1 Duration:  ".clean_string($offer1Days)."\n";
      $email_message .= "Offer 1 Quantity:  ".clean_string($offer1Quantity)."\n";
      $email_message .= "Offer 2:  ".clean_string($offer2)."\n";
      $email_message .= "Offer 2 Duration:  ".clean_string($offer2Days)."\n";
      $email_message .= "Offer 2 Quantity:  ".clean_string($offer2Quantity)."\n";
      $email_message .= "Offer 3: ".clean_string($offer3)."\n";
      $email_message .= "Offer 3 Duration:  ".clean_string($offer3Days)."\n";
      $email_message .= "Offer 3 Quantity:  ".clean_string($offer3Quantity)."\n";
  
  if ( move_uploaded_file ($_FILES['uploaded_file'] ['tmp_name'], 
         "uploads/{$_FILES['uploadFile'] ['name']}")  )
        {  print '&lt;p&gt; The file has been successfully uploaded &lt;/p&gt;';
         }     
  
  $headers = 'From: '.$email_from."\r\n".
  'Reply-To: '.$email_from."\r\n" .
  'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);  
  ?&gt;
  &lt;!-- include your own success html here --&gt;
  </code></pre>