Last active
March 24, 2019 01:37
-
-
Save voku/b88f0538d0dbcb2fc189 to your computer and use it in GitHub Desktop.
check for E-Mail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* checkEmail | |
* | |
* @param String $email | |
* @param Boolean $mxCheck (do not use, if you don't need it) | |
* | |
* @return Boolean | |
*/ | |
function checkEmail($email, $mxCheck = false) | |
{ | |
if (!is_string($email) || strlen($email) >= 320) { | |
$valid = false; | |
} elseif (!preg_match('/^(.*<?)(.*)@(.*)(>?)$/', $email, $matches)) { | |
$valid = false; | |
} else { | |
$domain = $matches[3]; | |
if (function_exists('idn_to_ascii')) { | |
$email = $matches[1] . $matches[2] . '@' . idn_to_ascii($domain) . $matches[4]; | |
} else { | |
$email = $matches[1] . $matches[2] . '@' . $domain . $matches[4]; | |
} | |
if (!$domain || !$email) { | |
$valid = false; | |
} else { | |
if (function_exists('filter_var') && function_exists('idn_to_ascii')) { | |
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
$valid = false; | |
} else { | |
$valid = true; | |
} | |
} else { | |
if (function_exists('idn_to_ascii')) { | |
$regEx = "/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i"; | |
} else { | |
$regEx = "/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([öäüa-z0-9]{1}[öäüa-z0-9\-]{0,62}[öäüa-z0-9]{1})|[öäüa-z])\.)+[öäüa-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i"; | |
} | |
if (!preg_match($regEx, $email)) { | |
$valid = false; | |
} else { | |
$valid = true; | |
} | |
} | |
if ($valid && $mxCheck && function_exists('checkdnsrr')) { | |
$valid = checkdnsrr($domain . '.', 'MX') || checkdnsrr($domain, 'A'); | |
} | |
} | |
} | |
return $valid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment