【问题标题】:Validate E-mail Domains [duplicate]验证电子邮件域 [重复]
【发布时间】:2013-02-12 10:53:00
【问题描述】:

我想验证电子邮件域,但我不想担心可能出现的任何子域。

例如:

@abc.com
@a.abc.com
@b.abc.com
...

这些都应该是有效的。

另外,我有一个要验证的域列表,例如 abc.com、xyz.com...从列表中验证电子邮件域(包括子域)的最佳方法是什么?

谢谢。

【问题讨论】:

    标签: php


    【解决方案1】:

    我决定将其重写为更友好,这样您就不会受限于您列入白名单的域方案类型。

    $whitelist = array("abc.com", "xyz.com", "specific.subdomain.com", "really.specific.subdomain.com"); //You can add basically whatever you want here because it checks for one of these strings to be at the end of the $email string.
    $email = "@d.xyz.com";
    
    function validateEmailDomain($email, $domains) {
        foreach ($domains as $domain) {
            $pos = strpos($email, $domain, strlen($email) - strlen($domain));
    
            if ($pos === false)
                continue;
    
            if ($pos == 0 || $email[(int) $pos - 1] == "@" || $email[(int) $pos - 1] == ".")
                return true;
        }
    
        return false;
    }
    

    所以,你可以这样使用:

    if (validateEmailDomain($email, $whitelist))
        //Do something.
    

    【讨论】:

    • 这里只是有问题。如果我在 $email 上输入“ABCxyz.com”,它将接受它。
    • 好的,我可以解决这个问题。
    • @user1893187 我按照你的意愿编辑了它。
    • @crush 我尝试了这个解决方案,但是当域不在白名单中时,我收到了Offset not contained in string 警告。任何想法如何解决这个问题?
    【解决方案2】:

    您还可以使用 dns 验证域:

    function validEmail($email)
    {
        $allowedDomains = array('abc.com');
        list($user, $domain) = explode('@', $email);
        if (checkdnsrr($domain, 'MX') && in_array($domain, $allowedDomains))
        {
            return true;
        }
        return false;
    }
    

    【讨论】:

    • 你不能简单地在@上爆炸。因为本地部分允许使用转义的@。见RFC 5322
    【解决方案3】:

    我不久前写了这个函数。它可能满足您正在寻找的要求。它做了两件事,验证电子邮件地址是有效地址,然后根据 DNS 中的 MX 记录验证域名是否是有效名称。

    function validate_email($email) {
        // Check email syntax
        if(preg_match('/^([a-zA-Z0-9\._\+-]+)\@((\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,7}|[0-9]{1,3})(\]?))$/', $email, $matches)) {
            $user = $matches[1];
            $domain = $matches[2];
    
            // Check availability of DNS MX records
            if(getmxrr($domain, $mxhosts, $mxweight)) {
                for($i=0;$i<count($mxhosts);$i++){
                    $mxs[$mxhosts[$i]] = $mxweight[$i];
                }
    
                // Sort the records
                asort($mxs);
                $mailers = array_keys($mxs);
            } elseif(checkdnsrr($domain, 'A')) {
                $mailers[0] = gethostbyname($domain);
            } else {
                $mailers = array();
            }
            $total = count($mailers);
    
            // Added to still catch domains with no MX records
            if($total == 0 || !$total) {
                $error = "No MX record found for the domain.";
            }
        } else {
            $error = "Address syntax not correct.";
        }
    
        return ($error ? $error : TRUE);
    }
    

    【讨论】:

      【解决方案4】:

      我写了一个简单的正则表达式示例,能够检查域列表。

          <?php
      
          $email = 'shagtv@a.xyz.com';
      
          $domains = array('abc.com', 'xyz.com');
      
          $pattern = "/^[a-z0-9._%+-]+@[a-z0-9.-]*(" . implode('|', $domains) . ")$/i";
      
          if (preg_match($pattern, $email)) {
              echo 'valid';
          } else {
              echo 'not valid';
          }
          ?>
      

      【讨论】:

      • 它接受带有额外字符的电子邮件,例如 ABCxyz.com,因为其中包含 xyz.com。我需要它只接受列表中的电子邮件。
      • 试试这个 $pattern = "/^[a-z0-9._%+-]+@([a-z0-9.-]+.)*(" . implode(' |', $domains) . ")$/i";
      【解决方案5】:

      你应该使用这样的代码:

      <?php
      $subject = "abcdef";
      $pattern = '/^def/';
      preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
      print_r($matches);
      ?>
      

      或者这个:

      <?php
      $email = "someone@exa mple.com";
      
      if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        {
        echo "E-mail is not valid";
        }
      else
        {
            echo "E-mail is valid";
        }
      ?>
      

      this that you have to do a little modifythis page

      希望对你有帮助!

      【讨论】:

        【解决方案6】:

        和这个帖子很像:PHP Check domain of email being registered is a 'school.edu' address

        但是,您需要更进一步。一旦你有了那个拆分,看看 parse_url http://php.net/manual/en/function.parse-url.php 并获取 HOST 部分。

        【讨论】:

          猜你喜欢
          • 2011-07-16
          • 1970-01-01
          • 2011-06-21
          • 2013-09-25
          • 2012-05-20
          • 2013-01-18
          • 2011-04-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多