我有这个功能来验证一个电子邮件地址:
function validateEMAIL($EMAIL) {
$v = "/[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/";
return (bool)preg_match($v, $EMAIL);
}
这样可以检查电子邮件地址是否有效吗?
我有这个功能来验证一个电子邮件地址:
function validateEMAIL($EMAIL) {
$v = "/[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/";
return (bool)preg_match($v, $EMAIL);
}
这样可以检查电子邮件地址是否有效吗?
当前回答
使用以下代码:
// Variable to check
$email = "john.doe@example.com";
// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate e-mail
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("Email is a valid email address");
}
其他回答
在关于电子邮件验证的“顶级问题”中回答了这个问题https://stackoverflow.com/a/41129750/1848217
For me the right way for checking emails is: Check that symbol @ exists, and before and after it there are some non-@ symbols: /^[^@]+@[^@]+$/ Try to send an email to this address with some "activation code". When the user "activated" his email address, we will see that all is right. Of course, you can show some warning or tooltip in front-end when user typed "strange" email to help him to avoid common mistakes, like no dot in domain part or spaces in name without quoting and so on. But you must accept the address "hello@world" if user really want it. Also, you must remember that email address standard was and can evolute, so you can't just type some "standard-valid" regexp once and for all times. And you must remember that some concrete internet servers can fail some details of common standard and in fact work with own "modified standard".
所以,只要检查@,提示用户在前端和发送验证电子邮件在给定的地址。
根据我的经验,regex解决方案有太多的假阳性,而filter_var()解决方案有假阴性(特别是对于所有新的tld)。
相反,最好是确保地址具有电子邮件地址的所有必需部分(用户、“@”符号和域),然后验证域本身是否存在。
无法确定(服务器端)外部域是否存在电子邮件用户。
这是我在Utility类中创建的一个方法:
public static function validateEmail(string $email): bool {
// SET INITIAL RETURN VARIABLE
// ENSURE -> EMAIL ISN'T EMPTY | AN @ SYMBOL IS PRESENT
$emailIsValid = FALSE;
if (
!empty($email) &&
strpos($email, '@') !== FALSE
) {
// GET EMAIL PARTS
$email = explode('@', $email);
$user = $email[0];
$domain = $email[1];
// VALIDATE EMAIL ADDRESS
if (
count($email) === 2 &&
!empty($user) &&
!empty($domain) &&
checkdnsrr($domain)
) {
$emailIsValid = TRUE;
}
}
// RETURN RESULT
return $emailIsValid;
}
这不仅可以验证你的邮件,还可以过滤掉意想不到的字符:
$email = $_POST['email'];
$emailB = filter_var($email, FILTER_SANITIZE_EMAIL);
if (filter_var($emailB, FILTER_VALIDATE_EMAIL) === false ||
$emailB != $email
) {
echo "This email adress isn't valid!";
exit(0);
}
看完这里的答案后,我得出了以下结论:
public static function isValidEmail(string $email) : bool
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
}
//Get host name from email and check if it is valid
$email_host = array_slice(explode("@", $email), -1)[0];
// Check if valid IP (v4 or v6). If it is we can't do a DNS lookup
if (!filter_var($email_host,FILTER_VALIDATE_IP, [
'flags' => FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE,
])) {
//Add a dot to the end of the host name to make a fully qualified domain name
// and get last array element because an escaped @ is allowed in the local part (RFC 5322)
// Then convert to ascii (http://us.php.net/manual/en/function.idn-to-ascii.php)
$email_host = idn_to_ascii($email_host.'.');
//Check for MX pointers in DNS (if there are no MX pointers the domain cannot receive emails)
if (!checkdnsrr($email_host, "MX")) {
return false;
}
}
return true;
}
如果你想检查电子邮件地址提供的域名是否有效,可以使用如下方法:
/*
* Check for valid MX record for given email domain
*/
if(!function_exists('check_email_domain')){
function check_email_domain($email) {
//Get host name from email and check if it is valid
$email_host = explode("@", $email);
//Add a dot to the end of the host name to make a fully qualified domain name and get last array element because an escaped @ is allowed in the local part (RFC 5322)
$host = end($email_host) . ".";
//Convert to ascii (http://us.php.net/manual/en/function.idn-to-ascii.php)
return checkdnsrr(idn_to_ascii($host), "MX"); //(bool)
}
}
这是一种方便的方式来过滤大量无效的电子邮件地址,以及标准的电子邮件验证,因为有效的电子邮件格式并不意味着有效的电子邮件。
请注意,idn_to_ascii()(或它的姐妹函数idn_to_utf8())函数在PHP安装中可能不可用,它需要扩展PECL intl >= 1.0.2和PECL idn >= 0.1。
还要记住,IPv4或IPv6作为电子邮件中的域部分(例如user@[IPv6:2001:db8::1])不能被验证,只有命名主机可以。
点击这里查看更多信息。