怎样用php验证电子邮件的地址是否真实有效?
怎样用php验证电子邮件的地址是否真实有效?比如:aaa@bbb.com这个信箱,怎么用php(如果用别的语言或者方法能证明也可)验证它是真实存在的信箱?
谢谢! 帮帮忙,很急的 有高手在吗?有没有解决的方法? if (!strstr($user_email, '@') || $user_email != stripslashes($user_email) || $user_email != htmlspecialchars($user_email)) {
echo("<SCRIPT LANGUAGE='JavaScript'>alert('你所输入的email格式不对,请重新检查!');history.go(-1);</SCRIPT>");exit;
} 只检查了@
用正则表达式!!
用正则表达式!!你应该会吧? 呵呵,应该?应该不会才对,那可不是那么好明白的东西,找些资料看吧.
那也只是查一下格式,至于能不能发通信,嘿嘿 javascript
[PHP]
function isEmail (theStr) {
var atIndex = theStr.indexOf ('@');
var dotIndex = theStr.indexOf ('.', atIndex);
var flag = true;
var theSub = theStr.substring (0, dotIndex + 1);
if ((atIndex < 1) || (atIndex != theStr.lastIndexOf ('@')) || (dotIndex < atIndex + 2) || (theStr.length <= theSub.length))
{
flag = false;
}
else { flag = true; }
return (flag);
}
[/PHP] <HTML>
<HEAD>
<TITLE>Listing 18.7</TITLE>
</HEAD>
<BODY>
<?
/*
** Function: verifyEmail
** Input: STRING address, REFERENCE error
** Output: BOOLEAN
** Description: Attempts to verify an email address by
** contacting a mail exchanger. Registered mail
** exchangers are requested from the domain controler first,
** then the exact domain itself. The error argument will
** contain relevant text if the address could not be
** verified.
*/
function verifyEmail($address, &$error)
{
global $SERVER_NAME;
list($user, $domain) = split("@", $address, 2);
//make sure the domain has a mail exchanger
if(checkdnsrr($domain, "MX"))
{
//get mail exchanger records
if(!getmxrr($domain, $mxhost, $mxweight))
{
$error = "Could not retrieve mail exchangers!<BR>\n";
return(FALSE);
}
}
else
{
//if no mail exchanger, maybe the host itself
//will accept mail
$mxhost[] = $domain;
$mxweight[] = 1;
}
//create sorted array of hosts
for($i = 0; $i < count($mxhost); $i++)
{
$weighted_host[($mxweight[$i])] = $mxhost[$i];
}
ksort($weighted_host);
//loop over each host
foreach($weighted_host as $host)
{
//connect to host on SMTP port
if(!($fp = fsockopen($host, 25)))
{
//couldn't connect to this host, but
//the next might work
continue;
}
/*
** skip over 220 messages
** give up if no response for 10 seconds
*/
set_socket_blocking($fp, FALSE);
$stopTime = time() + 10;
$gotResponse = FALSE;
while(TRUE)
{
//try to get a line from mail server
$line = fgets($fp, 1024);
if(substr($line, 0, 3) == "220")
{
//reset timer
$stopTime = time() + 10;
$gotResponse = TRUE;
}
elseif(($line == "") AND ($gotResponse))
{
break;
}
elseif(time() > $stopTime)
{
break;
}
}
if(!$gotResponse)
{
//this host was unresponsive, but
//maybe the next will be better
continue;
}
set_socket_blocking ($fp, TRUE);
//sign in
fputs($fp, "HELO $SERVER_NAME\r\n");
fgets($fp, 1024);
//set from
fputs($fp, "MAIL FROM: <info@$domain>\r\n");
fgets($fp, 1024);
//try address
fputs($fp, "RCPT TO: <$address>\r\n");
$line = fgets($fp, 1024);
//close connection
fputs($fp, "QUIT\r\n");
fclose($fp);
if(substr($line, 0, 3) != "250")
{
//mail server doesn't recognize
//this address, so it must be bad
$error = $line;
return(FALSE);
}
else
{
//address recognized
return(TRUE);
}
}
$error = "Unable to reach a mail exchanger!";
return(FALSE);
}
if(verifyEmail("leon@clearink.com", &$error))
{
print("Verified!<BR>\n");
}
else
{
print("Could not verify!<BR>\n");
print("Error: $error<BR>\n");
}
?>
</BODY>
</HTML> 谢谢cgxu,你的程序里面的确包括了怎样验证的方法,而不是简单判断格式。
但是:
getmxrr($domain, $mxhost, $mxweight)函数里面的$mxhost、$mxweight怎样得到?
另外,是不是只有验证了本域的用户?
谢谢!!
Re: 用正则表达式!!
[QUOTE][i]最初由 unclemoon 发布[/i][B]用正则表达式!!
你应该会吧? [/B][/QUOTE]
我不太明白用正则表达式怎么来解决邮件的有效问题,望unclemoon解说一下?
另外,大家注意,我说的不是验证邮件的格式正确与否,这个太easy了。
谢谢! !preg_match('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i', $_POST['mail']) 还是格式的检验吧~~~大哥:( 你是说发电子邮件确认吗? 就是说要证明比如:aaa@bbb.com这个信箱是存在的,不是证明格式 我正在做这个功能,一起讨论下
刚刚全部测试完用户注册,下午就要做这个功能了
[url]www.522000.net/eshop/register.php[/url] 这个很难吧。也可能说是不可能的,因为我所了解的很多发广告的网站要发送电子邮件,都在邮件中另了一些代码来验证,比如说当你点开邮件时就会向发送端返回信息。这样才可产知道这个邮件是否存在。所产说用php很难检出,
不过也可产这样试一下这个,用mail()发送一下,看能否发出,利用mail()返回值来判断邮件是否存在吧。
所以用户注册时发一确认信是很有必要的。 这个问题php不能解决吧!
这个你的发个e-mail测试一下!呵呵! 我的解决方法:
发送验证码到它的邮箱,通过验证码成为注册会员
页:
[1]