Forum Moderators: coopster
<?php
function ValidateMail($Email) {
global $HTTP_HOST;
$result = array(); if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {
$result[0]=false;
$result[1]="$Email is not properly formatted";
return $result;
}
list ( $Username, $Domain ) = split ("@",$Email);
if (getmxrr($Domain, $MXHost)) {
$ConnectAddress = $MXHost[0];
} else {
$ConnectAddress = $Domain;
} $Connect = fsockopen ( $ConnectAddress, 25 );
if ($Connect) {
if (ereg("^220", $Out = fgets($Connect, 1024))) {
fputs ($Connect, "HELO $HTTP_HOST\r\n");
$Out = fgets ( $Connect, 1024 );
fputs ($Connect, "MAIL FROM: <{$Email}>\r\n");
$From = fgets ( $Connect, 1024 );
fputs ($Connect, "RCPT TO: <{$Email}>\r\n");
$To = fgets ($Connect, 1024);
fputs ($Connect, "QUIT\r\n");
fclose($Connect);
if (!ereg ("^250", $From) ¦¦
!ereg ( "^250", $To )) {
$result[0]=false;
$result[1]="Server rejected address";
return $result;
} $result[0]=true;
$result[1]="$Email appears to be valid.";
return $result;
} // end of function
?>
} else {
$result[0] = false;
$result[1] = "No response from server";
return $result;
}
} else {
$result[0]=false;
$result[1]="Can not connect E-Mail server.";
return $result;
}
</form>
<p align="center"> </p>
</div>
<p> </p>
<p align="center"> </p>
</div>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) {
return FALSE;
}
list($Username, $Domain) = split("@",$email);
if(getmxrr($Domain, $MXHost)) {
return TRUE;
}
else {
if(fsockopen($Domain, 25, $errno, $errstr, 30)) {
return TRUE;
}
else {
return FALSE;
}
{
{
if(checkEmail($_POST['email']) == FALSE) {
echo "E-mail entered is not valid.";
} else {
echo "Success - please to to your email and click on the link to registrar!.";
}
As a general rule, it's better to be liberal in allowing too much through than conservative and not allow someone to enter what really IS a valid email address. In simple form validation you can't check if an address is Real, so you're really just trying to catch obvious typos or bad input like "I'm not telling"
I use:
^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)¦(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}¦[0-9]{1,3})(\]?)$
it allows domain addresses, but also understands IP addresses and multi-dot domains.
these are all vaild:
myname@mydomain.com
myname@mydomain.sub.ir.co.uk
myname@122.89.4.110
my.first.and.last.name@mydomain.site
Granted my pattern could be improved since it does allow some invalid patterns to get through, but it's good enough for most situations.
And since you're getting a true or false value back from the pattern matching, why not just return that from your function? IFs just clutter things up.
function isemail($var) {
return ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)¦(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}¦[0-9]{1,3})(\]?)$", $var);
}