开发者

ping to check for real email addresses

开发者 https://www.devze.com 2023-02-19 11:53 出处:网络
I开发者_开发知识库s there anyway to ping an email address or something such like to check with it is a real working address. I\'m not talking about regex or php validate filters etc, but actually chec

I开发者_开发知识库s there anyway to ping an email address or something such like to check with it is a real working address. I'm not talking about regex or php validate filters etc, but actually checking the address exists??


It is possible, but not reliable to connect to the recipient mailserver and offer a mail, prompting the mailserver to reject or accept the mail. Not all mail servers will check the validity of adresses, so don't rely on it. Similar question here.


useful function for check hostname exist(90% worked!):

function validate_email($email)
{
    if(!preg_match ("/^[\w\.-]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]+$/", $email))
        return false;
    list($prefix, $domain) = explode("@",$email);
    if(function_exists("getmxrr") && getmxrr($domain, $mxhosts))
        return true;
    elseif (@fsockopen($domain, 25, $errno, $errstr, 5))
        return true;
    else
        return false;
}


You could check to see if there are MX records for the corresponding domain: getmxrr() http://php.net/manual/en/function.getmxrr.php

But i would suggest using a two part validation: - first a simple regex for plain validation of input - then a simple check to see if the tld is valid

// pattern was taken from PHP's own source
$pattern =  "/^((\\\"[^\\\"\\f\\n\\r\\t\\b]+\\\")|([A-Za-z0-9_][A-Za-z0-9_\\!\\#\\$\\%\\&\\'\\*\\+\\-\\~\\/\\^\\`\\|\\{\\}]*(\\.[A-Za-z0-9_\\!\\#\\$\\%\\&\\'\\*\\+\\-\\~\\/\\^\\`\\|\\{\\}]*)*))@((\\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9])(([A-Za-z0-9\\-])*([A-Za-z0-9]))?(\\.(?=[A-Za-z0-9\\-]))?)+[A-Za-z]+))$/D";
if (preg_match($pattern, $email)) {
    /**
     * allow ip address as domain OR it should be a valid TLD
     */
    $long = ip2long(substr($email, strrpos($email, '@')+1));
    return (($long !==FALSE && $long>-1)
        || isValidTld(substr($email, strrpos($email, '.')+1)));
}

This is still no garantee that it works but other than sending an email and catching possible bounces ... this is pretty much (aside from the mx-check) it...

0

精彩评论

暂无评论...
验证码 换一张
取 消