验证电子邮件域

时间:2013-02-26 20:16:11

标签: php

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

例如:

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

这些都应该有效。

另外,我有一个要验证的域名列表,例如abc.com,xyz.com ......如何从列表中验证电子邮件域名,包括子域名?

感谢。

6 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

我前一段时间写了这个函数。它可能满足您所寻找的要求。它做了两件事,验证电子邮件地址是有效地址,然后验证域名是否是DNS中的有效名称。

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);
}

答案 2 :(得分: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.

答案 3 :(得分:1)

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

    <?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';
    }
    ?>

答案 4 :(得分:0)

你应该使用这样的代码:

<?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

希望它有所帮助!

答案 5 :(得分:0)

与此帖非常相似:PHP Check domain of email being registered is a 'school.edu' address

但是,您需要更进一步。完成拆分后,请查看parse_url http://php.net/manual/en/function.parse-url.php抓住主机部分。