Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Email Address Syntax Checker

by mousey (Scribe)
on May 14, 2002 at 17:28 UTC ( #166505=perlcraft: print w/replies, xml ) Need Help??

   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"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlcraft [id://166505]
Approved by defyance
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2022-12-03 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?