检查电子邮件是否存在 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14448649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if email exist php
提问by Neo Castelli
I have a question, i have a php script to check if the email address exist.
我有一个问题,我有一个 php 脚本来检查电子邮件地址是否存在。
But appear that yahoo, hotmail, aol and others providers are accepting any emails and not rejecting the invalid emails.
但似乎 yahoo、hotmail、aol 和其他提供商正在接受任何电子邮件,而不是拒绝无效电子邮件。
Only Gmail, and many domains like stackoverflow.com are rejecting the no vaild emails.
只有 Gmail 和许多域(如 stackoverflow.com)拒绝无效电子邮件。
Check my script and let me know if i can do some to check the yahoo and others.
检查我的脚本,如果我可以做一些检查雅虎和其他人,请告诉我。
html post form
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
<?php
/* Validate an email address. */
function jValidateEmailUsingSMTP($sToEmail, $sFromDomain = "gmail.com", $sFromEmail = "[email protected]", $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 protected]");
echo $bIsEmailValid ? "<b>Valid!</b>" : "Invalid! :(";
?>
回答by Pekka
If you need to make super sure that an E-Mail address exists, send an E-Mail to it. It should contain a link with a random ID. Only when that link is clicked, and contains the correct random ID, the user's account is activated (or ad published, or order sent, or whatever it is that you are doing).
如果您需要确保存在电子邮件地址,请向其发送电子邮件。它应该包含一个带有随机 ID 的链接。只有当该链接被点击并包含正确的随机 ID 时,用户的帐户才会被激活(或发布广告,或发送订单,或您正在执行的任何操作)。
This is the only reliable way to verify the existence of an E-Mail address, and to make sure that the person filling in the form has access to it.
这是验证电子邮件地址存在的唯一可靠方法,并确保填写表格的人可以访问它。
回答by Sverri M. Olsen
There is no 100% reliable way of checking the validity of an email address. There are a few things you can do to at least weed out obviously invalid addresses, though.
没有 100% 可靠的方法来检查电子邮件地址的有效性。不过,您可以采取一些措施来至少清除明显无效的地址。
The problems that arise with email addresses is actually very similar to those of snail mail. All three points below can also be used for sending snail mail (just change the DNS record with a physical address).
电子邮件地址出现的问题实际上与蜗牛邮件的问题非常相似。以下三点也可以用于发送蜗牛邮件(只需将DNS记录更改为物理地址即可)。
1. Check that the address is formatted correctly
1.检查地址格式是否正确
It is very difficult to check the format of email addresses, but PHP has a validation filterthat attempts to do it. The filter does not handle "comments and folding whitespace", but I doubt anyone will notice.
检查电子邮件地址的格式非常困难,但 PHP 有一个验证过滤器可以尝试这样做。过滤器不处理“评论和折叠空白”,但我怀疑有人会注意到。
if (filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE) {
echo 'Valid email address formatting!';
}
2. Check that the DNS record exists for the domain name
2.检查域名的DNS记录是否存在
If a DNS (Domain Name System) record exists then at least someone has set it up. It does not mean that there is an email server at the address, but if the address exists then it is more likely.
如果 DNS(域名系统)记录存在,那么至少有人已经设置了它。这并不意味着该地址有电子邮件服务器,但如果该地址存在,则更有可能。
$domain = substr($email, strpos($email, '@') + 1);
if (checkdnsrr($domain) !== FALSE) {
echo 'Domain is valid!';
}
3. Send a verification email to the address
3. 向地址发送验证邮件
This is the most effective way of seeing if someone is at the other end of the email address. If a confirmation email is not responded to in an orderly fashion -- 3 hours for example -- then there is probably some problem with the address.
这是查看电子邮件地址另一端是否有人的最有效方法。如果没有以有序的方式回复确认电子邮件(例如 3 小时),则地址可能存在问题。
回答by Eduardo Chavira
You can validate "used or real" emails with Telnet and MX records more info in here, for PHP exists a great library call php-smtp-email-validationthat simplify the process.
您可以使用 Telnet 和 MX 在这里记录更多信息来验证“使用过的或真实的”电子邮件,因为 PHP 存在一个很棒的库调用php-smtp-email-validation可以简化过程。
You create a boolean function with the file example1.php and call it when you'll validate the email text. For Gmail, Hotmail, Outlook, live and MSM I don's have any problems but with Yahoo and Terra the library can't validate correctly emails
您使用文件 example1.php 创建一个布尔函数,并在验证电子邮件文本时调用它。对于 Gmail、Hotmail、Outlook、live 和 MSM,我没有任何问题,但是对于 Yahoo 和 Terra,库无法正确验证电子邮件
回答by Sachin K
The problem is gmail uses port 25for their smtp outgoing mails, the other providers use different ports for their connection. Your response seems okay for gmail.com.
问题是 gmail 将端口25用于其 smtp 外发邮件,其他提供商使用不同的端口进行连接。你的回答似乎没问题gmail.com。
When you connect to gmail smtp it gives you response 250 2.1.5 OK j5si2542844pbs.271 - gsmtp
当您连接到 gmail smtp 时,它会为您提供 response 250 2.1.5 OK j5si2542844pbs.271 - gsmtp
But when you connect to any other smtp who does not use port 25 it gives you nullresponse.
但是当您连接到任何其他不使用端口 25 的 smtp 时,它会给您null响应。

