Email validation script

Hi, I want to add an email verification to my form. I have added this code to my PHP script, but the script does not fire back a warning telling me that the email address is valid. I also found this script, which is most accurate and most important reliable ?

// include SMTP Email Validation Class
require_once('smtp_validateEmail.class.php');

// the email to validate
$email = 'user@example.com';
// an optional sender
$sender = 'user@mydomain.com';
// instantiate the class
$SMTP_Validator = new SMTP_validateEmail();
// turn on debugging if you want to view the SMTP transaction
$SMTP_Validator->debug = true;
// do the validation
$results = $SMTP_Validator->validate(array($email), $sender);
// view results
echo $email.' is '.($results[$email] ? 'valid' : 'invalid')."\
";

// send email?
if ($results[$email]) {
  //mail($email, 'Confirm Email', 'Please reply to this email to confirm', 'From:'.$sender."\\r\
"); // send email
} else {
  echo 'The email addresses you entered is not valid';
}

My submit.php form code:

<?php
session_start();
$your_email ='';// <<=== update to your email address

$errors = '';
$name = '';
$visitor_email = '';
$user_message = '';
if(isset($_POST['name']))
{//Never check for submit button - IE has a but and doesn't always send it
	$name = $_POST['name'];
	$visitor_email = $_POST['email'];
	$user_message = $_POST['message'];
	///------------Do Validations-------------
	if(empty($name)||empty($visitor_email))
	{
		$errors .= "\
 Name and Email are required fields. ";
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\
 Bad email value!";
	}
	if(empty($_SESSION['6_letters_code'] ) ||
	strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
		//Note: the captcha code is compared case insensitively.
		//if you want case sensitive match, update the check above to
		// strcmp()
			$errors .= "\
 The captcha code does not match!";
	}
	if(empty($errors))
	{
		//send the email
		$to = $your_email;
		$subject="New form submission";
		$from = $your_email;
		$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
		$body = "A user  $name submitted the contact form:\
".
		"Name: $name\
".
		"Email: $visitor_email \
".
		"Message: \
 ".
		"$user_message\
".
		"IP: $ip\
";
		$headers = "From: $from \\r\
";
		$headers .= "Reply-To: $visitor_email \\r\
";
		
		mail($to, $subject, $body,$headers);

	}
}

// Function to validate against any email injection attempts

function IsInjected($str)
{
	$injections = array('(\
+)',
	'(\\r+)',
	'(\	+)',
	'(%0A+)',
	'(%0D+)',
	'(%08+)',
	'(%09+)'
	);
	$inject = join('|', $injections);
	$inject = "/$inject/i";
	if(preg_match($inject,$str))
	{
		return true;
	}
	else
	{
		return false;
	}
//THIS IS THE LOCATION I PLACED THE ABOVE SCRIPT, IF IT'S DEPENDABLE.
}

?>

I’m guessing I may need to add a warning to the form. I have found a few for such a task this one located on this page, interests me and it doesn’t seem hard to add just add a class to the HTML form with a specific word.

I originally misread your post. You’re trying to see if an email exists or not?

Or are you trying to validate an email address? Php has built in email validation

Here is how I validate an email address:

if (strlen($email) > 0) {
$clean_email = filter_var($email, FILTER_VALIDATE_EMAIL);
if ($clean_email === FALSE) {
echo ‘Please enter a valid email’;
}
} else {
echo ‘Please enter an email address’;
}

Hi, I want to see if an email address exists. I don’t know which route is the most effective.

Validation = “Is this formatted the way an email address should be”
Exists is something entirely different, and not (to my knowledge) something that can be identified without actually trying to send a mail to that account.

Then what do those scripts do, from my knowledge they validate if the email address is valid, unless upon sending they validate unless they fireback that it wasn’t valid. One of the links I posted in my original message is
How to check if an email address exists without sending an email?

Okay, technically it doesnt send an email; it sends the socket commands for everything up to sending the message (Mail FROM, ADDR). It’s still connecting to the remote host and sending the commands. So no, it doesnt send an email - but it still relies on being able to connect to the recipient’s email server. If fsockopen is allowed on your server, and the receiving server is amenable to socket connections in this format, it -should- work for most domains.

How do I get it to work to see if it works :slight_smile: ? I couldn’t decide which script to use what I had posted, unless neither is effective and someone knows of another ?

Well you cant place it where you have labeled it (Anything after the RETURN command will never execute) - Use the inject function first, then do the exists check.

As for which of the two linked scripts - the second one doesn’t actually check the existence of an email address - it just validates that it’s in the correct form.

Well you cant place it where you have labeled it (Anything after the RETURN command will never execute) - Use the inject function first, then do the exists check.

I should place the script before this line;

// Function to validate against any email injection attempts

As for which of the two linked scripts - the second one doesn’t actually check the existence of an email address - it just validates that it’s in the correct form.

Which second one are you referring to the code or the actual link ? Since the code comes after the link in my first message.

Link2 points to a Javascript validator - which doesnt check the existance of an email address.

Link1 points to the class file for CodeBlock1.

CodeBlock1 can be placed inside CodeBlock2 anywhere. If you wanted to functionalize it, then:


function EmailExists($email = '') {
require_once('smtp_validateEmail.class.php');
$sender = 'user@mydomain.com'; //Set me.
$SMTP_Validator = new SMTP_validateEmail();
$SMTP_Validator->debug = true; //Probably dont want this when you're done testing
$results = $SMTP_Validator->validate(array($email), $sender);
return $results;
}

