-2

I made a little system to sign up for an event. It all worked like a charm and just the way I wanted it. But when I checked it today all of a sudden the comfirmation email people get after signing up for the event wouldn't send anymore. Everything works the same way it did previously, but just the mails won't send anymore. I added a few things to my code and then it wouldn't send anymore.

I added the (!filter_var($email, FILTER_VALIDATE_EMAIL) to verify if the email adress was in the correct format. So if the email was not correct OR if the mail wasn't send for any other reason, you would be redirected to fail.html. Else, you will be redrected to succes.html. After redoing the changes and doublecheking everything I just don't manage to find what's wrong.. Anyone who can tell me what's wrong with the code?

<?php
$connect=mysqli_connect('xxxx','xxxxx','xxxxx','xxxxxx');

if(mysqli_connect_errno($connect))
{
        echo 'Failed to connect';
}

// create a variable
$naam=$_POST['naam'];
$email=$_POST['email'];
$club=$_POST['club'];
$eten=$_POST['eten'];
$moment=$_POST['moment'];
$slapen=$_POST['slapen'];
$acro=$_POST['acro'];
$opmerkingen=$_POST['opmerkingen'];
$datum=$_POST['datum'];
$dateTime = new DateTime("now", new DateTimeZone('Europe/Brussels'));
$mysqldate = $dateTime->format("d-m-Y H:i:s");

//Execute the query


mysqli_query($connect,"INSERT INTO registered (`naam`,`email`,`club`,`eten`,`moment`,`slapen`,`acro`,`opmerkingen`) VALUES('$naam','$email','$club','$eten','$moment','$moment','$acro','$opmerkingen')");


//Mail sending function
$subject = 'Bevestiging inschrijving JWDW van ' . htmlspecialchars($_POST["naam"]);
$to = $_POST['email'];
$from = "xxxxxxx@hotmail.com";

//data
$msg = "<html>
  <body> 

    xxxxxxxxxx

  </center> 
  </body> 
</html>";       

//Headers
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;

mail($to,$subject,$msg,$headers);
echo "Verwerken...";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)or(mysqli_affected_rows($connect) < 0)){
  echo '<script type="text/javascript">
           window.location = "fail.html"
      </script>';
  echo mysqli_error ($connect);

} elseif (mysqli_affected_rows($connect) > 0){
  echo '<script type="text/javascript">
           window.location = "succes.html"
      </script>';
}

?>

Thanks in advance!

Senne

  • 1
    so remove the code change, does it work again then? – RiggsFolly Feb 18 '15 at 18:08
  • Also why send javascript to the browser to tell it to redirect to another page from the server?? Use the php code `header('Location: fail.html')` – RiggsFolly Feb 18 '15 at 18:11
  • Also should'nt you check the email validity before storing it on the database and before trying to use it to send an email. – RiggsFolly Feb 18 '15 at 18:14
  • You need to be more responsive to comments. You shouldn't walk away after asking your question as you then can't react to questions and comments. By the time you get back your window for receiving help has shrunk or gone away. – John Conde Feb 18 '15 at 18:58
  • @JohnConde, writing the whole suggestions here in comment would make it unreadable for him. That's why I posted it as an answer. And upon he provides more info, I could have edited it and updated my answer. That's what I was trying to do by posting my suggestions to figure out the issue, as an answer. Anyway, since you pointed out, am removing my answer. Thank you – Akhilesh B Chandran Feb 18 '15 at 20:49
  • If you can show this isn't due to any of the reasons in the canonical question we can nominate this for re-opening. – John Conde Feb 18 '15 at 21:29

1 Answers1

0

php mail debugging is well answered elsewhere on stackoverflow. However, a more general answer to your question "what's wrong with the code?" is that if there is an error...

  • the user will need re-type their data.
  • the fail.htm page cannot give the user feedback about why their submission failed.
  • the user may begin to form a negative impression about an event which they were previously positive enough to register for.

Also, the code is vulnerable to SQL Injection. See How can I prevent SQL injection in PHP?

Consider rearranging the code using the following logic...

  • Validate the submitted data. If it is invalid, then redisplay the form, including the submitted data and an error message, so the user can correct it.
  • Attempt to update the db record. If no records are affected then attempt to insert the db record. If it fails, redisplay the form as above, and explain the problem.
  • Attempt to send the email. If it fails, redisplay the form as above, and explain in the message that they are registered, but the confirmation email wasn't sent and do they want to change any of the details?
  • Consider using php functions or classes to avoid duplication and improve clarity.
Community
  • 1
  • 1
Megawhat
  • 1
  • 1
  • 1
    This doesn't seem to answer the original question... These are good points, but the answers section is intended for complete answers to the *original* question, not tips on UX design and SQL Injections. – Tim Lewis Feb 18 '15 at 19:23