Soldato
i've edited this slightly, but in general this was made by chatGTP.
I wanted a php function that checked not just if the format of an email address was valid, but check for any signs the email was fake, such as too short, fake extensions etc.
I thought i'd share it to see what others thought, along with also if others could see room for improvement.
Ignore any place it echos. this is just for testing so I can see how the emails are getting their rating.
I wanted a php function that checked not just if the format of an email address was valid, but check for any signs the email was fake, such as too short, fake extensions etc.
I thought i'd share it to see what others thought, along with also if others could see room for improvement.
Ignore any place it echos. this is just for testing so I can see how the emails are getting their rating.
PHP:
function checkEmailValidity($email) {
$validityRating = 10;
$isShortUsername = 0;
$isFreeDomain = 0;
$isUnknownDomain = 0;
$isShortDomain = 0;
$isSuperShortUsername = 0;
$isNonExtension = 0;
// Remove whitespace and sanitize the email address
$email = filter_var(trim($email), FILTER_SANITIZE_EMAIL);
// Validate the email address
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return $validityRating; // Invalid email address
}
// Get the username, domain and extension from the email address
list($username, $fullurl) = explode('@', $email);
list($domain, $extension) = explode('.', $fullurl);
// Popular free email domains list
$popularFreeEmailDomains = [
'gmail',
'yahoo',
'ymail',
'hotmail',
'gmx',
'googlemail',
'btinternet',
'live',
'outlook',
'mail',
'protonmail',
'mac',
'me',
'yandex',
'aol',
'icloud',
'fastmail',
'qq',
'rocketmail',
'zoho',
'inbox',
'ntlworld',
'btopenworld',
'sky',
];
// Popular extensions list
$popularExtensions = [
'com',
'co',
'uk',
'net',
'it',
'org',
'info',
'me',
'io',
'gov',
'edu',
'biz',
'dk',
];
// Check if the domain is from a popular free email service
if (in_array($domain, $popularFreeEmailDomains)) {
$isFreeDomain = 1;
} else {
$validityRating -= 3;
$isUnknownDomain = 1;
echo "<br />unknown domain $validityRating";
}
// Check if the extension is not a common one
if (!in_array($extension, $popularExtensions)) {
$validityRating -= 2;
$isNonExtension = 1;
echo "<br />extension is not common one $validityRating";
}
// Check if the username length is shorter than 4 characters
if (strlen($username) < 4) {
$validityRating -= 1;
$isShortUsername = 1;
echo "<br />short username $validityRating";
}
// Check if the username length is super short, less than 3 characters
if (strlen($username) < 3) {
$validityRating -= 3;
$isSuperShortUsername = 1;
echo "<br />super short username $validityRating";
}
// Check if the domain length is shorter than 4 characters
if (strlen($domain) < 5 && $isFreeDomain != 1) {
$validityRating -= 3;
$isShortDomain = 1;
echo "<br />domain short and not free domain $validityRating";
}
// Check if the username is short and free domain
if ($isShortUsername == 1 && $isFreeDomain == 1) {
$validityRating -= 5;
echo "<br />domain short and is free domain $validityRating";
}
// Check if the username is super short and free domain
if ($isSuperShortUsername == 1 && $isFreeDomain == 1) {
$validityRating -= 9;
echo "<br />username supershort and is free domain $validityRating";
}
// Check if the username is short and not free domain
if ($isSuperShortUsername == 1 && $isFreeDomain != 1) {
$validityRating -= 3;
echo "<br />username super short and not free domain $validityRating";
}
// Check if the username is only numbers
if (ctype_digit($username)) {
$validityRating -= 5;
echo "<br />username is just numbers $validityRating";
}
return max(0, $validityRating);
}