2

Possible Duplicate:
How to validate an emailaddress in PHP

I was wondering what everyone is doing in regards to validating email addresses and how extensive the validation needs to be.

For example, of those methods, which one is preferred and why?

  1. Extensive check: http://www.linuxjournal.com/article/9585?page=0,3
  2. PHP filter: http://php.net/manual/en/filter.filters.validate.php
  3. Eregi: http://www.totallyphp.co.uk/code/validate_an_email_address_using_regular_expressions.htm
  4. Preg match with DNS check: http://www.soaptray.com/blog/2008/04/validate-email-addresses-using-php/

My goal is to find a good and solid solution for the average project, so when the time comes to validate an email address, I know exactly what I'm going to use.

Any feedback much appreciated!

-Ryan

Community
  • 1
  • 1
NightHawk
  • 3,633
  • 8
  • 37
  • 56

4 Answers4

7

Use PHP’s build-in filters for a syntactic validation and send a verification e-mail for an intention confirmation.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
3

http://www.regular-expressions.info/email.html has the RFC2822 recommended reg ex.

Andrew Sledge
  • 10,163
  • 2
  • 29
  • 30
0

I don't think there's a foolproof solution, other than sending an email to that address and requesting a response. Regexps and the like are a pain to implement properly.

Your solution will depend on how valuable that email address is. Are you sending a newsletter ? A sales confirmation ? A critical alert ? Some trivial regexp solution may suffice for the first. Getting some sort of confirmation would be valuable for the second. Repeated confirmations of validity (e.g. monthly checks) may be required for the third.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

Here's a great blog entry about this : http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx

Personnally, I usually use a simple RegExp that will validate most of the addresses you find on the Internet, so the solution in the third link is what I would use. But the best solution is, I think, to send a e-mail to the specified address (using a simple RegExp before to warn the user) with a link to validate it. So you're sure it's a valid and allocated address.

Pik'
  • 6,819
  • 1
  • 28
  • 24