There are probably better ways of doing this, but when pressed for an answer one day this is that I came up with.
The idea was to parse a text field from a CGI form that supposedly contained a valid E-Mail address. Not only did we want to see if the address was formed correctly but the desire was to make sure that it was deliverable.
If you have suggestions on how to improve on it I'd be very happy to hear from you.
sub emailValid {
my $email=shift; # This is the addr to test
my $t=$email; # We're gonna chop up $t mercilessly
local *PIPE;
my $nslookup;
# A very simple stupid way to find nslookup. It's gotta be here
# somewhere....
if ( -x "/usr/sbin/nslookup" ) {
$nslookup = "/usr/sbin/nslookup";
} else {
$nslookup = "/usr/bin/nslookup";
}
#
# Tokenize the mail address into user@domain syntax
$t=~m:^(.*)\@(.*)$:;
my($user,$domain)=($1,$2);
return 1 if (! $user ) or ( $user eq '' ); # No user!
return 2 if (! $domain ) or ( $domain eq '' ); # No domain!
my $IFS=$/; # Save that please...
$/=''; # We're gonna make one big input...
open(PIPE,"$nslookup -type=any $domain 2>&1 |")
or carp "Cannot run $nslookup ! " . $! ;
my @check=<PIPE>; #slurp!
close PIPE; # gulp!
$/=$IFS; # put that back.
return 3 if grep /Non-existent/,@check;
return 0; # Checks out ok...
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.