title: PHP Post email Sending email multiple times
body: <p>I have a script that sends an email with information stored in sessions - the info is collected from a form a user fills out. <strong><em>(the form 'action' is pointed to the code below)</em></strong></p>
  
  <p>However, when the submit button is clicked twice on the form for example, 2 emails are sent and when clicked 3 times, 3 emails are sent.</p>
  
  <p>I want to make sure that only 1 email is sent and if any session is empty no email is to be sent:</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  if(isset($_POST['email'])) {
  
  $email_to = "recipient@emailaddress.com";
  $email_subject = "My Subject";
  
  
  $machine = implode(",", $_SESSION['machinesesh']); //required 
  $machine_type = implode(" ", $_SESSION['typesesh']);; // required
  $address = $_SESSION['addresssesh']; //required
  $county = $_SESSION['countysesh']; //required
  $postcode = $_SESSION['postcodesesh']; //required
  $workplace = implode(', ', $_SESSION['worksesh']); //required
  $serving = implode(', ', $_SESSION['peoplesesh']);
  $company_name = $_SESSION['namesesh']; // required
  $visitorname = $_POST['yourname']; // required
  $email_from = $_POST['email']; // required
  $telephone = $_POST['telephone']; // not required
  
  
  $email_message = "New email alert .\n\n";
  
  function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
  }
  
  $email_message .= "Name: ".clean_string($visitorname)."\n";
  $email_message .= "Company: ".clean_string($company_name)."\n";
  $email_message .= "Address Line 1: ".clean_string($address)."\n";
  $email_message .= "County: ".clean_string($county)."\n";
  $email_message .= "Postcode: ".clean_string($postcode)."\n";
  $email_message .= "Machine(s) Wanted: ".clean_string($machine)."\n";
  $email_message .= "Environment: ".clean_string($workplace)."\n";
  $email_message .= "Serving: ".clean_string($serving)."\n";
  $email_message .= "Email: ".clean_string($email_from)."\n";
  $email_message .= "Telephone: ".clean_string($telephone)."\n";
  
  
  // create email headers
  $headers = 'From: sendaddress@email.com' . "\r\n".
  'Reply-To: '.$email_from."\r\n" .
  'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers); 
  
  header("Location: http://www.example.com?tag=$machine");
  
  ?&gt;
  </code></pre>