Skip to content

Instantly share code, notes, and snippets.

@duhow
Created February 22, 2017 12:02
Show Gist options
  • Save duhow/ca3c64e3ba1167cd1f986a145180bd35 to your computer and use it in GitHub Desktop.
Save duhow/ca3c64e3ba1167cd1f986a145180bd35 to your computer and use it in GitHub Desktop.
PHP JSON - Check and validate emails
<?php
function http_json($status, $data = NULL, $httpcode = 200){
http_response_code($httpcode);
$out = json_encode(array('status' => $status, 'data' => $data));
die($out);
}
function fix_hostname($hostname){
$search = [
'hotmail.com' => [
"hitmail.com","hoail.com","homai,com","homail.com",
"hootmail.com","hormail.com","hot.mail.com","hotamil.com",
"hotlail.com","hotmai.com","hotmail..com","hotmailm.com",
"hotmaiul.com","hotmal..com","hotmal.com",
"hotmil.com","hotnail.com","htomail.com","hit.comail.com",
"hotail.com","hotamail.com","hotmaik.com","hotmail.co",
"hotmail.cpm","hotmail.vom","hotmail.vpm","hotmaul.com",
"hotmeil.com","hotmsil.com","hotmial.com","hoymail.com",
"hotmail.com.ar","hotmail.com.mx","hotmail.com.pe","hotmail.com.py",
"hotamail.com.ar","otmail.com","hotsmail.com"
],
'hotmail.es' => ["hotmail.ea","hotmaul.es"],
'gmail.com' => [
"gimail.com", "gmai.com", "gmail­com", "gmail..com",
"gmail.c.com", "gmail.caa", "gmail.com", "gmail.comcom",
"gmail.con", "gmailcom", "gmali.com", "gmil.com", "g.mail.com",
"gamail.com", "gemail.com", "gilmail.com", "gmail.cm", "gmail.co",
"gmail.com.co", "gmail.com.py"
],
'yahoo.com' => ["yagoo.com", "yhoo.com", "yhoo.con","hayoo.com"],
'outlook.com' => [
"outk.com","outloock.com","outhlook.com","outkook.com",
"outlock.com","outlok.com","oulook.com","uotlook.com"
],
'outlook.es' => ["outlook.ds","outtlook.es"],
];
foreach($search as $domain => $values){
foreach($values as $pos){
if($hostname == $pos){
return $domain;
}
}
}
if(in_array(substr($hostname, -4), ['.con', '.cvm', '.cpm', '.vom', '.von', '.vpm'])){
return substr($hostname, 0, -4) .".com";
}
return FALSE;
}
function blacklist($hostname){
$list = [
'a.com', 'aaa.aaa', 'bbb.bbb', 'correo.com', 'ejemplo.com', 'example.com'
];
return in_array($hostname, $list);
}
function check_dns($hostname){
$hosts = array();
return getmxrr($hostname, $hosts);
}
// ------------------
header("Content-Type: application/json");
// GET PATH (from Rewrite URL) /script.php/email@example.com
$out = str_replace($_SERVER['SCRIPT_NAME'], "", $_SERVER['PHP_SELF']);
if(strlen($out) > 4 && $out[0] == "/"){ $out = substr($out, 1); }
if(isset($_REQUEST['email'])){
$email = $_REQUEST['email'];
}elseif(strlen($out) > 4){
$email = $out;
}
if(empty($email)){
http_json("ERROR", "EMPTY", 400);
}
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){
$tmp = base64_decode($email); // Intentar decodificar BASE64
if(filter_var($tmp, FILTER_VALIDATE_EMAIL) === FALSE){
http_json("ERROR", "NOT_VALID", 404);
}
$email = $tmp;
unset($tmp);
}
$email = strtolower($email); // HACK
$h = substr($email, strpos($email, "@") + 1);
if(blacklist($h)){
http_json("ERROR", "BLACKLIST", 401);
}
$check = fix_hostname($h);
if($check !== FALSE){
$email = str_replace($h, $check, $email);
http_json("SUGGEST", $email, 300);
}
if(check_dns($h) === FALSE){
http_json("ERROR", "DNS_ERROR", 404);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment