<?php
/**
* function getmxrr
* 获取指定
域名的MX记录信息
* edit www.jb200.com
*/
function win_getmxrr($
hostname, &$mxhosts, &$mxweight=false)
{
if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') return;
if (!is_array ($mxhosts) ) $mxhosts = array();
if (empty($hostname)) return;
$exec='nslookup -type=MX '.escape
shellarg($hostname);
@exec($exec, $output);
if (empty($output)) return;
$i=-1;
foreach ($output as $line) {
$i++;
if (preg_match("/^$hostnametMX preference = ([0-9]+), mail exchanger = (.+)$/i", $line, $parts)) {
$mxweight[$i] = trim($parts[1]);
$mxhosts[$i] = trim($parts[2]);
}
if (preg_match('/responsible mail addr = (.+)$/i', $line, $parts)) {
$mxweight[$i] = $i;
$mxhosts[$i] = trim($parts[1]);
}
}
return ($i!=-1);
}
if (!function_exists('getmxrr')) {
function getmxrr($hostname, &$mxhosts, &$mxweight=false) {
return win_getmxrr($hostname, $mxhosts, $mxweight);
}
}
/**
* class EmailLinker
* 邮箱验证类,正则匹配邮箱地址并对域名是否有MX解析进行验证
*/
class EmailLinker
{
CONST CONN_TIMEOUT = 10;
CONST READ_TIMEOUT = 5;
CONST SMTP_PORT = 25;
private $email;
function __construct($email)
{
$this->email = $email;
$this->redirectIfNeeded();
}
//
重定向邮箱地址
function redirectIfNeeded()
{
if(array_key_exists('mail',$_GET) && $this->email == base64_decode($_GET['mail']) ){
header('Location: mailto:'.$this->email);
exit;
}
}
//回调返回邮箱地址
public function link()
{
if($this->isValid()){
$encoded = base64_encode($this->email);
return '<a href="?mail='.urlencode($encoded).'" target="_blank">Email:'.$this->email.'</a>';
}
}
//获取邮箱地址并验证
public function isValid($mail = '')
{
static $valid = null;
if($mail){
return ( $this->getParts() != null );
}
if($valid !== null){
return $valid;
}
if($parts = $this->getParts()){
$valid = $this->validateUser($parts['host'],$parts['user']);
}
return $valid;
}
private function validateUser($hostname,$user)
{
if($sock = $this->openSMTPSocket($hostname)){
$this->smtpSend($sock,"HELO $hostname");
$resp = $this->smtpSend($sock,"MALL FROM: <$user@$hostname>");
//$resp = $this->smtpSend($sock,"RCPT TO:<$user@$hostname>"); //这一步可能被ISP过滤了,导致邮箱验证不成功
$vaild = (preg_match('/250|451|452s/',$resp) == 1);
fclose($sock);
return $vaild;
}else{
return false;
}
}
private function openSMTPSocket($hostname)
{
$hosts = $this->getMX($hostname);
foreach($hosts as $host => $weight)
{
if($sock = @fsockopen($host,self::SMTP_PORT,$errno,$errstr,self::CONN_TIMEOUT))
{
stream_set_timeout($sock,self::READ_TIMEOUT);
return $sock;
}
}
}
private function getMX($hostname)
{
$host = array();
$weights = array();
getmxrr($hostname,$hosts,$weights);
$results = array();
foreach($hosts as $i => $host)
{
$results[$host] = $weights[$i];
}
arsort($results,SORT_NUMERIC);
$results[$hostname] = 0;
return $results;
}
private function smtpSend($sock,$data)
{
fwrite($sock,"$datarn");
return fgets($sock,1024);
}
//正则匹配
private function getParts()
{
//$emailRegex = '/w+([-+.]w+)*@w+([-.]w)*.w+([-.]w+)*/x';
$emailRegex = <<<__REGEX__
/(?P<user>
w+([-+.]w+)*
)@(?P<host>
w+([-.]w)*.w+([-.]w+)*
)/x
__REGEX__;
return (preg_match($emailRegex,$this->email,$matches))? $matches : null ;
}
}
$emailLinker_class = new EmailLinker('790896688@qq.com');
echo $emailLinker_class->link();
?>