[Zope] Email address validator?
Jim Washington
jwashin@vt.edu
Sat, 24 Nov 2001 13:12:54 -0500
This is a multi-part message in MIME format.
--------------090008040709020108070208
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Barry A. Warsaw wrote:
>>>>>>"BE" == Bruce Eckel <Bruce@EckelObjects.com> writes:
>>>>>>
>
> BE> Worked like a charm! It turns out I was handing it an invalid
> BE> "from" email address.
>
> BE> Which brings up another question -- is there a piece of code
> BE> that will validate email addresses somewhere? Seems like it
> BE> would be a common tool (I'll bet the spammers have it!).
>
>Do you mean you want to find out if a string looks like a valid email
>address, or that it actually successfully delivers to some end
>destination? The former is doable, the latter is exceedingly
>difficult!
>
>Mailman has the following bit of code, which accomplishes the goal set
>out in its docstring <wink>:
>
I hadn't thought of looking through mailman. Here's one I put together
a couple of weeks ago that seems a bit more thorough...
-- Jim Washington
--------------090008040709020108070208
Content-Type: text/plain;
name="email_checker.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="email_checker.py"
# This is hacked from an existing script written in perl, licensed "public domain"
# The original perl script: http://aspn.activestate.com/ASPN/Cookbook/Rx/Recipe/68432 2Nov2001
# author e-mail: tbyte@bolehmail.com
"""
Text of the perl script:
sub ValidEmailAddr { #check if e-mail address format is valid
my $mail = shift; #in form name@host
return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ ); #characters allowed on name: 0-9a-Z-._ on host: 0-9a-Z-. on between: @
return 0 if ( $mail =~ /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/); #must start or end with alpha or num
return 0 if ( $mail !~ /([0-9a-zA-Z]{1})\@./ ); #name must end with alpha or num
return 0 if ( $mail !~ /.\@([0-9a-zA-Z]{1})/ ); #host must start with alpha or num
return 0 if ( $mail =~ /.\.\-.|.\-\..|.\.\..|.\-\-./g ); #pair .- or -. or -- or .. not allowed
return 0 if ( $mail =~ /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./g ); #pair ._ or -_ or _. or _- or __ not allowed
return 0 if ( $mail !~ /\.([a-zA-Z]{2,3})$/ ); #host must end with '.' plus 2 or 3 alpha for TopLevelDomain (MUST be modified in future!)
return 1;
}
e-mail checker, checks whether an e-mail address is formatted properly.
usage:
from email_checker import valid_email
s = "string to be tested"
if valid_email(s):
do_something()
else:
do something_else()
"""
import re
rega = re.compile("^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$") #failure a
regb = re.compile("^[^0-9a-zA-Z]|[^0-9a-zA-Z]$") #failure b
regc = re.compile("([0-9a-zA-Z]{1})\@.") #failure c
regd = re.compile(".\@([0-9a-zA-Z]{1})") #failure d
rege = re.compile(".\.\-.|.\-\..|.\.\..|.\-\-.") #failure e
regf = re.compile(".\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_.") #failure f
regg = re.compile(".\.([a-zA-Z]{2,3})$|.\.([a-zA-Z]{2,4})$")#failure g
def valid_email(address, debug=None):
fail = None
if rega.search(address) is None:
fail = 'a'
elif regb.search(address) is not None:
fail = 'b'
elif regc.search(address) is None:
fail = 'c'
elif regd.search(address) is None:
fail = 'd'
elif rege.search(address) is not None:
fail = 'e'
elif regf.search(address) is not None:
fail = 'f'
elif regg.search(address) is None:
fail = 'g'
if fail is not None:
if debug:
return address,fail,None
else:
return None
if debug:
return address,1,1
else:
return 1
if __name__ == '__main__':
#test a few:
print valid_email('z+z.e@bun.org', debug=1)
print valid_email('z.e@.org', debug=1)
print valid_email('_az.e@bun.org', debug=1)
print valid_email('e@b.g', debug=1)
print valid_email('a_-.e@bun.org', debug=1)
print valid_email('ok.computer@-bun.org', debug=1)
print valid_email('ok.e@bun.org', debug=1)
print valid_email('OK@bun.org', debug=1)
print valid_email('t@bun.info', debug=1)
print valid_email('my friend@bun.org', debug=1)
print valid_email('my.friend.bun.org', debug=1)
--------------090008040709020108070208--