这是一个完全符合rfc2822和rfc2821的代码。只检查单个email地址。
function check_email_address($email) {
// first, we check that theres one @ symbol, and that the lengths are right
if (!ereg(“[^@]{1,64}@[^@]{1,255}”, $email)) {
// email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// split it into sections to make life easier
$email_array = explode(“@”, $email);
$local_array = explode(“.”, $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg(“^(([a-za-z0-9!#$%&*+/=?^_`{|}~-][a-za-z0-9!#$%&*+/=?^_`{|}~\.-]{0,63})|(\”[^(\\|\”)]{0,62}\”))$”, $local_array[$i])) {
return false;
}
}
if (!ereg(“^\[?[0-9\.]+\]?$”, $email_array[1])) { // check if domain is ip. if not, it should be valid domain name
$domain_array = explode(“.”, $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg(“^(([a-za-z0-9][a-za-z0-9-]{0,61}[a-za-z0-9])|([a-za-z0-9]+))$”, $domain_array[$i])) {
return false;
}
}
}
return true;
}
rfc2822:定义了email的格式
http://www.faqs.org/rfcs/rfc2822.html
rfc2821:定义了email中@前后的用户名和服务器名的最大长度,4.5.3.1节
http://www.faqs.org/rfcs/rfc2821.html
另外rfc2234定义了abnf和一些常用的terminal:
http://www.faqs.org/rfcs/rfc2234.html
网上大多数的邮件地址检查代码都没有按照这些rfc来写。
另外还可以加上检查邮件服务器是否存在的代码。这个在网上有的是。