Skip to content

Instantly share code, notes, and snippets.

@axeloz
Last active June 25, 2022 14:09
Show Gist options
  • Save axeloz/15b51bedca0e9c8c75070dda477507ea to your computer and use it in GitHub Desktop.
Save axeloz/15b51bedca0e9c8c75070dda477507ea to your computer and use it in GitHub Desktop.
Check whether email address is temporary (disposable) using MX check (best option)
<?php
/**
* This function checks whether the email address
* is a temporary email address
* @param string $email The email address to check
* @return boolean
*/
function isTemporaryAddress(String $email):bool {
// TODO: that should maybe stored elsewhere
$blacklist = [
'yopmail.com',
'87.98.164.155',
'yopmail.fr',
'yopmail.net',
'jetable.fr.nf',
'nospam.ze.tc',
'nomail.xl.cx',
'mega.zik.dj',
'speed.1s.fr',
'cool.fr.nf',
'courriel.fr.nf',
'moncourrier.fr.nf',
'monemail.fr.nf',
'monmail.fr.nf',
'tempomail.fr',
// TODO: this list needs to be completed
];
// Getting the email's hostname
$domain = substr(strrchr($email, '@'), 1);
// If the MX lookup went fine
if (getmxrr($domain, $mxs)) {
// Trying to find a banned hostname among all MX servers
$filter = array_filter($mxs, function($m) use ($blacklist) {
// Comparing each MX hostname Vs the blacklist
foreach ($blacklist as $b) {
// This hostname is banned
if (strpos($m, $b) !== false) {
return $m;
}
}
});
// We found at least one banned hostname
if (! empty($filter) && is_array($filter) && count($filter) > 0) {
return true;
}
}
// Assuming everything went fine
return false;
}
// Email address to check
$email = 'test@jetable.fr.nf';
var_dump(isTemporaryAddress($email));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment