[Tutor] re: Checking e-mail addresses [Mailman and email validation]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri Jan 31 05:18:39 2003


[about writing a email address validator]

> If we could take this off-list and you could help me I'll make and
> release the Python version.


Hi Charlie,

I know that there's got to be something in Mailman, the Mailing List
Manager,

   http://www.list.org/

that must do some initial validation of email addresses: Mailman has a
utility that allows for a batch of users to become subscribed (it's
'bin/addmembers'), and in the process of mass subscription, Mailman culls
out a list of addresses that are definitely "wrong".


If you have the Mailman source code handly, you can take a look at
'Mailman/Utils.py:ValidateEmail'.  The responsible code is actually quite
short, but it does weird stuff.  (os.path.join()?  What is THAT doing in
there?  *grin*)


Here's what it looks like on a system of mine:

###
_badchars = re.compile('[][()<>|;^,]')

def ValidateEmail(str):
    """Verify that the an email address isn't grossly invalid."""
    # Pretty minimal, cheesy check.  We could do better...
    if not str:
        raise Errors.MMBadEmailError
    if _badchars.search(str) or str[0] == '-':
        raise Errors.MMHostileAddress
    if string.find(str, '/') <> -1 and \
       os.path.isdir(os.path.split(str)[0]):
        # then
        raise Errors.MMHostileAddress
    user, domain_parts = ParseEmail(str)
    # this means local, unqualified addresses, are no allowed
    if not domain_parts:
        raise Errors.MMBadEmailError
    if len(domain_parts) < 2:
        raise Errors.MMBadEmailError
###


But they know that it's a hack: they even say that they should do better!
So if you write an improved version of an email validator, the Mailman
folks should be very interested in it.  *grin*


Good luck!