Empty email from contact form ?!

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	
	$email_to = "mail@email.com";
	$name = $_POST["name"];
	$email = $_POST["email"];
	$url = $_POST["url"];
	$message = $_POST["message"];
	$text = "User's Name: $name\
" .
    "User's Email: $email\
" .
    "User's TLF: $url\
" .
    "User's Message:\
\
" .
    "$message";
	$headers = "MIME-Version: 1.0" . "\\r\
"; 
	$headers .= "Content-type:text/html; charset=utf-8" . "\\r\
"; 
	$headers .= "FROM: <$email>" . "\\r\
";
	mail($email_to, "Message", $text, $headers);
} 
?>

Above is okay ?

Sorry not sure how to use/place it:

function valid_email($email) {
    regex = "/^[\\S]+@[\\S]+\\.[\\S]+$/"
    return (bool) preg_match($regex, (string) $email);
}

You could do something like this:


function valid_email($email) {
    $regex = "/^[\\S]+@[\\S]+\\.[\\S]+$/"
    return (bool) preg_match($regex, (string) $email);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email_to = "mail@email.com";
    $name = $_POST["name"];
    $email = $_POST["email"];
    $url = $_POST["url"];
    $message = $_POST["message"];

    // Check for valid form data
    if (!empty($email) AND valid_email($email) AND !empty($name) AND !empty($message)) {
         $text = "User's Name: $name\
" .
        "User's Email: $email\
" .
        "User's TLF: $url\
" .
        "User's Message:\
\
" .
        "$message";
        $headers = "MIME-Version: 1.0" . "\\r\
";
        $headers .= "Content-type:text/html; charset=utf-8" . "\\r\
";
        $headers .= "FROM: <$email>" . "\\r\
";
        mail($email_to, "Message", $text, $headers);
    }
}

I have just copy/pasted above code
I get an error on this line :

return (bool) preg_match($regex, (string) $email); 

Sorry, looks like I missed a semicolon off the end of this line $regex = "/^[\\S]+@[\\S]+\\.[\\S]+$/"

Great that helped.
Thanks alot for all your help, cant wait to see if this also makes the empty emails disappear.
Thanks again

No need to wait, try sending an empty email yourself. :wink:

You are right. :slight_smile: I have tested it and it works. Thanks again