Create a gist now

Instantly share code, notes, and snippets.

@voku /checkEmail.php
Last active Jul 28, 2017

What would you like to do?
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