title: Strip Bad Characters from an HTML PHP Contact Form
body: <p>New to PHP, but making new progress. I have a contact form that will send an email after it has been submitted by the user. Now I want to make this form better, by stripping out bad characters, or anything that could potentially wreck an email. The email will be read by me, so I could technically browse through all the spam, but I don't want to. I want a clean email coming in for documentation purposes.</p>
  
  <p>I am using the $_POST array, here is the HTML for the contact form:</p>
  
  <pre><code>&lt;form class="form" method="post" action="contact.php"&gt;
  
  &lt;p class="name"&gt;
    &lt;input type="text" name="name" id="name" /&gt;
    &lt;label for="name"&gt;Name&lt;/label&gt;
  &lt;/p&gt;
  
  &lt;p class="email"&gt;
    &lt;input type="text" name="email" id="email" /&gt;
    &lt;label for="email"&gt;E-mail&lt;/label&gt;
  &lt;/p&gt;
  
  &lt;p class="web"&gt;
    &lt;input type="text" name="web" id="web" /&gt;
    &lt;label for="web"&gt;Website&lt;/label&gt;
  &lt;/p&gt;
  
  &lt;p class="telephone"&gt;
    &lt;input type="text" name="telephone" id="telephone" /&gt;
    &lt;label for="telephone"&gt;Telephone&lt;/label&gt;
  &lt;/p&gt;
  
  &lt;p class="question"&gt;Preferred Contact Method&lt;/p&gt;
    &lt;p id="preferred_question"&gt;
      &lt;input type="radio" name="contact_option" class="telephone_opt" value="Telephone" /&gt;
      &lt;label for="telephone_opt"&gt;Telephone&lt;/label&gt;&lt;br /&gt;
  
      &lt;input type="radio" name="contact_option" class="email_opt" value="Email" /&gt;
      &lt;label for="email_opt"&gt;Email&lt;/label&gt;&lt;/p&gt;
  
  &lt;p class="question"&gt;Describe what you need..&lt;/p&gt;
    &lt;p class="text"&gt;
      &lt;textarea name="text"&gt;&lt;/textarea&gt;
  &lt;/p&gt;
  
  &lt;p class="submit"&gt;
    &lt;input type="submit" value="Send" /&gt;
  &lt;/p&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Here is my contact.php (form action file)</p>
  
  <pre><code>&lt;?php
  $msg = "Name: " . $_POST['name'] . "&lt;br /&gt;";
  $msg .= "Email: " . $_POST['email'] . "&lt;br /&gt;";
  $msg .= "Website: " . $_POST['web'] . "&lt;br /&gt;";
  $msg .= "Telephone: " . $_POST['telephone'] . "&lt;br /&gt;";
  $msg .= "Preferred Contact Method: " . $_POST['contact_option'] . "&lt;br /&gt;";
  $msg .= "Customer Needs: " . $_POST['text'];
  
  $recipient = "support@mysite.com";
  $subject = "Contact Has Been Made..";
  $mailheaders = "MIME-Version: 1.0" . "\r\n";
  $mailheaders .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $mailheaders .= "From: &lt;support@mysite.com&gt;" . "\r\n";
  
  mail($recipient, $subject, $msg, $mailheaders);
  echo "Thank You!";
  ?&gt;
  </code></pre>