我想验证电子邮件域名,但我不想担心任何可能出现的子域名。验证电子邮件域名
例如:
@abc.com
@a.abc.com
@b.abc.com
...
这些都应该是有效的。
此外,我有一个域名列表进行验证,如abc.com,xyz.com ...如何从列表中验证电子邮件域,包括子域的最佳方式是什么?
谢谢。
我想验证电子邮件域名,但我不想担心任何可能出现的子域名。验证电子邮件域名
例如:
@abc.com
@a.abc.com
@b.abc.com
...
这些都应该是有效的。
此外,我有一个域名列表进行验证,如abc.com,xyz.com ...如何从列表中验证电子邮件域,包括子域的最佳方式是什么?
谢谢。
我决定改写这个更友好一点,这样您就不会受限于您的白名单类型的域名方案。
$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.
那么你应该使用一些像这样的代码:
<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
或本:
<?php
$email = "[email protected] 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 modify与this page。
希望它能帮助!
我后来写了这个函数。它可能会满足你要找的东西的要求。它做了两件事,验证电子邮件地址是一个有效的地址,然后验证该域名是否是针对它在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);
}
非常相似,这个帖子:PHP Check domain of email being registered is a 'school.edu' address
但是,你需要把它一步。一旦你有了这个分割,看看parse_url http://php.net/manual/en/function.parse-url.php并抓住主机部分。
您还可以使用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](http://tools.ietf.org/html/rfc5322)。 – PeeHaa 2013-04-24 18:19:17
我写的正则表达式的一个简单的例子与检查域列表的能力。
<?php
$email = '[email protected]';
$domains = array('abc.com', 'xyz.com');
$pattern = "/^[a-z0-9._%+-][email protected][a-z0-9.-]*(" . implode('|', $domains) . ")$/i";
if (preg_match($pattern, $email)) {
echo 'valid';
} else {
echo 'not valid';
}
?>
它接受带有额外字符的电子邮件,如ABCxyz.com,因为其中包含xyz.com。我需要它只接受名单上的电子邮件。 – user1893187 2013-02-26 20:48:33
试试这个 $ pattern =“/^[a-z0-9 ._%+ - ] + @([a-z0-9 .-] +。)*(”。implode('|',$ domains) 。“)$/i”; – 2013-02-26 20:55:22
这里只是一个问题。如果我在$ email上输入“ABCxyz.com”,它会接受它。 – user1893187 2013-02-26 20:47:38
好的,我可以解决这个问题。 – crush 2013-02-26 20:48:42
@ user1893187我编辑它就可以随心所欲。 – crush 2013-02-26 21:00:19