【发布时间】:2013-01-05 02:21:58
【问题描述】:
我有一个问题,我有一个 php 脚本来检查电子邮件地址是否存在。
但似乎 yahoo、hotmail、aol 和其他提供商正在接受任何电子邮件,而不是拒绝无效电子邮件。
只有 Gmail 和许多像 stackoverflow.com 这样的域都拒绝无效的电子邮件。
检查我的脚本,如果我可以做一些检查雅虎和其他人,请告诉我。
html 发布表单
<html>
<body>
<form action="checkemail.php" method="POST">
<b>E-mail</b> <input type="text" name="email">
<input type="submit">
</form>
</body>
</html>
php
<?php
/* Validate an email address. */
function jValidateEmailUsingSMTP($sToEmail, $sFromDomain = "gmail.com", $sFromEmail = "email@gmail.com", $bIsDebug = false) {
$bIsValid = true; // assume the address is valid by default..
$aEmailParts = explode("@", $sToEmail); // extract the user/domain..
getmxrr($aEmailParts[1], $aMatches); // get the mx records..
if (sizeof($aMatches) == 0) {
return false; // no mx records..
}
foreach ($aMatches as $oValue) {
if ($bIsValid && !isset($sResponseCode)) {
// open the connection..
$oConnection = @fsockopen($oValue, 25, $errno, $errstr, 30);
$oResponse = @fgets($oConnection);
if (!$oConnection) {
$aConnectionLog['Connection'] = "ERROR";
$aConnectionLog['ConnectionResponse'] = $errstr;
$bIsValid = false; // unable to connect..
} else {
$aConnectionLog['Connection'] = "SUCCESS";
$aConnectionLog['ConnectionResponse'] = $errstr;
$bIsValid = true; // so far so good..
}
if (!$bIsValid) {
if ($bIsDebug) print_r($aConnectionLog);
return false;
}
// say hello to the server..
fputs($oConnection, "HELO $sFromDomain\r\n");
$oResponse = fgets($oConnection);
$aConnectionLog['HELO'] = $oResponse;
// send the email from..
fputs($oConnection, "MAIL FROM: <$sFromEmail>\r\n");
$oResponse = fgets($oConnection);
$aConnectionLog['MailFromResponse'] = $oResponse;
// send the email to..
fputs($oConnection, "RCPT TO: <$sToEmail>\r\n");
$oResponse = fgets($oConnection);
$aConnectionLog['MailToResponse'] = $oResponse;
// get the response code..
$sResponseCode = substr($aConnectionLog['MailToResponse'], 0, 3);
$sBaseResponseCode = substr($sResponseCode, 0, 1);
// say goodbye..
fputs($oConnection,"QUIT\r\n");
$oResponse = fgets($oConnection);
// get the quit code and response..
$aConnectionLog['QuitResponse'] = $oResponse;
$aConnectionLog['QuitCode'] = substr($oResponse, 0, 3);
if ($sBaseResponseCode == "5") {
$bIsValid = false; // the address is not valid..
}
// close the connection..
@fclose($oConnection);
}
}
if ($bIsDebug) {
print_r($aConnectionLog); // output debug info..
}
return $bIsValid;
}
$email = $_POST['email'];
$bIsEmailValid = jValidateEmailUsingSMTP("$email", "gmail.com", "email@gmail.com");
echo $bIsEmailValid ? "<b>Valid!</b>" : "Invalid! :(";
?>
【问题讨论】:
-
这不是验证电子邮件地址的非常可靠的方法。许多服务器不会正确返回指示电子邮件是否存在以防止垃圾邮件收集的代码。
-
我认为许多提供商不会让您测试这样的电子邮件地址(因为有人可以使用相同的技术来挖掘有效电子邮件中的垃圾邮件)。验证电子邮件地址的唯一真正方法是发送一封带有唯一令牌的电子邮件,并具有允许用户将令牌输入您的应用程序以确认他们收到电子邮件的机制。
-
这永远不会以任何程度的可靠性工作
-
典型的流程是向该地址发送一封验证电子邮件,并且必须用户通过点击电子邮件中的链接或输入一些代码来执行验证;回复电子邮件是另一种选择。
-
你不能。故事结束:(