0

I have a script that sends an email with information stored in sessions - the info is collected from a form a user fills out. (the form 'action' is pointed to the code below)

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.

I want to make sure that only 1 email is sent and if any session is empty no email is to be sent:

<?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");

?>
  • You could add a counter variable to the session, if it is empty/0/not set, send the email. Otherwise, don't. – Antony Jan 23 '17 at 14:18
  • Does it send three times (with three clicks) without reloading or redirecting? – Progrock Jan 23 '17 at 14:40
  • Similar: http://stackoverflow.com/questions/4614052/how-to-prevent-multiple-form-submission-on-multiple-clicks-in-php – Progrock Jan 23 '17 at 14:43

2 Answers2

0

How about settings an $_SESSION["emailsent"] = 1; ? Before you send the email, check if the variable exists AND if it is set to 1! In this case you know, that in this session the email is only sent once!

Nilse
  • 140
  • 9
0

You can try to erase the session after sending the email. The next time the form is submitted, it should check if session value is set or not. If not set, then it should not send email again.

Nadir Latif
  • 3,690
  • 1
  • 15
  • 24