Skip to content

Instantly share code, notes, and snippets.

@jalbertbowden
Forked from voku/checkEmail.php
Created March 24, 2019 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalbertbowden/1de3c699897879ae796aa5e379a7ee6b to your computer and use it in GitHub Desktop.
Save jalbertbowden/1de3c699897879ae796aa5e379a7ee6b to your computer and use it in GitHub Desktop.
check for E-Mail
<?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