Deprecated: Function set_magic_quotes_runtime() is deprecated in /nfs/c01/h08/mnt/2939/domains/cidamon.com/html/blog/textpattern/lib/txplib_db.php on line 14
Cidamon: Blog: Email validation function with DNS check
Email validation function with DNS check
08 June, 2008

This is a simple email address validation function. It can be used as a spam guard in the contact forms. It takes the email address and checks for DNS record of the domain name. So, from an email address like: sample@domain.com, it looks for DNS records against sample.com. If there is a valid DNS record against that domain name then the script accepts it as a valid email address otherwise it rejects the email address. The first function we have here will check for the validity and return true or false. Depending on the value you can choose take any action you like.

function checkEmail($email) {
        if(preg_match('/^\w[-.\w]*@(\w[-._\w]*\.[a-zA-Z]{2,}.*)$/', $email, $matches))
        {
            if(function_exists('checkdnsrr'))
            {
                if(checkdnsrr($matches[1] . '.', 'MX')) return true;
                if(checkdnsrr($matches[1] . '.', 'A')) return true;
            }else{
                if(!empty($hostName))
                {
                    if( $recType == '' ) $recType = "MX";
                    exec("nslookup -type=$recType $hostName", $result);
                    foreach ($result as $line)
                    {
                        if(eregi("^$hostName",$line))
                        {
                            return true;
                        }
                    }
                    return false;
                }
                return false;
            }
        }
        return false;
    }

Now call the function from here:

if(!checkEmail($email)) { 
	 	echo "Invalid email address..!";
	}
	else{
	 	echo "Valid email address..!";
	}

We hope this helps!

---

Comment