Don't use regular expressions to "validate" email addresses

Steve Holden steve at holdenweb.com
Fri Sep 22 00:18:39 EDT 2006


Ben Finney wrote:
> "John Machin" <sjmachin at lexicon.net> writes:
> 
> 
>>A little more is unfortunately not enough. The best advice you got was
>>to use an existing e-mail address validator. The definition of a valid
>>e-mail address is complicated. You may care to check out "Mastering
>>Regular Expressions" by Jeffery Friedl. In the first edition, at least
>>(I haven't looked at the 2nd), he works through assembling a 4700+ byte
>>regex for validating e-mail addresses. Yes, that's 4KB.  It's the best
>>advertisement for *not* using regexes for a task like that that I've
>>ever seen.
> 
> 
> The best advice I've seen when people ask "How do I validate whether
> an email address is valid?" was "Try sending mail to it".
> 
That only applies if it's a likely-looking email address. If someone 
asks me to send mail to "splurge.!#$%*&^from at thingie?><{}_)" I will 
probably assume that it isn't worth my time trying.

If the email looks syntactically correct, *then* it's worth further 
validation by trying a delivery attempt.

> It's both Pythonic, and truly the best way. If you actually want to
> confirm, don't try to validate it statically; *use* the email address,
> and check the result.  Send an email to that address, and don't use it
> any further unless you get a reply saying "yes, this is the right
> address to use" from the recipient.
> 
This is a rather scatter-shot approach. Many possibilities can be 
properly eliminated by judicious lexical checks before delivery is 
considered.

> The sending system's mail transport agent, not regular expressions,
> determines which part is the domain to send the mail to.
> 
> The domain name system, not regular expressions, determines what
> domains are valid, and what host should receive mail for that domain.
> 
> Most especially, the receiving mail system, not regular expressions,
> determines what local-parts are valid.
> 
Nevertheless, I am *not* going to try delivery to (for example) a 
non-local address that doesn't contain an "at@ sign.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list