rvalkass 5 Report post Posted November 26, 2005 This simple script will allow you to run some basic checks to make sure that any email address entered is actually an email address. There is no guarantee offered that this will stop every single fake email address, but it'll provide some protection. Now, the code! First we need to get the email address to verify. Here, I get it using POST from an HTML form. <?php//Load email address from web form$email = $_POST['email']; Now, we move on to our first check. Does the text that has been entered look like it could be an email address?This check can be performed using something called 'regular expressions'. This is basically a set of rules defining where characters can be and what characters are allowed. Its quite complicated to start with, but you should be able to get the jist of it. //Check that the text entered follows the format of an email addressif (preg_match("/^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$/", $email)){ echo '<font face="Arial" size="+3" color="green">The format is <b>valid</b>!</font><br>';}else{ echo '<font face="Arial" size="+3" color="red">The format is <b>invalid</b>!</font><br>';} The regular expression here is: /^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$/In English it basically says:Some letters or numbers, followed by some more letters or numbers or a dot then followed by an @ sign, then some more letters and numbers or a dot. Its quite complicated to explain! If the text entered follows that format then the first echo is carried out. If not, then it says its failed. You can easily replace those with anything you want. Now, we split the email address into 2 parts: the username and the domain name. //Split the email address apartlist($userName, $domainName) = split("@", $email);Quite self explanitory. Anything before the @ is put into $userName and anything after is put into $domainName We can now use this information to check that the domain name exists. This gives a very good idea as to whether the email address is valid. Someone could enter "gobbledeegook@madman.pest" which would pass the first test, but fail the second. We check for the domain by checking for a mail exchange record for that domain name. If one doesn't exists it probably means the server doesn't either. //Then see if the domain existsif (checkdnsrr($domainName, "MX")) { echo '<font face="Arial" size="+3" color="green">The domain name is <b>valid</b>!</font><br>';}else{ echo '<font face="Arial" size="+3" color="red">The domain name is <b>invalid</b>!</font><br>';}?>The "MX" tells PHP we want to look for a mail exchange record. There are various records that can be checked for, but they are irrelevant for this purpose. Again, it prints the success and failure messages but these can be replaced as you wish. Thats it. You now have a mail checker. I have set up a demo here with an HTML form front end. There is one final check that can be performed, but this blocks most free domains such as @hotmail.com, @yahoo.co.uk, @gmail.com and I have therefore omitted it. It is incredibly secure and it does mean that any address you do get through is likely to be an ISP address, web hosting address or another address you have to pay for, and is therefore very unlikely to be fake, but blocks the majority of the Internet using World out. Any problems, questions, comments or suggestions please feel free to contact me by posting below, PM, email or MSN Messenger, all of which is on my member profile. Share this post Link to post Share on other sites
Spectre 0 Report post Posted November 27, 2005 Just to provide a (hopefully) clearer description, the pattern: [_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)* Basically translates to:[any letter, number, underscore, or dash] repeated one or more times (a dot [any letter, number, underscore, or dash] repeated one or more times) appearing zero or more times @ [any number, letter, or dash] repeated one ore more times (a dot [any letter, number, or a dash] repeated one or more times) appearing zero or times.Also, you may want to quote sources or give due credit when using code obtained from other parties. Share this post Link to post Share on other sites
prashant 0 Report post Posted November 27, 2005 Nice cool validation thing.I would be doing a similar thing in javaScript.just tell me one thing .what is the minimum length of characters of domain ??eg. prashant@abc.comprashant@ab.comprashant@a.comcan there be sites like A.com , AB.com or ABC.com ??what is the minimum length of the domain ?? Share this post Link to post Share on other sites
Spectre 0 Report post Posted November 27, 2005 It depends on the TLD. The minimum .com length is currently three characters - although every single possible three character .com domain name has long been registered, and the only way to get one is by snatching it after it expires. In the past, the minimum length has been two and even one for this TLD, but only for a short time (x.com, cj.com and aa.com are examples of domain names registered before the minimum length was increased). With the exception of .info, which has a minimum four character length, the other generic TLDs are the same, and most ccTLDs are as well. You should be able to find more information at IANA, InterNIC and ICANN. Anyway, this is getting a little off topic... Share this post Link to post Share on other sites
rvalkass 5 Report post Posted November 27, 2005 As far as I am aware, only 3 single-letter domains exist as there was a restriction put on them in 1993. The script doesn't check for the length of the domain, merely the existance of it. If it's too short to exist, then it will not find the domain and stop anyway.Spectre, the only code that I took from anywhere else was the regexp, which is from here, and thats only because my first attempt refused a few real addresses. The rest of the code I typed myself. Share this post Link to post Share on other sites
Plenoptic 0 Report post Posted November 27, 2005 This is a very nice tutorial. Thanks a lot rvalkass. I will have to use this when I make a membership system for my site. I haven't tried the tutorial yet but I will make sure to remember it. Share this post Link to post Share on other sites
corzel 0 Report post Posted December 17, 2005 Hi!Can i validate email addresses with this script in the same way that programs like bulk email validator works.That?s because i have a mailing list and some times i receive may mails back. Share this post Link to post Share on other sites
bucksta 0 Report post Posted January 6, 2006 nice php script. a lot of sites use something like this. staticscripts uses some sort of emaill scripts. i dont know how it sends email but it has a system that sends it as a certain email depending oin what is happening. that thing also validates. nice script! Share this post Link to post Share on other sites
wassie 0 Report post Posted January 7, 2006 nice php script :)good tutorial, nice explaining of what does what. Thanks for posting this on Xisto, i think lots of members can use this :Pthumbs up! Share this post Link to post Share on other sites
iGuest 3 Report post Posted August 30, 2008 A native email validation function Verifying Email Addresses There is a built-in email validation function in PHP. It's called filter_var <http://de1.php.net/manual-lookup.php?pattern=filter_var%2F%26gt〈=en&scope=404quickref; Use it like this: Filter_var($email, FILTER_VALIDATE_EMAIL) -reply by waro Share this post Link to post Share on other sites