0

I have a contact form at http://www.kaimeramedia.com/derek/Website/contact.php .I checked my PHP code at PHPcodechecker.com and it says there are no syntax errors. I think the beginning code is working in some manor because every time I press submit it makes it to the next validation where I am left with: "Improper email address detected. Please hit your browser back button and try again".

I just want to know if I should be writing the code differently to make sure the email validation goes through and allow the message to be submitted. I'm Not sure if I have something coded backwards. I tried many combinations, but as it stands this is the farthest I have come without a mailform.php error. The code for the mailform.php file is:

 <?php 
     session_start();
     $dontsendemail = 0;
     $possiblespam = FALSE;
     $strlenmessage = "";
     $email = $_REQUEST['email']; 
     $name = $_REQUEST['name']; 
     $message = $_REQUEST['message']; 
     $subject = "Regarding Your Portfolio";$emailaddress = "[email protected]"; 

     // checks if name field is empty
     function checkname() {
if (empty($name)) {
    die ("You did not enter your name.  Please hit your browser back button and try again.");
return 1;
            }
        }

// checks if the captcha is input correctly
function checkcaptcha() {
            if ($_SESSION["pass"] != $_POST["userpass"]) {
                die("Sorry, you failed the CAPTCHA. Note that the CAPTCHA is case-sensitive. Please hit your browser back button and try again.");
                return 1;
            }
        }

// checks proper syntax 
function checkemail() {
    if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email));
    else{
        die("Improper email address detected. Please hit your browser back button and try again."); 
        return 1;
    }
}


function spamcheck($field) {
    if(eregi("to:",$field) || eregi("cc:",$field) || eregi("\r",$field) || eregi("\n",$field) || eregi("%0A",$field)){ 
        $possiblespam = TRUE;
    }else $possiblespam = FALSE;
    if ($possiblespam) {
        die("Possible spam attempt detected. If this is not the case, please edit the content of the contact form and try again.");
        return 1;
    }
}
function strlencheck($field,$minlength,$whichfieldresponse) {
    if (strlen($field) < $minlength){
        die($whichfieldresponse); 
        return 1;
    }



}

        if ($dontsendemail == 0) $dontsendemail = checkcaptcha($email);

if ($dontsendemail == 0) $dontsendemail = checkemail($email);
if ($dontsendemail == 0) $dontsendemail = spamcheck($email);
if ($dontsendemail == 0) $dontsendemail = spamcheck($name);
if ($dontsendemail == 0) $dontsendemail = strlencheck($email,10,"The email address field is too short. Please hit your browser back button and check your entry.<br />");
if ($dontsendemail == 0) $dontsendemail = strlencheck($message,10,"The message field is too short. Please hit your browser back button and check your entry.<br />");
if ($dontsendemail == 0) $dontsendemail = strlencheck($emailaddress,8,"You have not selected a recipient of your message. Please hit your browser back button and check your entry.<br />");
if ($dontsendemail == 0) $dontsendemail = strlencheck($name,3,"The Name field is too short. Please hit your browser back button and check your entry.<br />");
if ($dontsendemail == 0) {mail($emailaddress,"Subject: $subject",$message,"From: $name,$email" ); include "thankyou.php";}
?> 

I put a dummy email in this code, but the real one is in the mailform.php file If anyone can see where I went wrong it would be of greatly appreciated.

3
  • Can you upload the email address your testing with? Commented Feb 25, 2012 at 4:22
  • @giorgio I thought it may have been a regex issue. Commented Feb 25, 2012 at 4:39
  • It's on the php page if you look at the source code at www.kaimeramedia.com/derek/Website/mailform.php Commented Feb 25, 2012 at 13:14

2 Answers 2

2

Building on my previous answer, I went all out and cleaned up your script. I clearly have too much time on my hands. Haven't checked if it works. Goodluck!

<?PHP
session_start();
try{
    $check = new check();
    if(!isset($_REQUEST['Email']))
        throw new exception('You did not enter an email address.');

    if(!isset($_REQUEST['message']))
        throw new exception('You did not enter a message.');

    if(!isset($_REQUEST['Name']))
        throw new exception('You did not enter a name');

    $sender = $_REQUEST['Email'];
    $message = $_REQUEST['message'];
    $name = $_REQUEST['Name'];
    $recipient = '[email protected]';

    $subject = 'Regarding Your Portfolio';

    if($check->captcha('userpass') == FALSE)
        throw new exception('Your captcha is incorrect.');

    if($check->spam($sender) == FALSE)
        throw new exception('Your email field contains spam.');

    if($check->spam($name) == FALSE)
        throw new exception('Your name field contains spam.');

    if($check->length($sender, 10) == FALSE)
        throw new exception('Your email field does not satisfy the minimum character count.');

    if($check->length($message, 8) == FALSE)
        throw new exception('Your message field does not satisfy the minimum character count.');

    if($check->length($name, 3) == FALSE)
        throw new exception('Your name field does not satisfy the minimum character count.');

    mail($recipient, $subject, $message, "From: $name <$sender>" );
    include "thankyou.php";
}catch (Exception $E){
    die($E->getMessage());
}




class check{

    function captcha($field){
        if(isset($_REQUEST[$field])==FALSE){ return false; }
        if($_SESSION['pass'] != $_REQUEST[$field]){ return false; }
        return true;
    }

    function email($email){
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ return false;}
        return true;
    }

    function spam($field){
        if(eregi("to:",$field) || eregi("cc:",$field) || eregi("\r",$field) || eregi("\n",$field) || eregi("%0A",$field)){ return false; }
        return true;
    }

    function length($field, $min){
        if(strlen($field) < $min){ return false; }
        return true;
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

I tried your above code in both phpcodechecker.com and I made a mailform_test.php page located at www.kaimeramedia.com/derek/Website/mailform_test.php and the code checker found no errors.
However when you load the contact.php page and fill in all the boxes correctly and hit submit you are left with the resulting page and text: I tried your above code in both phpcodechecker.com and I made a mailform_test.php page located at www.kaimeramedia.com/derek/Website/mailform_test.php and the code checker found no errors. However when you load the page you are automatically sent to a new page with the resulting text: You did not enter an email address.
I sent you to the wrong location... www.kaimeramedia.com/derek/Website/contact_testnew.php
That is because your field name is 'Email' not 'email'. Same with the name field. I have fixed the code.
Thanks Michael. One last thing if you can help. How do I make the thankyou.php page load in the same content window. My menu is setup as such to allow for this: <a href="javascript:loadContent('#content', 'cgi_new.php'), loadContent('#footer', 'footer_cgi.php');"> but I'm not sure how to adapt the code for the mailform page. By the way I just made everything lower case... so weird how I didn't see the Name and Email were capitalized. If you can help let me know
|
0

There are two problems with your script.

  1. You are not parsing in the $email paramater. You forgot to include it.
  2. Regular Expressions for emails are very picky and don't always include all the standards.

Solution: As of PHP5 you can use the following to validate an email or if you want to use the regular expression just add the $email paramater.

function checkemail($email) {
    if(filter_var($email, FILTER_VALIDATE_EMAIL)){
    }else{
        die("Improper email address detected. Please hit your browser back button and try again."); 
        return 1;
    }
}

1 Comment

I tried just entering this code into the original mailform.php page but I still get the same results regarding email. Try it out at www.kaimeramedia.com/derek/Website/contact.php. This email validation is a bane in my side!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.