I'm evaluating a bunch of email validation techniques and someone of them output that email@domain is valid. I read somewhere that this email may be technically valid if it's being used for internal networking, but really for almost all practical purposes on the web today, this email should not evaluate to true.
What does your email va开发者_运维技巧lidation library or home-baked method do, and do you allow this sort of thing?
Well, it depends on what the application is supposed to do.
Is it going to be used in an intranet? If so, email@domain
may be fine.
If not, you might want to explicitly require a fqdn so that they can't send mail internally on your domain (foo@localhost
, etc).
It shouldn't be difficult to check the domain part:
$domain = array_pop(explode('@', $email));
Then, depending on your need, validate the domain.
You can check it for valid syntax (that it's a fqdn). There are plenty of tutorials online (And that a lot of frameworks provide) that can validate a domain in a string to see if it's a fqdn format...
Or, if your needs are greater, you can just verify that your server can resolve it (Via something like dns_get_record()
...
if (false === dns_get_record($domain, DNS_MX)) {
//invalid domain (can't find an MX record)...
}
(Note, I said you could do this, not if you should. That will depend on your exact use case)...
The domain .io currently has an MX resource record, so yes it is valid. It is explicitly allowed by RFC 5321.
You are welcome to use my free PHP function is_email()
to validate addresses. It's available to download here. Try validating http://isemail.info/jblue@io online for example.
It will ensure that an address is fully RFC 5321 compliant. It can optionally also check whether the domain actually exists and has an MX record.
You shouldn't rely on a validator to tell you whether a user's email address actually exists: some ISPs give out non-compliant addresses to their users, particularly in countries which don't use the Latin alphabet. More in my essay about email validation here: http://isemail.info/about.
See this article for a regex to match all valid email addresses:
You may want to tweak it to
- Discard IP domains
- Discard port numbers
And to answer your quetion about email@domain, you can discard that too, if you are not expecting intranet emails.
the check with false isn't the best way, the return value can be an empty array, e.g. $domain = '-onlinbe.de';
try empty() instead: http://us3.php.net/manual/en/function.empty.php
$dnsMx = dns_get_record($domain, DNS_MX); $dnsA = dns_get_record($domain, DNS_A);
if (empty($dnsMx) && empty($dnsA)) {
echo 'domain not available';
}
or use
if(gethostbyname($domain) === $domain) {
echo 'no ip found';
}
精彩评论