Results 1 to 6 of 6

Thread: Adding valid email into php form

  1. #1
    Join Date
    Feb 2007
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Adding valid email into php form

    Hi, im wanting how to know how i can add this sort of email validation into my form code below.
    PHP Code:
    <?php
    // get posted data into local variables
    $EmailFrom "ryan.fitton@hotmail.co.uk";
    $EmailTo "ryan.fitton@hotmail.co.uk";
    $Subject "Email From Personal Website";
    $YourName Trim(stripslashes($_POST['YourName'])); 
    $YourEmail Trim(stripslashes($_POST['YourEmail'])); 
    $YourMessage Trim(stripslashes($_POST['YourMessage'])); 

    // validation
    $validationOK=true;
    if (!
    $validationOK) {
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
      exit;
    }

    // Check if required fields are filled in
    if(trim($YourName) == '' || trim($YourEmail) == '' || trim($YourMessage) == '') {
    print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
    exit;
    }

    // prepare email body text
    $Body "";
    $Body .= "Name: ";
    $Body .= $YourName;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $YourEmail;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= $YourMessage;
    $Body .= "\n";

    // send email 
    $success mail($EmailTo$Subject$Body"From: <$EmailFrom>");

    // redirect to success page 
    if ($success){
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_sent.php\">";

    }
    else{
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
    }

    ?>
    Last edited by Snookerman; 05-12-2009 at 04:20 PM. Reason: added “Resolved” prefix

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    PHP Code:
    <?php

    function isValidEmail($address) {
      return 
    preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
                        
    $address);
     }

    // get posted data into local variables
    $EmailFrom "ryan.fitton@hotmail.co.uk";
    $EmailTo "ryan.fitton@hotmail.co.uk";
    $Subject "Email From Personal Website";
    $YourName Trim(stripslashes($_POST['YourName'])); 
    $YourEmail Trim(stripslashes($_POST['YourEmail'])); 
    $YourMessage Trim(stripslashes($_POST['YourMessage'])); 

    // validation
    $validationOK=true;
    if (!
    $validationOK) {
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
      exit;
    }

    // Check if required fields are filled in
    if(trim($YourName) == '' || trim($YourEmail) == '' || trim($YourMessage) == '') {
    print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
    exit;
    }

    // check for a valid email

    if(!isValidEmail($YourEmail))
       die(
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">");
    else {
       

    // prepare email body text
    $Body "";
    $Body .= "Name: ";
    $Body .= $YourName;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $YourEmail;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= $YourMessage;
    $Body .= "\n";

    // send email 
    $success mail($EmailTo$Subject$Body"From: <$EmailFrom>");

    // redirect to success page 
    if ($success){
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_sent.php\">";

    }
    else{
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
    }

    }

    ?>
    You can put the function into a separate page, called functions.php for example but this should work for you.

  3. The Following User Says Thank You to Schmoopy For This Useful Post:

    Ryan Fitton (05-11-2009)

  4. #3
    Join Date
    Feb 2007
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Thanks, it works great

  5. #4
    Join Date
    Feb 2007
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Post

    Im also thinking about if it would be a could idea to add this, to my form script aswell, it tests to see wether the email address exists or if it doesnt.

    How would i add this into my code?

  6. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    PHP Code:
    <?php

    function domain_exists($email,$record 'MX')
    {
        list(
    $user,$domain) = split('@',$email);
        return 
    checkdnsrr($domain,$record);
    }

    function 
    isValidEmail($address) {
      return 
    preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
                        
    $address);
     }

    // get posted data into local variables
    $EmailFrom "ryan.fitton@hotmail.co.uk";
    $EmailTo "ryan.fitton@hotmail.co.uk";
    $Subject "Email From Personal Website";
    $YourName Trim(stripslashes($_POST['YourName'])); 
    $YourEmail Trim(stripslashes($_POST['YourEmail'])); 
    $YourMessage Trim(stripslashes($_POST['YourMessage'])); 

    // validation
    $validationOK=true;
    if (!
    $validationOK) {
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
      exit;
    }

    // Check if required fields are filled in
    if(trim($YourName) == '' || trim($YourEmail) == '' || trim($YourMessage) == '') {
    print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
    exit;
    }

    // check for a valid email

    if(!isValidEmail($YourEmail) || !domain_exists($YourEmail))
       die(
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">");
    else {
       

    // prepare email body text
    $Body "";
    $Body .= "Name: ";
    $Body .= $YourName;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $YourEmail;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= $YourMessage;
    $Body .= "\n";

    // send email 
    $success mail($EmailTo$Subject$Body"From: <$EmailFrom>");

    // redirect to success page 
    if ($success){
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_sent.php\">";

    }
    else{
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
    }

    }

    ?>
    This will now check that the email is valid in a string, like does it contain "@" etc... Then if it passes that it then checks for an actual valid email by checking the domain, if it passes that then the email is sent.

    If this doesn't work for you then you may need to find an alternative. checkdnsrr() function does not work on windows.

  7. The Following User Says Thank You to Schmoopy For This Useful Post:

    Ryan Fitton (05-12-2009)

  8. #6
    Join Date
    Feb 2007
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Thankyou =D

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •