在 PHP 中验证电子邮件地址是否有效有多容易?

发布于 2024-09-01 19:20:57 字数 210 浏览 7 评论 0原文

给定一个电子邮件地址,我如何验证它是否有效? (该电子邮件的域将接受该地址的电子邮件。)

这在 PHP 中看起来如何?

注意:我不想验证电子邮件地址本身在语法上是否有效。我想知道该域是否接受发送到该地址的电子邮件。

我本以为有某种方法可以用 MX 记录或其他东西来做到这一点......

Given an email address, how can I verify that it is valid? (That the email's domain will accept email for that address.)

How would this look in PHP?

Note: I don't want to verfify that the email address itself is syntactically valid. I want to know whether the domain will accept email to that address.

I would have thought there was some way to do this with an MX record or something...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

三寸金莲 2024-09-08 19:20:57

我认为您唯一的选择是 SMTP RCPT TO 或 VRFY 命令。

RCPT TO可能是一种检查方法,只要您在发出后断开连接即可。然而,如果帐户不存在,并非所有服务器都会启动您(uce 预防、捕获所有地址等)。

VRFY 可以告诉您该服务器上是否存在帐户,但这几乎总是被禁用以防止帐户探测。

执行 RCPT TO 验证(以及其他方法)的 PHP 类是: http: //code.google.com/p/php-smtp-email-validation/

I think your only options would be the SMTP RCPT TO or VRFY commands.

RCPT TO could be a way to check, as long as you disconnect after issuing it. However not all servers will boot you if the account doesn't exist(uce prevention, catch-all addresses, etc...).

VRFY can tell you if an account exists on that server, but this is almost always disabled to prevent account probes.

A PHP class that does RCPT TO verification(among other methods) is: http://code.google.com/p/php-smtp-email-validation/

很酷不放纵 2024-09-08 19:20:57

既然你特别说明

该电子邮件的域将接受该地址的电子邮件。

没有正则表达式可以帮助你。验证这一点的唯一方法是尝试向该地址发送电子邮件并查看它是否被退回。即使如此,您也可能会收到误报,因为邮件服务器可能不会发送失败通知。

使用 PHP 发送这样的基本电子邮件相当简单; 此处查看 mail() 函数的文档一个>。

Since you specifically state

That the email's domain will accept email for that address.

no regular expression is going to help you. The only way to verify that is to try to send an email to the address and see if it bounces. Even so, you could get false positives, since the mail server may not send out failure notifications.

Sending a basic email like this with PHP is reasonably simple; check out the documentation for the mail() function here.

狼性发作 2024-09-08 19:20:57

您实际上无法检查服务器是否会接受它。邮件服务器没有 API 来处理这个问题。

曾经有一个脚本尝试连接到 MX 服务器,并从服务器查找某种表明它需要密码的响应,而不是仅仅将其作为未使用的邮箱拒绝。然而,这是非常糟糕的做法。

您唯一能做的就是检查有效的电子邮件地址,并希望得到最好的结果:

http ://www.linuxjournal.com/article/9585

这是真正遵循 RFC 中标准的教程之一。

/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email 
address format and the domain exists.
*/
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || 
 ↪checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

为了确保电子邮件能够送达,验证几乎是您能得到的最好的结果。

You can't actually check if the server will accept it. Mail server's don't have an API to handle that.

There used to be a script that attempted to connect to the MX server, and looked for some sort of response from the server that indicated that it wanted a password, instead of just rejecting it as a not-in-use mailbox. This however is very bad practice.

The only thing you can pretty much do is to check for a valid email address, and hope for the best:

http://www.linuxjournal.com/article/9585

That is one of the tutorials that actually follow the standards in the RFC.

/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email 
address format and the domain exists.
*/
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || 
 ↪checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

In the way of making sure that an email is able to be delivered, validations is about as good as you are going to get.

余罪 2024-09-08 19:20:57

验证电子邮件地址是否有效有多容易?

一点也不容易。看看Dominic Sayers 的电子邮件验证在 PHP 中,可能是任何语言中最完整的电子邮件验证库。它还包括 DNS 检查。

另请参阅 Jeff Atwood 的这篇相关文章介绍了确保电子邮件到达所需的检查。

How easy is it to verify that an email address is valid?

Not easy at all. Take a look at Dominic Sayers' email validation in PHP, possibly the most complete email validation library in any language. It also includes DNS checking.

Also see this related article from Jeff Atwood about the checks required to guarantee email arrival.

简单 2024-09-08 19:20:57

电子邮件(在语法上)有效与接受电子邮件不同。为了检查该域是否接受电子邮件,您必须尝试直接向 MX 发送电子邮件,但即使这样,如果 SMTP 服务器接受了该电子邮件,也不能保证此后它不会被拒绝,即,将发送未送达报告。如果您位于动态地址池中或没有 DNS 反向,则 SMTP 服务器可能会拒绝您的电子邮件

An e-mail being (syntactically) valid is not the same as it accepting e-mails. In order to check whether the domain will accept e-mail you have to try sending an e-mail directly to the MX, but even then, if it is accept by the SMTP server, you are not guaranteed it will not be rejected afterwards, i.e. a non delivery report will be sent. And the SMTP server will probably reject your e-mail if you are on a dynamic pool of addresses or have no DNS reverse

遇见了你 2024-09-08 19:20:57

只是想我会提到 PHP 有一个内置函数 getmxrr () 从域检索 MX 记录。

这有什么用,我不知道......在页面上它指出:

此函数不应用于地址验证目的。

因此它的用途仅限于验证域是否有邮件服务器。

Just thought I'd mention that PHP has a built-in function getmxrr() that retrieves the MX record from a domain.

How this is useful, I don't know... On the page it states:

This function should not be used for the purposes of address verification.

So its use is limited to verifying that a domain even has a mailserver.

秋风の叶未落 2024-09-08 19:20:57

如果您使用 PHP 5.2.x,现在不需要使用自定义验证函数。 PHP 带有一个内置函数

If you are using PHP 5.2.x, now there is not need to use custom validation functions. PHP comes with a built in function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文