hux has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I know its been asked millios of times before, but in the eyes of the almighty perl monks, what is the best way to validate an e-mail address?

If its just a simple regexp then please enlighen me to what it would look like.

Edit by tye

Replies are listed 'Best First'.
Re: E-mail
by DamnDirtyApe (Curate) on Aug 03, 2002 at 22:42 UTC

    To do it correctly, it is not a simple regexp. use the Email::Valid module from CPAN.

    use Email::Valid; print (Email::Valid->address('maurice@hevanet.com') ? 'yes' : 'no');

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      As I'm sure you've found out, this will fail on non-RFC compliant addresses that are deliverable (as detailed in recipe 6.19 of The Perl Cookbook), as well as those people from AOL who don't know their own email address (joe@aol, because everyone uses AOL, right?)

      Also, with Email::Valid, you should enable the -mxcheck option and do some SMTP afterwards to verify that what you found, has an actual account on the other side.

Re: E-mail
by Albannach (Monsignor) on Aug 04, 2002 at 02:28 UTC
    You should really type perldoc -q address at your nearest console and read the well chosen answer. While DamnDirtyApe has suggested a fine and useful module, it does help to be aware that there really is no way to be certain that an e-mail address will work. Actually that is also repeated in the POD for Email::Valid which also references the PerlFAQ, and the cycle continues...

    If you really want to see a regex that attempts this, I recommend Jeffrey Friedl's wonderful (if slightly outdated now) Mastering Regular Expressions which includes a discussion on just such a beast towards the end.

    Update: See more good information from larsen at Re: Regex Unexplained Failure in response to a very similar question asked only 20 minutes after yours...

    --
    I'd like to be able to assign to an luser

Re: How to validate E-mail addresses
by hacker (Priest) on Aug 04, 2002 at 08:47 UTC
      My module is called "RFC::RFC822::Address", and the link you are making is to a different module, written by someone else.

      Abigail

Re: How to validate E-mail addresses
by TStanley (Canon) on Aug 04, 2002 at 05:26 UTC
    I asked a similar question some time back. Take a look at the responses there. Also note that I used the regex referred to by Albannach in my script.

    TStanley
    --------
    Never underestimate the power of very stupid people in large groups -- Anonymous
Re: How to validate E-mail addresses
by adrianh (Chancellor) on Aug 04, 2002 at 10:59 UTC

    You might want to consider Mail::CheckUser instead of Email::Valid.

    It can do a bit more than the basic MX lookup that Email::Valid does.

    It does lack the address "fudging" and ability to specify rules for local addresses - but that's often not a huge issue.