如何编写代码来检查电子邮件是否存在?例如 donkey_sdh123@gmail.com、donkey_sdh123@yahoo.com 或 donkey_sdh123@lycos.com 所有这些电子邮件都不存在于 gmail、yahoo 和 lycos 数据库中。
请参阅屏幕截图。donkey_sdh123@gmail.com 不好。这意味着它不存在。我怎样才能在我的项目中实现同样的东西?
欢迎使用 javascript、jquery shell 脚本、c 或 c++。没有.net。
如何编写代码来检查电子邮件是否存在?例如 donkey_sdh123@gmail.com、donkey_sdh123@yahoo.com 或 donkey_sdh123@lycos.com 所有这些电子邮件都不存在于 gmail、yahoo 和 lycos 数据库中。
请参阅屏幕截图。donkey_sdh123@gmail.com 不好。这意味着它不存在。我怎样才能在我的项目中实现同样的东西?
欢迎使用 javascript、jquery shell 脚本、c 或 c++。没有.net。
简而言之:这是不可能的。您最多可以尝试检查相关域是否有 MX 记录并尝试连接到其邮件服务器。即使这样也不能保证它处于工作状态。
您绝对无法以某种标准化的方式检查特定的电子邮件是否存在,因为许多服务器采用了许多伪装和别名的方法。服务器可以并且将在 SMTP 交换中报告不存在的地址,因为许多原因在VRFY
和MAIL
/中都是有效的RCPT
。你能得到的唯一确定的答案是,如果电子邮件被MAIL
/拒绝,它就是无效的RCPT
,但被接受并不能确定它是有效的,因为它可以在电子邮件处理过程中被拒绝。滥用MAIL
/RCPT
没有实际发送任何内容也可能导致您被阻止。
如果您想验证用户提供的电子邮件,最好的办法是在那里发送确认信。
如果您真的需要确认的工作电子邮件,您还应该查看。
可以通过 telnet 连接到远程 smtp 服务器:
http://www.yuki-onna.co.uk/email/smtp.html
唯一的问题是,许多邮件服务器不接受 smtp(例如 yahoo),并且由于有关地址是否对垃圾邮件发送者有价值的信息,因此很容易发现存在哪些电子邮件地址不符合任何电子邮件主机的利益。
您可以使用 ping 或其他网络扫描对域是否存在进行基本检查,并且您可以使用 Google API 搜索电子邮件地址,以防它在互联网上明确列出。但是你和所有的骗子都在做同样的事情,所以可能不值得尝试。
需要注意的另一件事是,许多电子邮件提供商允许用户提供派生地址、临时地址或别名地址,这些地址可以在有限的时间或使用范围内工作,但它们本身不是邮箱。在这种情况下,即使是有效的检查器也会认为该地址不存在,即使用户会收到一封发送给它的电子邮件。
由于过期地址可能不会被释放(以防止人们收到以前用户的邮件),因此会有误报的来源(检查说是但错误),以及假阴性(检查说没有但错误) ,以及被视为垃圾邮件发送者的所有摩擦。
看到这个代码我从网上得到的。
class SmtpValidator {
private $options = array(
"port" => 25,
"timeout" => 1, // Connection timeout to remote mail server.
"sender" => "info@webtrafficexchange.com",
"short_response" => false,
);
/**
* Override the options for those specified.
*/
function __construct($options = null) {
if (!empty($options)) {
if (is_array($options)) {
foreach ($options as $key => $value) {
$this->options[$key] = $value;
}
}
}
}
/**
* Validate the email address via SMTP.
* If 'shore_response' is true, the method will return true or false;
* Otherwise, the entire array of useful information will be provided.
*/
public function validate($email, $options = null) {
$result = array("valid" => false);
$errors = array();
// Email address (format) validation
if (empty($email)) {
$errors = array("Email address is required.\n");
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors = array("Invalid email address.\n");
} else {
list($username, $hostname) = split('@', $email);
if (function_exists('getmxrr')) {
if (getmxrr($hostname, $mxhosts, $mxweights)) {
$result['mx_records'] = array_combine($mxhosts, $mxweights);
asort($result['mx_records']);
} else {
$errors = "No MX record found.";
}
}
foreach ($mxhosts as $host) {
$fp = @fsockopen($host, $this->options['port'], $errno, $errstr,
$this->options['timeout']);
if ($fp) {
$data = fgets($fp);
$code = substr($data, 0, 3);
if($code == '220') {
$sender_domain = split('@', $this->options['sender']);
fwrite($fp, "HELO {$sender_domain}\r\n");
fread($fp, 4096);
fwrite($fp, "MAIL FROM: <{$this->options['sender']}>\r\n");
fgets($fp);
fwrite($fp, "RCPT TO:<{$email}>\r\n");
$data = fgets($fp);
$code = substr($data, 0, 3);
$result['response'] = array("code" => $code, "data" => $data);
fwrite($fp, "quit\r\n");
fclose($fp);
switch ($code) {
case "250": // We're good, so exit out of foreach loop
case "421": // Too many SMTP connections
case "450":
case "451": // Graylisted
case "452":
$result['valid'] = true;
break 2; // Assume 4xx return code is valid.
default:
$errors[] = "({$host}) RCPT TO: {$code}: {$data}\n";
}
} else {
$errors[] = "MTA Error: (Stream: {$data})\n";
}
} else {
$errors[] = "{$errno}: $errstr\n";
}
}
}
if (!empty($errors)) {
$result['errors'] = $errors;
}
return ($this->options['short_response']) ? $result['valid'] : $result;
}
}