1

I am having problems making a website with a contact form. First of all, any email address entered it spews out as invalid. Also, I have a expectancy for the name, and I'm not sure how to add special characters (Eg. É) and spaces for the last name.

Here is the existing PHP:

$error_message = "";
$email_exp = "/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z];{2,4}$/";

if (!preg_match($email_exp, $email)) {
    $error_message .= '<b> The Email Address you entered does not appear to be valid. </b> <br/>';
}

$string_exp = "/^[A-Za-z.'-]+$/";   
if (!preg_match($string_exp, $name)) {
    $error_message .= '<b> The Name you entered does not appear to be valid. </b> <br/>';
}

if(strlen($subject) < 3){
    $error_message .= '<b> The Subject you entered does not appear to be valid. </b> <br/>';    
}

if(strlen($message) < 2){
    $error_message .= '<b> The Message you entered does not appear to be valid. </b> <br/>';    
}

Updated PHP for @J. Robertson:

<?php

if (isset($_POST['email'])){

// email info
$email_to ="example@example.com";
$email_subject = $_POST['subject'];
$email_from = $_POST['email'];

//error code

function died($error) {
    echo "I am sorry, but there seems to be error(s) found within the form you submitted. <br/>";
    echo "The error(s) will appear below: <br/> <br/>";
    echo $error. "<br/><br/>";
    echo "Please go back and fix these error(s). <br/>";
    die();
    }

//validation

    if(!isset($_POST['name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['subject']) || 
    !isset($_POST['message'])) {
        died('I am sorry, but there appears to be a problem with the form you submitted.');
        }

    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

//expected strings

$error_message = "";
$email_exp = '/[^\s]*@[a-z0-9.-]*/i';

if (!preg_match($email_exp, $email)) {
    $error_message .= '<b> The Email Address you entered does not appear to be valid. </b> <br/>';
    }

$string_exp = "/^[A-Za-z.'-]+$/";   
if (!preg_match($string_exp, $name)) {
    $error_message .= '<b> The Name you entered does not appear to be valid. </b> <br/>';
    }
    if(strlen($subject) < 3){
    $error_message .= '<b> The Subject you entered does not appear to be valid. </b> <br/>';    
    }

if(strlen($message) < 2){
    $error_message .= '<b> The Message you entered does not appear to be valid. </b> <br/>';    
    }

if(strlen($error_message) > 0 ){
    died($error_message);
    }
    $email_message = "Form Details below.\n\n";

//sanitization
function clean_string($string){ 
    $bad = array("content-type", "bcc:", "to:", "cc:", "href");
    return str_replace($bad, "", $string);
    }

$email_message .= "Name:" . clean_string($name) . "\n";
$email_message .= "E-Mail:" . clean_string($email) . "\n";
$email_message .= "Subject:" . clean_string($subject) . "\n";
$email_message .= "Message:" . clean_string($message) . "\n";

//email headers
$headers = 'From: ' .$email_From . '\r\n'. 'Reply-To' .
    $email. "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);

} ?>

  • 1
    Use [`filter_var()`](http://php.net/manual/en/function.filter-var.php) for validating emails: `filter_var('bob@example.com', FILTER_VALIDATE_EMAIL)` – John Conde Aug 06 '13 at 18:17
  • 1
    filter_var makes problems if the email contains non latin characters – Didatus Aug 06 '13 at 18:20

1 Answers1

0

I suspect the problem lies with your reg ex. Using http://gskinner.com/RegExr/ to test it against test@test.com, your reg ex does not pick up even that basic email address.

It's highly unlikely you'll come up with a regex that covers all email possibilities- it may be more worthwhile to simply check for an '@' sign, and then send a test email to the server to see if it's valid.

Otherwise, if you'd like a valid regex to test against, try

/[^\s]*@[a-z0-9.-]*/i
JRizz
  • 226
  • 3
  • 12
  • So, this would replace what I currently have? If so, then I get this error: "preg_match(): Unknown modifier '<'" and it still gives me invalid email address. –  Aug 06 '13 at 18:47
  • Yes, that would replace it. What are you using as the test email address? – JRizz Aug 06 '13 at 18:50
  • I'm using a hotmail address, however, using that gives me a preg_match(): Unknown modifier'< error. EDIT: I fixed that error, however, it still doesn't the recognize hotmail address. EDIT 2: I tried it with a domain address as well, still doesn't work. –  Aug 06 '13 at 18:52
  • I've edited my answer to include a much simpler version - it worked on test@hotmail.com. Bear in mind, verifying an email with regex is generally discouraged - see the answer to this question http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address?lq=1 – JRizz Aug 06 '13 at 19:09
  • Didn't even think of that, so the preferred way of checking email addresses would be this way? http://www.devshed.com/c/a/PHP/Email-Address-Verification-with-PHP/1/ –  Aug 06 '13 at 19:18
  • This fixed the email problems, but brought up a new one. It now says this code is not correct (email header section): " //email headers $headers = 'From: ' .$email_From . '\r\n'. 'Reply-To' . $email. "\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); " The error I'm getting is: "Notice: Undefined variable: email_From" –  Aug 06 '13 at 19:31
  • hm. Without seeing more of the code to get context, it's hard to tell. are you sure $email_From is defined and in scope? – JRizz Aug 06 '13 at 19:36
  • That's the problem - when you define email_from, it's lower case: $email_from = $_POST['email']; At the bottom, in your $headers definition, From is capitalized. its a simple typo. – JRizz Aug 06 '13 at 19:52
  • Hey, did you get everything figured out? If so, I'd really appreciate it if you'd mark this as the answer. Thanks! – JRizz Aug 06 '13 at 21:16
  • Oh, sorry, I got busy doing other things. Yes, I did get the email line figured out, however, do you have a solution for the name line? I need to make it so doesn't say spaces are invalid. (for last names) Thanks –  Aug 06 '13 at 23:15
  • The php function trim can remove white spaces. http://php.net/manual/en/function.trim.php – JRizz Aug 07 '13 at 13:40
  • That's not exactly what I'm looking for, the problem is with the name expectancy, it doesn't allow spaces, so the user who enters their first and last name will be told that it is invalid as they have a space between the first and last name. Also, for people with non-latin characters in their name, it would say that their name is invalid. –  Aug 07 '13 at 13:49