Skip to content

Instantly share code, notes, and snippets.

@neo22s
Created July 13, 2014 08:37
Show Gist options
  • Save neo22s/502dd133fa809fc6a8fd to your computer and use it in GitHub Desktop.
Save neo22s/502dd133fa809fc6a8fd to your computer and use it in GitHub Desktop.
<?php
/*
EmailAddressValidator Class
http://code.google.com/p/php-email-address-validation/
Released under New BSD license
http://www.opensource.org/licenses/bsd-license.php
Sample Code
----------------
$validator = new EmailAddressValidator;
if ($validator->check_email_address('[email protected]')) {
// Email address is technically valid
}
*/
class EmailAddressValidator {
/**
* Check email address validity
* @param strEmailAddress Email address to be checked
* @return True if email is valid, false if not
*/
public function check_email_address($strEmailAddress) {
// If magic quotes is "on", email addresses with quote marks will
// fail validation because of added escape characters. Uncommenting
// the next three lines will allow for this issue.
//if (get_magic_quotes_gpc()) {
// $strEmailAddress = stripslashes($strEmailAddress);
//}
// Control characters are not allowed
if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $strEmailAddress)) {
return false;
}
// Check email length - min 3 (a@a), max 256
if (!$this->check_text_length($strEmailAddress, 3, 256)) {
return false;
}
// Split it into sections using last instance of "@"
$intAtSymbol = strrpos($strEmailAddress, '@');
if ($intAtSymbol === false) {
// No "@" symbol in email.
return false;
}
$arrEmailAddress[0] = substr($strEmailAddress, 0, $intAtSymbol);
$arrEmailAddress[1] = substr($strEmailAddress, $intAtSymbol + 1);
// Count the "@" symbols. Only one is allowed, except where
// contained in quote marks in the local part. Quickest way to
// check this is to remove anything in quotes. We also remove
// characters escaped with backslash, and the backslash
// character.
$arrTempAddress[0] = preg_replace('/\./'
,''
,$arrEmailAddress[0]);
$arrTempAddress[0] = preg_replace('/"[^"]+"/'
,''
,$arrTempAddress[0]);
$arrTempAddress[1] = $arrEmailAddress[1];
$strTempAddress = $arrTempAddress[0] . $arrTempAddress[1];
// Then check - should be no "@" symbols.
if (strrpos($strTempAddress, '@') !== false) {
// "@" symbol found
return false;
}
// Check local portion
if (!$this->check_local_portion($arrEmailAddress[0])) {
return false;
}
// Check domain portion
if (!$this->check_domain_portion($arrEmailAddress[1])) {
return false;
}
//modification Chema 13/07/2014
// Check if the email domain has a valid MX record
if (! (bool) checkdnsrr($arrEmailAddress[1], 'MX'))
return false;
//do not allow banned domains
if (in_array($arrEmailAddress[1],$this->get_banned_domains()))
return false;
//end modification
// If we're still here, all checks above passed. Email is valid.
return true;
}
/**
* Checks email section before "@" symbol for validity
* @param strLocalPortion Text to be checked
* @return True if local portion is valid, false if not
*/
protected function check_local_portion($strLocalPortion) {
// Local portion can only be from 1 to 64 characters, inclusive.
// Please note that servers are encouraged to accept longer local
// parts than 64 characters.
if (!$this->check_text_length($strLocalPortion, 1, 64)) {
return false;
}
// Local portion must be:
// 1) a dot-atom (strings separated by periods)
// 2) a quoted string
// 3) an obsolete format string (combination of the above)
$arrLocalPortion = explode('.', $strLocalPortion);
for ($i = 0, $max = sizeof($arrLocalPortion); $i < $max; $i++) {
if (!preg_match('.^('
. '([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]'
. '[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]{0,63})'
.'|'
. '("[^\\\"]{0,62}")'
.')$.'
,$arrLocalPortion[$i])) {
return false;
}
}
return true;
}
/**
* Checks email section after "@" symbol for validity
* @param strDomainPortion Text to be checked
* @return True if domain portion is valid, false if not
*/
protected function check_domain_portion($strDomainPortion) {
// Total domain can only be from 1 to 255 characters, inclusive
if (!$this->check_text_length($strDomainPortion, 1, 255)) {
return false;
}
// Check if domain is IP, possibly enclosed in square brackets.
if (preg_match('/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
.'(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$/'
,$strDomainPortion) ||
preg_match('/^\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
.'(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\]$/'
,$strDomainPortion)) {
return true;
} else {
$arrDomainPortion = explode('.', $strDomainPortion);
if (sizeof($arrDomainPortion) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0, $max = sizeof($arrDomainPortion); $i < $max; $i++) {
// Each portion must be between 1 and 63 characters, inclusive
if (!$this->check_text_length($arrDomainPortion[$i], 1, 63)) {
return false;
}
if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|'
.'([A-Za-z0-9]+))$/', $arrDomainPortion[$i])) {
return false;
}
if ($i == $max - 1) { // TLD cannot be only numbers
if (strlen(preg_replace('/[0-9]/', '', $arrDomainPortion[$i])) <= 0) {
return false;
}
}
}
}
return true;
}
/**
* Check given text length is between defined bounds
* @param strText Text to be checked
* @param intMinimum Minimum acceptable length
* @param intMaximum Maximum acceptable length
* @return True if string is within bounds (inclusive), false if not
*/
protected function check_text_length($strText, $intMinimum, $intMaximum) {
// Minimum and maximum are both inclusive
$intTextLength = strlen($strText);
if (($intTextLength < $intMinimum) || ($intTextLength > $intMaximum)) {
return false;
} else {
return true;
}
}
/**
* gets the array of not allowed domains to subscribe
* @return array
* @see https://github.com/ivolo/disposable-email-domains/blob/master/index.json
* @author Chema
* @date 13/07/2014
*/
protected function get_banned_domains()
{
return array("0-mail.com","0815.ru","0clickemail.com","0wnd.net","0wnd.org","10minutemail.com","20minutemail.com","2prong.com","30minutemail.com","33mail.com","3d-painting.com","4warding.com","4warding.net","4warding.org","60minutemail.com","675hosting.com","675hosting.net","675hosting.org","6url.com","75hosting.com","75hosting.net","75hosting.org","7tags.com","9ox.net","disposableemailaddresses.com","emailmiser.com","putthisinyourspamdatabase.com","sendspamhere.com","spamherelots.com","spamhereplease.com","tempemail.net","a-bc.net","afrobacon.com","ajaxapp.net","amilegit.com","amiri.net","amiriindustries.com","anonbox.net","anonymbox.com","antichef.com","antichef.net","antispam.de","armyspy.com","azmeil.tk","baxomale.ht.cx","beefmilk.com","binkmail.com","bio-muesli.net","bobmail.info","bodhi.lawlita.com","bofthew.com","boun.cr","bouncr.com","brefmail.com","broadbandninja.com","bsnow.net","bugmenot.com","bumpymail.com","casualdx.com","centermail.com","centermail.net","chogmail.com","choicemail1.com","cool.fr.nf","correo.blogos.net","cosmorph.com","courriel.fr.nf","courrieltemporaire.com","cubiclink.com","curryworld.de","cust.in","cuvox.de","dacoolest.com","dandikmail.com","dayrep.com","deadaddress.com","deadspam.com","despam.it","despammed.com","devnullmail.com","dfgh.net","digitalsanctuary.com","discardmail.com","discardmail.de","disposableaddress.com","disposeamail.com","disposemail.com","dispostable.com","dm.w3internet.co.ukexample.com","dodgeit.com","dodgit.com","dodgit.org","donemail.ru","dontreg.com","dontsendmespam.de","drdrb.com","drdrb.net","dump-email.info","dumpandjunk.com","dumpmail.de","dumpyemail.com","e4ward.com","einrot.com","email60.com","emaildienst.de","emailias.com","emailigo.de","emailinfive.com","emailmiser.com","emailsensei.com","emailtemporario.com.br","emailto.de","emailwarden.com","emailx.at.hm","emailxfer.com","emeil.in","emeil.ir","emz.net","enterto.com","ephemail.net","etranquil.com","etranquil.net","etranquil.org","explodemail.com","fakeinbox.com","fakeinformation.com","fastacura.com","fastchevy.com","fastchrysler.com","fastkawasaki.com","fastmazda.com","fastmitsubishi.com","fastnissan.com","fastsubaru.com","fastsuzuki.com","fasttoyota.com","fastyamaha.com","filzmail.com","fizmail.com","fleckens.hu","fr33mail.info","frapmail.com","front14.org","fux0ringduh.com","garliclife.com","get1mail.com","get2mail.fr","getairmail.com","getonemail.com","getonemail.net","ghosttexter.de","girlsundertheinfluence.com","gishpuppy.com","gowikibooks.com","gowikicampus.com","gowikicars.com","gowikifilms.com","gowikigames.com","gowikimusic.com","gowikinetwork.com","gowikitravel.com","gowikitv.com","great-host.in","greensloth.com","grr.la","gsrv.co.uk","guerillamail.biz","guerillamail.com","guerillamail.net","guerillamail.org","guerrillamail.biz","guerrillamail.com","guerrillamail.de","guerrillamail.net","guerrillamail.org","guerrillamailblock.com","gustr.com","h.mintemail.com","h8s.org","haltospam.com","hatespam.org","hidemail.de","hochsitze.com","hotpop.com","hulapla.de","ieatspam.eu","ieatspam.info","ihateyoualot.info","iheartspam.org","imails.info","inbax.tk","inbox.si","inboxalias.com","inboxclean.com","inboxclean.org","incognitomail.com","incognitomail.net","incognitomail.org","insorg-mail.info","ipoo.org","irish2me.com","iwi.net","jetable.com","jetable.fr.nf","jetable.net","jetable.org","jnxjn.com","jourrapide.com","junk1e.com","kasmail.com","kaspop.com","keepmymail.com","killmail.com","killmail.net","kir.ch.tc","klassmaster.com","klassmaster.net","klzlk.com","koszmail.pl","kulturbetrieb.info","kurzepost.de","letthemeatspam.com","lhsdv.com","lifebyfood.com","link2mail.net","litedrop.com","lol.ovpn.to","lookugly.com","lopl.co.cc","lortemail.dk","lr78.com","m4ilweb.info","maboard.com","mail-temporaire.fr","mail.by","mail.mezimages.net","mail2rss.org","mail333.com","mail4trash.com","mailbidon.com","mailblocks.com","mailcatch.com","maildrop.cc","maileater.com","mailexpire.com","mailfa.tk","mailfreeonline.com","mailin8r.com","mailinater.com","mailinator.com","mailinator.net","mailinator2.com","mailincubator.com","mailismagic.com","mailme.ir","mailme.lv","mailmetrash.com","mailmoat.com","mailnator.com","mailnesia.com","mailnull.com","mailshell.com","mailsiphon.com","mailslite.com","mailtothis.com","mailzilla.com","mailzilla.org","mbx.cc","mega.zik.dj","meinspamschutz.de","meltmail.com","messagebeamer.de","mierdamail.com","mintemail.com","moburl.com","moncourrier.fr.nf","monemail.fr.nf","monmail.fr.nf","monumentmail.com","msa.minsmail.com","mt2009.com","mx0.wwwnew.eu","mycleaninbox.net","mypartyclip.de","myphantomemail.com","myspaceinc.com","myspaceinc.net","myspaceinc.org","myspacepimpedup.com","myspamless.com","mytrashmail.com","neomailbox.com","nepwk.com","nervmich.net","nervtmich.net","netmails.com","netmails.net","netzidiot.de","neverbox.com","no-spam.ws","nobulk.com","noclickemail.com","nogmailspam.info","nomail.xl.cx","nomail2me.com","nomorespamemails.com","nospam.ze.tc","nospam4.us","nospamfor.us","nospamthanks.info","notmailinator.com","nowmymail.com","nurfuerspam.de","nus.edu.sg","nwldx.com","objectmail.com","obobbo.com","oneoffemail.com","onewaymail.com","online.ms","oopi.org","ordinaryamerican.net","otherinbox.com","ourklips.com","outlawspam.com","ovpn.to","owlpic.com","pancakemail.com","pimpedupmyspace.com","pjjkp.com","politikerclub.de","poofy.org","pookmail.com","privacy.net","proxymail.eu","prtnx.com","punkass.com","qq.com","quickinbox.com","rcpt.at","reallymymail.com","recode.me","recursor.net","regbypass.com","regbypass.comsafe-mail.net","rejectmail.com","rhyta.com","rklips.com","rmqkr.net","rppkn.com","rtrtr.com","s0ny.net","safe-mail.net","safersignup.de","safetymail.info","safetypost.de","sandelf.de","saynotospams.com","selfdestructingmail.com","sharklasers.com","shiftmail.com","shitmail.me","shortmail.net","sibmail.com","skeefmail.com","slaskpost.se","slopsbox.com","smashmail.de","smellfear.com","snakemail.com","sneakemail.com","sofimail.com","sofort-mail.de","sogetthis.com","soodonims.com","spam.la","spam.su","spam4.me","spamavert.com","spambob.com","spambob.net","spambob.org","spambog.com","spambog.de","spambog.ru","spambox.info","spambox.irishspringrealty.com","spambox.us","spamcannon.com","spamcannon.net","spamcero.com","spamcon.org","spamcorptastic.com","spamcowboy.com","spamcowboy.net","spamcowboy.org","spamday.com","spamex.com","spamfree24.com","spamfree24.de","spamfree24.eu","spamfree24.info","spamfree24.net","spamfree24.org","spamgoes.in","spamgourmet.com","spamgourmet.net","spamgourmet.org","spamhole.com","spamify.com","spaminator.de","spamkill.info","spaml.com","spaml.de","spammotel.com","spamobox.com","spamoff.de","spamslicer.com","spamspot.com","spamthis.co.uk","spamthisplease.com","spamtrail.com","speed.1s.fr","squizzy.de","supergreatmail.com","supermailer.jp","superrito.com","suremail.info","tagyourself.com","teewars.org","teleworm.com","teleworm.us","tempalias.com","tempe-mail.com","tempemail.biz","tempemail.com","tempinbox.co.uk","tempinbox.com","tempmail.it","tempmail2.com","tempomail.fr","temporarily.de","temporarioemail.com.br","temporaryemail.net","temporaryforwarding.com","temporaryinbox.com","thanksnospam.info","thankyou2010.com","thisisnotmyrealemail.com","throwawayemailaddress.com","tilien.com","tmailinator.com","tradermail.info","trash-amil.com","trash-mail.at","trash-mail.com","trash-mail.de","trash2009.com","trashemail.de","trashmail.at","trashmail.com","trashmail.de","trashmail.me","trashmail.net","trashmail.org","trashmail.ws","trashmailer.com","trashymail.com","trashymail.net","trillianpro.com","turual.com","twinmail.de","tyldd.com","uggsrock.com","upliftnow.com","uplipht.com","venompen.com","veryrealemail.com","viditag.com","viewcastmedia.com","viewcastmedia.net","viewcastmedia.org","webm4il.info","wegwerfadresse.de","wegwerfemail.de","wegwerfmail.de","wegwerfmail.net","wegwerfmail.org","wetrainbayarea.com","wetrainbayarea.org","wh4f.org","whatpaas.com","whyspam.me","willselfdestruct.com","winemaven.info","wronghead.com","wuzup.net","wuzupmail.net","www.e4ward.com","www.gishpuppy.com","www.mailinator.com","wwwnew.eu","xagloo.com","xemaps.com","xents.com","xmaily.com","xoxy.net","yep.it","yogamaven.com","yopmail.com","yopmail.fr","yopmail.net","ypmail.webarnak.fr.eu.org","yuurok.com","zehnminutenmail.de","zippymail.info","zoaxe.com","zoemail.org");
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment