PHP-Formular E-Mail-Validierung

Meine E-mail-Formular ist immer noch das versenden von E-Mails, auch wenn die E-Mail-Adresse ist nicht gültig.
Wenn ich zum Beispiel füllen Sie die E-Mail als "bob", und drücken Sie senden, meine javascript-validator gibt eine Warnmeldung angezeigt, aber die E-Mail geht immer noch durch. Es landet in meinem spam-box als [email protected]

Wie kann ich das validieren der E-Mail-Adresse und verhindern, dass Einreichen, wenn es nicht überprüft?

Ich bin neu in php.

HTML:

 <div id="emailform">
                <h2>Confirm your purchase information</h2>
                <hr>
                <form method="post" name="contactform" action="mail_form.php" id="submit">
                <p>
                <label for='name'>Your Name:</label> <br>
                <input type="text" name="name">
                </p>
                <p>
                <label for='email'>Email Address:</label> <br>
                <input type="text" name="email">
                </p>
                <p>
                <label for='purchasecode'>Purchase Code:</label> <br>
                <input type="text" name="purchasecode">
                </p>
                <p>
                <label for='vendor'>Vendor Name:</label> <br>
                <select name="vendor">
                  <option value="" selected="selected"></option>
                  <option value="Amazon" >Amazon</option>
                  <option value="Barnes&Noble" >Barnes &amp; Noble</option>
                  <option value="Family Christian" >Family Christian</option>
                  <option value="Christianbook" >Christianbook.com</option>
                  <option value="LifeWay" >LifeWay</option>
                  <option value="BAM" >Books-A-Million</option>
                  <option value="Mardel" >Mardel</option>
                </select>
                </p>
                <button type="submit" id="submitbutton" name="submit" value="Submit" class="mainButton">SUBMIT</button><br>
                </form>

<!--            Code for validating the form
                Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
                for details -->
                <script type="text/javascript">
                var frmvalidator  = new Validator("contactform");
                frmvalidator.addValidation("name","req","Please provide your name");
                frmvalidator.addValidation("email","email","Please enter a valid email address");
                frmvalidator.addValidation("vendor","dontselect=000");
                frmvalidator.addValidation("purchasecode","maxlen=50");
                </script>
            </div>

PHP:

<?php
ini_set('display_errors',1);
 error_reporting(E_ALL);

if(!isset($_POST['submit']))
{
  //This page should not be accessed directly. Need to submit the form.
  echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$email = $_POST['email'];
$purchasecode = $_POST['purchasecode'];
$vendor = $_POST['vendor'];


//Validate first
if(empty($_POST['name'])  ||
   empty($_POST['email']) ||
   empty($_POST['purchasecode']) ||
   empty($_POST['vendor']))
{
    echo "All fields are required.";
exit;
}

if(IsInjected($email))
{
    echo "Bad email value!";
    exit;
}

$email_from = $email;
$email_subject = "GDFY Purchase Confirmation";
$email_body = "New purchase confirmation from $name.\n".
    "Here are the details:\n\n Name: $name \n\n Email: $email \n\n Purchase Code: $purchasecode \n\n Vendor: $vendor";

$to = "[email protected]";//<== update the email address

$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: index.html');

//echo "success";


//Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?>

Javascript:

  $('#submit').submit(function() { //catch the form's submit event
      $.ajax({ //create an AJAX call...
          data: $(this).serialize(), //get the form data
          type: $(this).attr('method'), //GET or POST
          url: $(this).attr('action'), //the file to call
          success: function(response) { //on success..
              $('#emailform').html("<h2 style='text-align:center;'>Thank you!</h2><hr><p style='text-align:center;'>Thank you for submitting your purchase information.<br>We will send your free gifts soon!</p>"); //update the DIV
          }
      });
      return false; //cancel original event to prevent form submitting
  });
Im ernst, verwenden Sie eine anständige mailer-Klasse wie PHPMailer oder Swiftmailer-es tut alle diese Art von Sachen für Sie, und macht es richtig.
if( // condition email == false) {echo "Bad email value!"; } else { // HERE YOUR MAIL SCRIPT }
Könnten Sie $email_check = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i"; anstelle von dem, was Sie haben, oder fügen Sie es, dann verwenden Sie if(!preg_match($email_check,$email)){ die("Please enter a valid email address!"); }
wenn( // Bedingung email == false) {echo "ungültige E-Mail Wert!"; } else { // HIER IHRE E-MAIL-SCRIPT }Dies scheint zu stoppen die schlechten E-Mails senden, aber mein javascript noch aktualisiert, div mit einer Erfolgsmeldung. Wie kann ich dies verhindern?
Sie müssen die Prüfung auf gültige Zeichen. Wie in meinem obigen Beispiel (bearbeitet) Kommentar. Es funktioniert, aber es gibt Raum für Verbesserungen.

InformationsquelleAutor eloist | 2013-09-04

Schreibe einen Kommentar