1: #!/usr/bin/perl
   2: 
   3: # By Sean Murphy
   4: # E-Mail Syntax Checker
   5: #
   6: # This is a simple script that will
   7: # check the syntax of an email address
   8: # and spit back whether the address is
   9: # syntactically correct or not. This
  10: # is my first post to the Craft node.
  11: 
  12: #mmmm...marshmallowey goodness...
  13: $atom = '[^()<>@,;:\".\[\] \000-\037\177]+';
  14: $quoted = '"(?:[^"\\\n]|\\.)*"';
  15: $word = "(?:$atom|$quoted)";
  16: $localpart = "$word(?:\.$word)*";
  17: $subdomain = "(?:$atom|\\[(?:[^\\[\\]\\\\\\r]|\\.)*\\])";
  18: $domain = "$subdomain(?:\.$subdomain)*";
  19: $addrspec = "$localpart\@$domain";
  20: $route = "(?:\@$domain)+:";
  21: $routeaddr = "<$route?$addrspec>";
  22: $mailbox = "(?:$addrspec|$word+$routeaddr)";
  23: $group = "$word+:(?:$mailbox(?:,+$mailbox)*)?;";
  24: $address = "$mailbox|$group";
  25: 
  26: sub addr {
  27: local($_) = @_;
  28: 1 while s/\((?:[^()]|\\[()])*?[^\\]\)/ /g;
  29: /$address/o;
  30: }
  31: 
  32: if (@ARGV > 0) {
  33: foreach (@ARGV) { print "$_ is ", addr($_) ? "" : "not ", "valid\n"; }
  34: } else {
  35: while (<>) { chop; print "$_ is ", addr($_) ? "" : "not ", "valid\n"; }
  36: }

Replies are listed 'Best First'.
Re: Email Address Syntax Checker
by grep (Monsignor) on May 14, 2002 at 18:15 UTC
    FYI there is already a Email::Valid module on CPAN that will even do mx record lookups to verify a valid (valid in the sense that that there is a mx record) domain name in the email address.

    PS: I also do not see where you support UUCP style email addresses



    grep
    Unix - where you can throw the manual on the keyboard and get a command
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Email Address Syntax Checker
by lachoy (Parson) on May 14, 2002 at 20:30 UTC

    In addition to Email::Valid, there's also a Mail::RFC822::Address which not only has no external dependencies (Email::Valid uses Net::DNS) but also exists as a PPM from the primary PPM repository.

    Chris
    M-x auto-bs-mode

      The only problem with those 2 modules is that they follow the exact RFC standard. Why is it a problem to follow standard? Because there are millions of email addresses out there that have a . before the @ (without the . being quoted), which is not RFC valid. I personally use Email::Valid::Loose, which contains all of the great features of Email::Valid yet doesn't break on my own email adderss.

        You might want to check again:

        #!/usr/bin/perl use strict; use Email::Valid; use Mail::RFC822::Address; my $address = 'ryan.311@osu.edu'; print 'Email::Valid: ', ( Email::Valid->address( $address ) ? 'yes' : 'no' ), "\n"; print 'Mail::RFC822::Address: ', ( Mail::RFC822::Address::valid( $address ) ? 'yes' : 'no' ), "\n";

        Output:

        Email::Valid: yes Mail::RFC822::Address: yes

        Chris
        M-x auto-bs-mode

Re: Re: Re: Email Address Syntax Checker
by belg4mit (Prior) on May 14, 2002 at 19:36 UTC
    "Snippets" kind of belong in CUFP no Craft. If you do a little searching here or on the web you'll actually found the proper regexp for checking mail address syntax. This is also addressed in perlfaq9.

    --
    perl -pew "s/\b;([mnst])/'$1/g"