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

Hello I have been using this code for a long time to check email addresses to see if they are valid. Now all of a sudden I get a call from someone with a valid email address that gets my 'invalid email address' message. His domain is in this format: firstnamelastinitial@swat.coop
swat.coop is the real domain name.

Here is my code:
if ($f->{name} eq "email" && ($value =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\ +.) / || $value !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1 +,3})(\]?)$/)) { $f->{errors} = "Invalid Email Address!"; $errors++; }
So my question is do you know of a better more efficient way to validate an email address?

What I mean is that there are so many different modules out there which one is best, this code worked, so I thought, I have used it for a very long time and did not even realize that domains with greater than 3 character .TLD's would not work...

thx,
Richard

Replies are listed 'Best First'.
Re: checking email syntax
by kyle (Abbot) on Feb 05, 2008 at 17:20 UTC
Re: checking email syntax
by mr_mischief (Monsignor) on Feb 05, 2008 at 18:05 UTC
    Your regex also disallows .info and .name BTW. If you're not going to use a module, at least follow the RFCs.

    I recommend Email::Address or Regexp::Common::Email::Address. I'd also encourage you to look into Mail::Message::Field::Full, Data::Validate::Email, Email::AddressParser or maybe Email::Valid as previously mentioned. Those are maintained for us all to use, and get updated whenever they need to be. It sure beats everyone doing something different. I can only recommend two specifically, because I can't recall having used the others.

    If you're having issues getting modules from CPAN (or PPM), please make a new post regarding those problems.

    If you're really needing to have a regex in your own code rather than using a module, you can get the regex for a valid email from Email::Address or Regexp::Common::Email::Address. I'd recommend against doing it that way, because you might need to update manually again in the future. CPAN is your friend, and it makes updating to newer versions of modules pretty easy.

    You might want to note that Email::Valid uses Mail::Address, and the author of Mail::Address states that it only covers 95% of valid cases. The author of that recommends Mail::Message::Field::Full. Email::Address doesn't have this particular issue, as it is designed to the RFC 2822 spec. That's why I mildly recommend against Email::Valid and Mail::Address.

Re: checking email syntax
by toolic (Bishop) on Feb 05, 2008 at 17:23 UTC
Re: checking email syntax
by dsheroh (Monsignor) on Feb 05, 2008 at 18:16 UTC
    If you really want to set up your own regex to validate email addresses, grab a copy of Mastering Regular Expressions, which has the appropriate regex on the last page (prior to the index). It's only 6,598 characters long...

    If you don't want to have to deal with that, then there's always CPAN. Also keep in mind that a syntactically-correct email address is not necessarily a valid email address.

Re: checking email syntax
by bradcathey (Prior) on Feb 05, 2008 at 19:11 UTC

    kyle is dead on. I use Email::Valid all day. There are just toooooo many options for e-mail addresses my friend.

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: checking email syntax
by superfrink (Curate) on Feb 06, 2008 at 04:38 UTC
    I use Regexp::Common
    use Regexp::Common qw[Email::Address]; unless ($email_addr =~ m/($RE{Email}{Address})/) { # error }
    I also just started using Email::Valid. It can do checks to see if the domain really exists and has a MX record.
    unless (Email::Valid->address(-address => $email_addr, -mxcheck => 1)) + { # error }