and then after the if(IsInjected … block:


    if(!EmailExists($visitor_email))
    {
        $errors .= "\
 Email does not exist/server unfriendly!";
    }

Where does this code block go;

function EmailExists($email = '') {
require_once('smtp_validateEmail.class.php');
$sender = 'user@mydomain.com'; //Set me.
$SMTP_Validator = new SMTP_validateEmail();
$SMTP_Validator->debug = true; //Probably dont want this when you're done testing
$results = $SMTP_Validator->validate(array($email), $sender);
return $results;
} 

My Script.


<?php
session_start();
$your_email ='christopher@thecreativesheep.ca';// <<=== update to your email address

$errors = '';
$name = '';
$visitor_email = '';
$user_message = '';
if(isset($_POST['name']))
{//Never check for submit button - IE has a but and doesn't always send it
	$name = $_POST['name'];
	$visitor_email = $_POST['email'];
	$user_message = $_POST['message'];
	///------------Do Validations-------------
	if(empty($name)||empty($visitor_email))
	{
		$errors .= "\
 Name and Email are required fields. ";
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\
 Bad email value!";
	}
	   if(!EmailExists($visitor_email))
	{
        $errors .= "\
 Email does not exist/server unfriendly!";
	}

	if(empty($_SESSION['6_letters_code'] ) ||
	strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
		//Note: the captcha code is compared case insensitively.
		//if you want case sensitive match, update the check above to
		// strcmp()
			$errors .= "\
 The captcha code does not match!";
	}
	if(empty($errors))
	{
		//send the email
		$to = $your_email;
		$subject="New form submission";
		$from = $your_email;
		$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
		$body = "A user  $name submitted the contact form:\
".
		"Name: $name\
".
		"Email: $visitor_email \
".
		"Message: \
 ".
		"$user_message\
".
		"IP: $ip\
";
		$headers = "From: $from \\r\
";
		$headers .= "Reply-To: $visitor_email \\r\
";
		
		mail($to, $subject, $body,$headers);

	}
}

// Function to validate against any email injection attempts

function IsInjected($str)
{
	$injections = array('(\
+)',
	'(\\r+)',
	'(\	+)',
	'(%0A+)',
	'(%0D+)',
	'(%08+)',
	'(%09+)'
	);
	$inject = join('|', $injections);
	$inject = "/$inject/i";
	if(preg_match($inject,$str))
	{
		return true;
	}
	else
	{
		return false;
	}
}

?>

It can go anywhere except inside another function, really…

Right before that comment line about the IsInjected function would be a good place. Or the end of the file. Either works perfectly well.

I apply the script, but the script is not validating the email address. The form is broken as well now, argh.

‘broken’. Please define.

I’m not getting an email, or could that be because the email address I entered wasn’t a valid address, let me try on a valid address see how the script preforms and update.

I’m not getting any email, from the form and there should be atleast a warning telling the user that their email address is not valid, as example sexy@bod.com if that address is valid the form would submit, if invalid the user would be informed it’s invalid right on the form.

did you set the line that i marked with “Set me”? Perhaps show me the code as it exists now.

Code as it currently exist.

<?php
session_start();
$your_email ='christopher@thecreativesheep.ca';// <<=== update to your email address

$errors = '';
$name = '';
$visitor_email = '';
$user_message = '';
if(isset($_POST['name']))
{//Never check for submit button - IE has a but and doesn't always send it
	$name = $_POST['name'];
	$visitor_email = $_POST['email'];
	$user_message = $_POST['message'];
	///------------Do Validations-------------
	if(empty($name)||empty($visitor_email))
	{
		$errors .= "\
 Name and Email are required fields. ";
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\
 Bad email value!";
	}
	   if(!EmailExists($visitor_email))
	{
        $errors .= "\
 Email does not exist/server unfriendly!";
	}

	if(empty($_SESSION['6_letters_code'] ) ||
	strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
		//Note: the captcha code is compared case insensitively.
		//if you want case sensitive match, update the check above to
		// strcmp()
			$errors .= "\
 The captcha code does not match!";
	}
	if(empty($errors))
	{
		//send the email
		$to = $your_email;
		$subject="New form submission";
		$from = $your_email;
		$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
		$body = "A user  $name submitted the contact form:\
".
		"Name: $name\
".
		"Email: $visitor_email \
".
		"Message: \
 ".
		"$user_message\
".
		"IP: $ip\
";
		$headers = "From: $from \\r\
";
		$headers .= "Reply-To: $visitor_email \\r\
";
		
		mail($to, $subject, $body,$headers);

	}
}
function EmailExists($email = '') {
require_once('smtp_validateEmail.class.php');
$sender = 'user@mydomain.com'; //Set me.
$SMTP_Validator = new SMTP_validateEmail();
$SMTP_Validator->debug = true; //Probably dont want this when you're done testing
$results = $SMTP_Validator->validate(array($email), $sender);
return $results;
}
// Function to validate against any email injection attempts

function IsInjected($str)
{
	$injections = array('(\
+)',
	'(\\r+)',
	'(\	+)',
	'(%0A+)',
	'(%0D+)',
	'(%08+)',
	'(%09+)'
	);
	$inject = join('|', $injections);
	$inject = "/$inject/i";
	if(preg_match($inject,$str))
	{
		return true;
	}
	else
	{
		return false;
	}
}

?>

I dont see a form on this page anywhere. So you’re… including this file somewhere else?

You didnt change the “Set me” line in the EmailExists function.

You should be seeing the SMTP debug messages… do you see any?

The email exists function should be set to;

if(!EmailExists($Setme))

You should be seeing the SMTP debug messages… do you see any?

Nope.

if(!EmailExists($visitor_email)) <– Should be this.

$sender = ‘user@mydomain.com’; //Set me. <---- should be set to something else. Your email address.

Are you seeing a blank page? If so stick this up at the top of the script right after session_start:


ini_set('display_errors', 1); 
error_reporting(E_ALL);