blib/lib/Email/Valid.pm

filter: Statementstmt 71.0% 152 / 214 Branchbran 47.3% 72 / 152 Conditioncond 48.2% 28 / 58 MC/DCmcdc 27.7% 10 / 36 Subroutinesub 84.6% 22 / 26 Podpod 100% 6 / 6 totaltotal 58.9% 290 / 492 SCAR 66.1
CC
98
Cov
59%
_nslookup_query
210.0
_find_nslookup
56.0
_net_dns_query
35.8
CRAP 739.1
↑ Summary
1 3 require 5.006;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package Email::Valid 1.203;
5  
6 # ABSTRACT: Check validity of Internet email addresses
7 our (
8 3 $RFC822PAT,
9 $Details, $Resolver, $Nslookup_Path,
10 $Debug,
11 );
12  
13 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Mail::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 our %AUTOLOAD = (
20 allow_ip => 1,
21 fqdn => 1,
22 fudge => 1,
23 mxcheck => 1,
24 tldcheck => 1,
25 local_rules => 1,
26 localpart => 1,
27 );
28  
29 3 our $NSLOOKUP_PAT = 'preference|serial|expire|mail\s+exchanger';
30 3 our @NSLOOKUP_PATHS = File::Spec->path();
31  
32 # initialize if already loaded, better in prefork mod_perl environment
33 3 ▶our $DNS_Method = defined $Net::DNS::VERSION ? 'Net::DNS' : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $Net::DNS::VERSION ? : 1 2 3
34 3 ▶unless ($DNS_Method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Email::Valid::DNS_Method) 2 1 3
35 2 __PACKAGE__->_select_dns_method;
36 }
37  
38 # initialize $Resolver if necessary
39 3 ▶if ($DNS_Method eq 'Net::DNS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($Email::Valid::DNS_Method eq "Net::DNS") 3 0 3
40 3 ▶ unless (defined $Resolver) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Email::Valid::Resolver) 3 0 3
41 3 $Resolver = Net::DNS::Resolver->new;
42 }
43 }
44  
45 sub new {
46 1 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
47  
48 1 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
49 1 bless my $self = {}, $class;
50 1 $self->_initialize;
51 1 %$self = $self->_rearrange([ keys %AUTOLOAD ], \@_);
52 1 return $self;
53 }
54  
55 sub _initialize {
56 5 ▶ my $self = shift;
Subroutine _initialize: called CC=1 SCAR=0.0
57  
58 5 $self->{mxcheck} = 0;
59 5 $self->{tldcheck} = 0;
60 5 $self->{fudge} = 0;
61 5 $self->{fqdn} = 1;
62 5 $self->{allow_ip} = 1;
63 5 $self->{local_rules} = 0;
64 5 $self->{localpart} = 1;
65 5 $self->{details} = $Details = undef;
66 }
67  
68 # Pupose: handles named parameter calling style
69 sub _rearrange {
70 59 ▶ my $self = shift;
Subroutine _rearrange: called CC=8 SCAR=20.8
71 59 my(@names) = @{ shift() };
72 59 my(@params) = @{ shift() };
73 59 my(%args);
74  
75 59 ▶ ref $self ? %args = %$self : _initialize( \%args );
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 55 4 59
76 59 ▶ return %args unless @params;
Branch: 1 branch
BranchTrueFalseTotal
unless @params 1 58 59
77  
78 58 ▶ unless (@params > 1 and $params[0] =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@params > 1 and $params[0] =~ /^-/) 12 46 58
Condition66%2/3
@params > 1 and $params[0] =~ /^-/
!ll&&!rl&&r
12046
MC/DC50%1/2
@params > 1 and $params[0] =~ /^-/
@params > 1 $params[0] =~ /^-/
Truth table66%2/3
A and B
A= @params > 1B= $params[0] =~ /^-/
ABresult
0X0
100
111
79 12 ▶ while(@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 12 12 24
80 12 ▶ croak 'unexpected number of parameters' unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 12 12
81 12 $args{ lc shift @names } = shift @params;
82 }
83 12 return %args;
84 }
85  
86 46 ▶ while(@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 61 46 107
87 61 my $param = lc substr(shift @params, 1);
88 61 $args{ $param } = shift @params;
89 }
90  
91 46 %args;
92 }
93  
94 # Purpose: determine why an address failed a check
95 sub details {
96 13 ▶ my $self = shift;
Subroutine details: called CC=4 SCAR=13.9
Pod documented
97  
98 13 ▶ return (ref $self ? $self->{details} : $Details) unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 3 10 13
ref $self ? : 3 0 3
99 10 $Details = shift;
100 10 ▶ $self->{details} = $Details if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 8 2 10
101 10 return undef;
102 }
103  
104 # Purpose: Check whether address conforms to RFC 822 syntax.
105 sub rfc822 {
106 28 ▶ my $self = shift;
Subroutine rfc822: called CC=6 SCAR=18.6
Pod documented
107 28 my %args = $self->_rearrange([qw( address )], \@_);
108  
109 28 ▶ my $addr = $args{address} or return $self->details('rfc822');
Branch: 1 branch
BranchTrueFalseTotal
my $addr = $args{"address"} or return $self->details("rfc822") 0 28 28
110 28 ▶ $addr = $addr->address if (blessed($addr) && $addr->isa('Mail::Address'));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($addr) and $addr->isa("Mail::Address") 0 28 28
Condition33%1/3
builtin::blessed($addr) and $addr->isa("Mail::Address")
!ll&&!rl&&r
2800
MC/DC0%0/2
builtin::blessed($addr) and $addr->isa("Mail::Address")
builtin::blessed($addr) $addr->isa("Mail::Address")
Truth table33%1/3
A and B
A= builtin::blessed($addr)B= $addr->isa("Mail::Address")
ABresult
0X0
100
111
111  
112 28 ▶ return $self->details('rfc822')
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\P{ASCII}/ or not $addr =~ /^$Email::Valid::RFC822PAT$/o 3 25 28
Condition100%3/3
$addr =~ /\P{ASCII}/ or not $addr =~ /^$Email::Valid::RFC822PAT$/o
l!l&&r!l&&!r
2125
MC/DC100%2/2
$addr =~ /\P{ASCII}/ or not $addr =~ /^$Email::Valid::RFC822PAT$/o
$addr =~ /\P{ASCII}/ not $addr =~ /^$Email::Valid::RFC822PAT$/o
Truth table100%3/3
A or not(B)
A= $addr =~ /\P{ASCII}/B= not $addr =~ /^$Email::Valid::RFC822PAT$/o
ABresult
1X1
011
000
113 if $addr =~ /\P{ASCII}/ or $addr !~ m/^$RFC822PAT$/o;
114  
115 25 return 1;
116 }
117  
118 # Purpose: attempt to locate the nslookup utility
119 sub _find_nslookup {
120 0 ▶ my $self = shift;
Subroutine _find_nslookup: not called CC=7 SCAR=40.3
121  
122 0 my $ns = 'nslookup';
123 0 foreach my $path (@NSLOOKUP_PATHS) {
124 0 my $file = File::Spec->catfile($path, $ns);
125 0 ▶ return "$file.exe" if ($^O eq 'MSWin32') and -x "$file.exe" and !-d _;
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" and -x "$file.exe" and not -d _ 0 0 0
Condition0%0/6
$^O eq "MSWin32" and -x "$file.exe" and not -d _
!ll&&!rl&&r
000
$^O eq "MSWin32" and -x "$file.exe"
!ll&&!rl&&r
000
MC/DC0%0/3
$^O eq "MSWin32" and -x "$file.exe" and not -d _
$^O eq "MSWin32" -x "$file.exe" not -d _
Truth table0%0/4
A and B and not(C)
A= $^O eq "MSWin32"B= -x "$file.exe"C= not -d _
ABCresult
0XX0
10X0
1100
1111
126 0 ▶ return $file if -x $file and !-d _;
Branch: 1 branch
BranchTrueFalseTotal
if -x $file and not -d _ 0 0 0
Condition0%0/3
-x $file and not -d _
!ll&&!rl&&r
000
MC/DC0%0/2
-x $file and not -d _
-x $file not -d _
Truth table0%0/3
A and not(B)
A= -x $fileB= not -d _
ABresult
0X0
100
111
127 }
128 0 return undef;
129 }
130  
131 sub _select_dns_method {
132 # Configure a global resolver object for DNS queries
133 # if Net::DNS is available
134 2 ▶ eval { require Net::DNS };
Subroutine _select_dns_method: called CC=2 SCAR=7.6
135 2 ▶ return $DNS_Method = 'Net::DNS' unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 2 0 2
136  
137 0 $DNS_Method = 'nslookup';
138 }
139  
140 # Purpose: perform DNS query using the Net::DNS module
141 sub _net_dns_query {
142 2 ▶ my $self = shift;
Subroutine _net_dns_query: called CC=13 SCAR=35.8
143 2 my $host = shift;
144  
145 2 ▶ $Resolver = Net::DNS::Resolver->new unless defined $Resolver;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Email::Valid::Resolver 0 2 2
146  
147 2 my @mx_entries = Net::DNS::mx($Resolver, $host);
148  
149 # Check for valid MX records for $host
150 2 ▶ if (@mx_entries) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mx_entries) 2 0 2
151 # Check for RFC-7505 Null MX
152 2 my $nmx = scalar @mx_entries;
153 2 ▶ if ($nmx == 1 && length($mx_entries[0]->exchange) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmx == 1 and length $mx_entries[0]->exchange == 0) 0 2 2
Condition33%1/3
$nmx == 1 and length $mx_entries[0]->exchange == 0
!ll&&!rl&&r
200
MC/DC0%0/2
$nmx == 1 and length $mx_entries[0]->exchange == 0
$nmx == 1 length $mx_entries[0]->exchange == 0
Truth table33%1/3
A and B
A= $nmx == 1B= length $mx_entries[0]->exchange == 0
ABresult
0X0
100
111
154 0 return $self->details('mx');
155 }
156 2 foreach my $mx (@mx_entries) {
157 2 my $mxhost = $mx->exchange;
158 2 my $query = $Resolver->search($mxhost);
159 2 ▶ next unless ($query);
Branch: 1 branch
BranchTrueFalseTotal
unless $query 0 2 2
160 2 foreach my $a_rr ($query->answer) {
161 2 ▶ return 1 unless $a_rr->type ne 'A';
Branch: 1 branch
BranchTrueFalseTotal
unless $a_rr->type ne "A" 2 0 2
162 }
163 }
164 }
165  
166 # Check for A record for $host
167 0 my $ans = $Resolver->query($host, 'A');
168 0 ▶ my @a_rrs = $ans ? $ans->answer : ();
Branch: 1 branch
BranchTrueFalseTotal
$ans ? : 0 0 0
169  
170 0 ▶ if (@a_rrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a_rrs) 0 0 0
171 0 foreach my $a_rr (@a_rrs) {
172 0 ▶ return 1 unless $a_rr->type ne 'A';
Branch: 1 branch
BranchTrueFalseTotal
unless $a_rr->type ne "A" 0 0 0
173 }
174 }
175  
176 # MX Check failed
177 0 return $self->details('mx');
178 }
179  
180 # Purpose: perform DNS query using the nslookup utility
181 sub _nslookup_query {
182 0 ▶ my $self = shift;
Subroutine _nslookup_query: not called CC=14 SCAR=53.5
183 0 my $host = shift;
184 0 local($/, *OLDERR);
185  
186 0 ▶ unless ($Nslookup_Path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Email::Valid::Nslookup_Path) 0 0 0
187 0 ▶ $Nslookup_Path = $self->_find_nslookup
Branch: 1 branch
BranchTrueFalseTotal
$Email::Valid::Nslookup_Path = $self->_find_nslookup or croak("unable to locate nslookup") 0 0 0
188 or croak 'unable to locate nslookup';
189 }
190  
191 # Check for an A record
192 0 ▶ return 1 if gethostbyname $host;
Branch: 1 branch
BranchTrueFalseTotal
if gethostbyname $host 0 0 0
193  
194 # Check for an MX record
195 0 ▶ if ($^O eq 'MSWin32' or $^O eq 'Cygwin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32' or $^O eq 'Cygwin') { } 0 0 0
Condition0%0/3
$^O eq "MSWin32" or $^O eq "Cygwin"
l!l&&r!l&&!r
000
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "Cygwin"
$^O eq "MSWin32" $^O eq "Cygwin"
Truth table0%0/3
A or B
A= $^O eq "MSWin32"B= $^O eq "Cygwin"
ABresult
1X1
011
000
196 # Oh no, we're on Windows!
197 0 require IO::CaptureOutput;
198 0 my $response = IO::CaptureOutput::capture_exec(
199 $Nslookup_Path, '-query=mx', $host
200 );
201 0 ▶ croak "unable to execute nslookup '$Nslookup_Path': exit $?" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
202 0 ▶ print STDERR $response if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Email::Valid::Debug 0 0 0
203 0 ▶ $response =~ /$NSLOOKUP_PAT/io or return $self->details('mx');
Branch: 1 branch
BranchTrueFalseTotal
$response =~ /$Email::Valid::NSLOOKUP_PAT/io or return $self->details("mx") 0 0 0
204 0 return 1;
205 } else {
206 # phew, we're not on Windows!
207 0 ▶ if (my $fh = IO::File->new('-|')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = 'IO::File'->new('-|')) { } 0 0 0
208 0 my $response = <$fh>;
209 0 ▶ print STDERR $response if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Email::Valid::Debug 0 0 0
210 0 close $fh;
211 0 ▶ $response =~ /$NSLOOKUP_PAT/io or return $self->details('mx');
Branch: 1 branch
BranchTrueFalseTotal
$response =~ /$Email::Valid::NSLOOKUP_PAT/io or return $self->details("mx") 0 0 0
212 0 return 1;
213 } else {
214 0 ▶ open OLDERR, '>&STDERR' or croak "cannot dup stderr: $!";
Branch: 1 branch
BranchTrueFalseTotal
open OLDERR, ">&STDERR" or croak("cannot dup stderr: $!") 0 0 0
215 0 ▶ open STDERR, '>&STDOUT' or croak "cannot redirect stderr to stdout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or croak("cannot redirect stderr to stdout: $!") 0 0 0
216 {
217 0 exec $Nslookup_Path, '-query=mx', $host;
218 }
219 0 open STDERR, ">&OLDERR";
220 0 croak "unable to execute nslookup '$Nslookup_Path': $!";
221 }
222 }
223 }
224  
225 # Purpose: Check whether a top level domain is valid for a domain.
226 sub tld {
227 0 ▶ my $self = shift;
Subroutine tld: not called CC=4 SCAR=30.0
Pod documented
228 0 my %args = $self->_rearrange([qw( address )], \@_);
229  
230 0 ▶ unless (eval {require Net::Domain::TLD; Net::Domain::TLD->VERSION(1.65); 1}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Net::Domain::TLD; "Net::Domain::TLD"->VERSION("1.65"); 1 } }) 0 0 0
231 0 die "Net::Domain::TLD not available";
232 }
233  
234 0 ▶ my $host = $self->_host( $args{address} or return $self->details('tld') );
Condition0%0/2
$args{'address'} || (return $self->details('tld'))
l!l
00
MC/DC0%0/1
$args{'address'} || (return $self->details('tld'))
$args{'address'}
Truth table0%0/2
A
A= $args{'address'}
Aresult
11
00
235 0 my ($tld) = $host =~ m#\.(\w+)$#;
236  
237 0 my %invalid_tlds = map { $_ => 1 } qw(invalid test example localhost);
238  
239 0 ▶ return defined $invalid_tlds{$tld} ? 0 : Net::Domain::TLD::tld_exists($tld);
Branch: 1 branch
BranchTrueFalseTotal
defined $invalid_tlds{$tld} ? : 0 0 0
240 }
241  
242 # Purpose: Check whether a DNS record (A or MX) exists for a domain.
243 sub mx {
244 2 ▶ my $self = shift;
Subroutine mx: called CC=7 SCAR=25.7
Pod documented
245 2 my %args = $self->_rearrange([qw( address )], \@_);
246  
247 2 ▶ my $host = $self->_host($args{address}) or return $self->details('mx');
Branch: 1 branch
BranchTrueFalseTotal
my $host = $self->_host($args{"address"}) or return $self->details("mx") 0 2 2
248  
249 2 ▶ $self->_select_dns_method unless $DNS_Method;
Branch: 1 branch
BranchTrueFalseTotal
unless $Email::Valid::DNS_Method 0 2 2
250  
251 2 ▶ if ($DNS_Method eq 'Net::DNS') {
Branch: 2 branches
BranchTrueFalseTotal
if ($Email::Valid::DNS_Method eq 'Net::DNS') { } 2 0 2
elsif ($Email::Valid::DNS_Method eq 'nslookup') { } 0 0 0
252 2 ▶ print STDERR "using Net::DNS for dns query\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Email::Valid::Debug 0 2 2
253 2 return $self->_net_dns_query( $host );
254 } elsif ($DNS_Method eq 'nslookup') {
255 0 ▶ print STDERR "using nslookup for dns query\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Email::Valid::Debug 0 0 0
256 0 return $self->_nslookup_query( $host );
257 } else {
258 0 croak "unknown DNS method '$DNS_Method'";
259 }
260 }
261  
262 # Purpose: convert address to host
263 # Returns: host
264  
265 sub _host {
266 2 ▶ my $self = shift;
Subroutine _host: called CC=4 SCAR=14.8
267 2 my $addr = shift;
268  
269 2 ▶ $addr = $addr->address if (blessed($addr) && $addr->isa('Mail::Address'));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($addr) and $addr->isa("Mail::Address") 0 2 2
Condition33%1/3
builtin::blessed($addr) and $addr->isa("Mail::Address")
!ll&&!rl&&r
200
MC/DC0%0/2
builtin::blessed($addr) and $addr->isa("Mail::Address")
builtin::blessed($addr) $addr->isa("Mail::Address")
Truth table33%1/3
A and B
A= builtin::blessed($addr)B= $addr->isa("Mail::Address")
ABresult
0X0
100
111
270  
271 2 ▶ my $host = ($addr =~ /^.*@(.*)$/ ? $1 : $addr);
Branch: 1 branch
BranchTrueFalseTotal
$addr =~ /^.*@(.*)$/ ? : 0 2 2
272 2 $host =~ s/\s+//g;
273  
274 # REMOVE BRACKETS IF IT'S A DOMAIN-LITERAL
275 # RFC822 3.4.6
276 # Square brackets ("[" and "]") are used to indicate the
277 # presence of a domain-literal, which the appropriate
278 # name-domain is to use directly, bypassing normal
279 # name-resolution mechanisms.
280 2 $host =~ s/(^\[)|(\]$)//g;
281 2 $host;
282 }
283  
284 # Purpose: Fix common addressing errors
285 # Returns: Possibly modified address
286 sub _fudge {
287 1 ▶ my $self = shift;
Subroutine _fudge: called CC=3 SCAR=11.3
288 1 my $addr = shift;
289  
290 1 ▶ $addr =~ s/\s+//g if $addr =~ /aol\.com$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /aol\.com$/i 1 0 1
291 1 ▶ $addr =~ s/,/./g if $addr =~ /compuserve\.com$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /compuserve\.com$/i 0 1 1
292 1 $addr;
293 }
294  
295 # Purpose: Special address restrictions on a per-domain basis.
296 # Caveats: These organizations may change their rules at any time.
297 sub _local_rules {
298 0 ▶ my $self = shift;
Subroutine _local_rules: not called CC=1 SCAR=6.9
299 0 my($user, $host) = @_;
300  
301 0 1;
302 }
303  
304 sub _valid_local_part {
305 24 ▶ my ($self, $localpart) = @_;
Subroutine _valid_local_part: called CC=3 SCAR=11.0
306  
307 24 ▶ return 0 unless defined $localpart and length $localpart <= 64;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $localpart and length $localpart <= 64 1 23 24
Condition66%2/3
defined $localpart and length $localpart <= 64
!ll&&!rl&&r
0123
MC/DC50%1/2
defined $localpart and length $localpart <= 64
defined $localpart length $localpart <= 64
Truth table66%2/3
A and B
A= defined $localpartB= length $localpart <= 64
ABresult
0X0
100
111
308  
309 23 return 1;
310 }
311  
312 sub _valid_domain_parts {
313 21 ▶ my ($self, $string) = @_;
Subroutine _valid_domain_parts: called CC=6 SCAR=18.8
314  
315 21 ▶ return unless $string and length $string <= 255;
Branch: 1 branch
BranchTrueFalseTotal
unless $string and length $string <= 255 0 21 21
Condition33%1/3
$string and length $string <= 255
!ll&&!rl&&r
0021
MC/DC0%0/2
$string and length $string <= 255
$string length $string <= 255
Truth table33%1/3
A and B
A= $stringB= length $string <= 255
ABresult
0X0
100
111
316 21 ▶ return if $string =~ /\.\./;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /\.\./ 0 21 21
317 21 my @labels = split /\./, $string;
318  
319 21 for my $label (@labels) {
320 43 ▶ return 0 unless $self->_is_domain_label($label);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_domain_label($label) 3 40 43
321 }
322 18 return scalar @labels;
323 }
324  
325 sub _is_domain_label {
326 43 ▶ my ($self, $string) = @_;
Subroutine _is_domain_label: called CC=2 SCAR=6.9
327 43 ▶ return unless $string =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /\A [A-Z0-9] # must start with an alnum (?: [-A-Z0-9]* # then maybe a dash or alnum [A-Z0-9] # finally ending with an alnum )? # lather, rinse, repeat \z/xi 3 40 43
328 [A-Z0-9] # must start with an alnum
329 (?:
330 [-A-Z0-9]* # then maybe a dash or alnum
331 [A-Z0-9] # finally ending with an alnum
332 )? # lather, rinse, repeat
333 \z/ix;
334 40 return 1;
335 }
336  
337 # Purpose: Put an address through a series of checks to determine
338 # whether it should be considered valid.
339 sub address {
340 28 ▶ my $self = shift;
Subroutine address: called CC=24 SCAR=33.9
Pod documented
341 28 my %args = $self->_rearrange([qw( address fudge mxcheck tldcheck fqdn
342 local_rules )], \@_);
343  
344 28 ▶ my $addr = $args{address} or return $self->details('rfc822');
Branch: 1 branch
BranchTrueFalseTotal
my $addr = $args{"address"} or return $self->details("rfc822") 0 28 28
345 28 ▶ $addr = $addr->address if (blessed($addr) && $addr->isa('Mail::Address'));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($addr) and $addr->isa("Mail::Address") 2 26 28
Condition66%2/3
builtin::blessed($addr) and $addr->isa("Mail::Address")
!ll&&!rl&&r
2602
MC/DC50%1/2
builtin::blessed($addr) and $addr->isa("Mail::Address")
builtin::blessed($addr) $addr->isa("Mail::Address")
Truth table66%2/3
A and B
A= builtin::blessed($addr)B= $addr->isa("Mail::Address")
ABresult
0X0
100
111
346  
347 28 ▶ $addr = $self->_fudge( $addr ) if $args{fudge};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"fudge"} 1 27 28
348 28 ▶ $self->rfc822( -address => $addr ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->rfc822("-address", $addr) or return undef 3 25 28
349  
350 25 ($addr) = Mail::Address->parse( $addr );
351  
352 25 ▶ $addr or return $self->details('rfc822'); # This should never happen
Branch: 1 branch
BranchTrueFalseTotal
$addr or return $self->details("rfc822") 0 25 25
353  
354 25 ▶ if (length($addr->address) > 254) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $addr->address > 254) 1 24 25
355 1 return $self->details('address_too_long');
356 }
357  
358 24 ▶ if ($args{local_rules}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"local_rules"}) 0 24 24
359 0 ▶ $self->_local_rules( $addr->user, $addr->host )
Branch: 1 branch
BranchTrueFalseTotal
$self->_local_rules($addr->user, $addr->host) or return $self->details("local_rules") 0 0 0
360 or return $self->details('local_rules');
361 }
362  
363 24 ▶ if ($args{localpart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"localpart"}) 24 0 24
364 24 ▶ $self->_valid_local_part($addr->user) > 0
Branch: 1 branch
BranchTrueFalseTotal
$self->_valid_local_part($addr->user) > 0 or return $self->details("localpart") 1 23 24
365 or return $self->details('localpart');
366 }
367  
368 23 ▶ my $ip_ok = $args{allow_ip} && $addr->host =~ /\A\[
Condition100%3/3
$args{'allow_ip'} && $addr->host =~ /\A\[ (?:[0-9]{1,3}\.){3}[0-9]{1,3} /x
!ll&&!rl&&r
1211
MC/DC100%2/2
$args{'allow_ip'} && $addr->host =~ /\A\[ (?:[0-9]{1,3}\.){3}[0-9]{1,3} /x
$args{'allow_ip'} $addr->host =~ /\A\[ (?:[0-9]{1,3}\.){3}[0-9]{1,3} /x
Truth table100%3/3
A && B
A= $args{'allow_ip'}B= $addr->host =~ /\A\[ (?:[0-9]{1,3}\.){3}[0-9]{1,3} /x
ABresult
0X0
100
111
369 (?:[0-9]{1,3}\.){3}[0-9]{1,3}
370 /x;
371  
372 23 ▶ if (! $ip_ok && $args{fqdn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ip_ok and $args{"fqdn"}) 21 2 23
Condition100%3/3
not $ip_ok and $args{"fqdn"}
!ll&&!rl&&r
1121
MC/DC100%2/2
not $ip_ok and $args{"fqdn"}
not $ip_ok $args{"fqdn"}
Truth table100%3/3
not(A) and B
A= not $ip_okB= $args{"fqdn"}
ABresult
0X0
100
111
373 21 my $domain_parts = $self->_valid_domain_parts($addr->host);
374  
375 21 ▶ return $self->details('fqdn')
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_ok or $domain_parts and $domain_parts > 1 5 16 21
Condition83%5/6
$ip_ok or $domain_parts and $domain_parts > 1
l!l&&r!l&&!r
0183
$domain_parts and $domain_parts > 1
!ll&&!rl&&r
3216
MC/DC33%1/3
$ip_ok or $domain_parts and $domain_parts > 1
$ip_ok $domain_parts $domain_parts > 1
Truth table50%2/4
A or B and C
A= $ip_okB= $domain_partsC= $domain_parts > 1
ABCresult
1XX1
0111
00X0
0100
376 unless $ip_ok || ($domain_parts && $domain_parts > 1);
377 }
378  
379 18 ▶ if (! $ip_ok && $args{tldcheck}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ip_ok and $args{"tldcheck"}) 0 18 18
Condition66%2/3
not $ip_ok and $args{"tldcheck"}
!ll&&!rl&&r
1170
MC/DC0%0/2
not $ip_ok and $args{"tldcheck"}
not $ip_ok $args{"tldcheck"}
Truth table66%2/3
not(A) and B
A= not $ip_okB= $args{"tldcheck"}
ABresult
0X0
100
111
380 0 ▶ $self->tld( $addr->host ) or return $self->details('tldcheck');
Branch: 1 branch
BranchTrueFalseTotal
$self->tld($addr->host) or return $self->details("tldcheck") 0 0 0
381 }
382  
383 18 ▶ if ($args{mxcheck}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"mxcheck"}) 2 16 18
384 # I'm not sure this ->details call is needed, but I'll test for it later.
385 # The whole ->details thing is... weird. -- rjbs, 2006-06-08
386 2 ▶ $self->mx( $addr->host ) or return $self->details('mxcheck');
Branch: 1 branch
BranchTrueFalseTotal
$self->mx($addr->host) or return $self->details("mxcheck") 0 2 2
387 }
388  
389 18 ▶ return (wantarray ? ($addr->address, $addr) : $addr->address);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 18 18
390 }
391  
392 sub AUTOLOAD {
393 1 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=5 SCAR=21.7
394 1 ▶ my $type = ref($self) || die "$self is not an object";
Condition50%1/2
ref $self || die("$self is not an object")
l!l
10
MC/DC0%0/1
ref $self || die("$self is not an object")
ref $self
Truth table50%1/2
A
A= ref $self
Aresult
11
00
395 1 my $name = our $AUTOLOAD;
396  
397 1 $name =~ s/.*://;
398 1 ▶ return if $name eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "DESTROY" 1 0 1
399 0 ▶ die "unknown autoload name '$name'" unless $AUTOLOAD{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $Email::Valid::AUTOLOAD{$name} 0 0 0
400  
401 0 ▶ return (@_ ? $self->{$name} = shift : $self->{$name});
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
402 }
403  
404 # Regular expression built using Jeffrey Friedl's example in
405 # _Mastering Regular Expressions_ (http://www.ora.com/catalog/regexp/).
406  
407 3 $RFC822PAT = <<'EOF';
408 [\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\
409 xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xf
410 f\n\015()]*)*\)[\040\t]*)*(?:(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\x
411 ff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|"[^\\\x80-\xff\n\015
412 "]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[\040\t]*(?:\([^\\\x80-\
413 xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80
414 -\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*
415 )*(?:\.[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\
416 \\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\
417 x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x8
418 0-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|"[^\\\x80-\xff\n
419 \015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[\040\t]*(?:\([^\\\x
420 80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^
421 \x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040
422 \t]*)*)*@[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([
423 ^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\
424 \\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\
425 x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-
426 \xff\n\015\[\]]|\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()
427 ]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\
428 x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\04
429 0\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\
430 n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\
431 015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?!
432 [^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\
433 ]]|\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\
434 x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\01
435 5()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)*|(?:[^(\040)<>@,;:".
436 \\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]
437 )|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[^
438 ()<>@,;:".\\\[\]\x80-\xff\000-\010\012-\037]*(?:(?:\([^\\\x80-\xff\n\0
439 15()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][
440 ^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)|"[^\\\x80-\xff\
441 n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[^()<>@,;:".\\\[\]\
442 x80-\xff\000-\010\012-\037]*)*<[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?
443 :(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-
444 \xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:@[\040\t]*
445 (?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015
446 ()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()
447 ]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\0
448 40)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\
449 [^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\
450 xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*
451 )*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80
452 -\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x
453 80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t
454 ]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\
455 \[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])
456 *\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x
457 80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80
458 -\xff\n\015()]*)*\)[\040\t]*)*)*(?:,[\040\t]*(?:\([^\\\x80-\xff\n\015(
459 )]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\
460 \x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*@[\040\t
461 ]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\0
462 15()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015
463 ()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(
464 \040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|
465 \\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80
466 -\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()
467 ]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x
468 80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^
469 \x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040
470 \t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".
471 \\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff
472 ])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\
473 \x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x
474 80-\xff\n\015()]*)*\)[\040\t]*)*)*)*:[\040\t]*(?:\([^\\\x80-\xff\n\015
475 ()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\
476 \\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)?(?:[^
477 (\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-
478 \037\x80-\xff])|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\
479 n\015"]*)*")[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|
480 \([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))
481 [^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80-\xff
482 \n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\x
483 ff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(
484 ?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\
485 000-\037\x80-\xff])|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\
486 xff\n\015"]*)*")[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\x
487 ff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)
488 *\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)*@[\040\t]*(?:\([^\\\x80-\x
489 ff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-
490 \xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)
491 *(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\
492 ]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])*\]
493 )[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-
494 \xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\x
495 ff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(
496 ?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80
497 -\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<
498 >@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x8
499 0-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])*\])[\040\t]*(?:
500 \([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]
501 *(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)
502 *\)[\040\t]*)*)*>)
503 EOF
504  
505 3 $RFC822PAT =~ s/\n//g;
506  
507 3 1;
508  
509  
510 #pod =head1 SYNOPSIS
511 #pod
512 #pod use Email::Valid;
513 #pod my $address = Email::Valid->address('maurice@hevanet.com');
514 #pod print ($address ? 'yes' : 'no');
515 #pod
516 #pod =head1 DESCRIPTION
517 #pod
518 #pod This module determines whether an email address is well-formed, and
519 #pod optionally, whether a mail host exists for the domain.
520 #pod
521 #pod Please note that there is no way to determine whether an
522 #pod address is deliverable without attempting delivery
523 #pod (for details, see L<perlfaq 9|http://perldoc.perl.org/perlfaq9.html#How-do-I-check-a-valid-mail-address>).
524 #pod
525 #pod =head1 PREREQUISITES
526 #pod
527 #pod This module requires perl 5.004 or later and the L<Mail::Address> module.
528 #pod Either the L<Net::DNS> module or the nslookup utility is required
529 #pod for DNS checks. The L<Net::Domain::TLD> module is required to check the
530 #pod validity of top level domains.
531 #pod
532 #pod =head1 METHODS
533 #pod
534 #pod Every method which accepts an C<< <ADDRESS> >> parameter may
535 #pod be passed either a string or an instance of the Mail::Address
536 #pod class. All errors raise an exception.
537 #pod
538 #pod =over 4
539 #pod
540 #pod =item new ( [PARAMS] )
541 #pod
542 #pod This method is used to construct an Email::Valid object.
543 #pod It accepts an optional list of named parameters to
544 #pod control the behavior of the object at instantiation.
545 #pod
546 #pod The following named parameters are allowed. See the
547 #pod individual methods below for details.
548 #pod
549 #pod -mxcheck
550 #pod -tldcheck
551 #pod -fudge
552 #pod -fqdn
553 #pod -allow_ip
554 #pod -local_rules
555 #pod
556 #pod =item mx ( <ADDRESS>|<DOMAIN> )
557 #pod
558 #pod This method accepts an email address or domain name and determines
559 #pod whether a DNS record (A or MX) exists for it.
560 #pod
561 #pod The method returns true if a record is found and undef if not.
562 #pod
563 #pod Either the Net::DNS module or the nslookup utility is required for
564 #pod DNS checks. Using Net::DNS is the preferred method since error
565 #pod handling is improved. If Net::DNS is available, you can modify
566 #pod the behavior of the resolver (e.g. change the default tcp_timeout
567 #pod value) by manipulating the global L<Net::DNS::Resolver> instance stored in
568 #pod C<$Email::Valid::Resolver>.
569 #pod
570 #pod =item rfc822 ( <ADDRESS> )
571 #pod
572 #pod This method determines whether an address conforms to the RFC822
573 #pod specification (except for nested comments). It returns true if it
574 #pod conforms and undef if not.
575 #pod
576 #pod =item fudge ( <TRUE>|<FALSE> )
577 #pod
578 #pod Specifies whether calls to address() should attempt to correct
579 #pod common addressing errors. Currently, this results in the removal of
580 #pod spaces in AOL addresses, and the conversion of commas to periods in
581 #pod Compuserve addresses. The default is false.
582 #pod
583 #pod =item allow_ip ( <TRUE>|<FALSE> )
584 #pod
585 #pod Specifies whether a "domain literal" is acceptable as the domain part. That
586 #pod means addresses like: C<rjbs@[1.2.3.4]>
587 #pod
588 #pod The checking for the domain literal is stricter than the RFC and looser than
589 #pod checking for a valid IP address, I<but this is subject to change>.
590 #pod
591 #pod The default is true.
592 #pod
593 #pod =item fqdn ( <TRUE>|<FALSE> )
594 #pod
595 #pod Specifies whether addresses passed to address() must contain a fully
596 #pod qualified domain name (FQDN). The default is true.
597 #pod
598 #pod B<Please note!> FQDN checks only occur for non-domain-literals. In other
599 #pod words, if you have set C<allow_ip> and the address ends in a bracketed IP
600 #pod address, the FQDN check will not occur.
601 #pod
602 #pod =item tld ( <ADDRESS> )
603 #pod
604 #pod This method determines whether the domain part of an address is in a
605 #pod recognized top-level domain.
606 #pod
607 #pod B<Please note!> TLD checks only occur for non-domain-literals. In other
608 #pod words, if you have set C<allow_ip> and the address ends in a bracketed IP
609 #pod address, the TLD check will not occur.
610 #pod
611 #pod =item local_rules ( <TRUE>|<FALSE> )
612 #pod
613 #pod Specifies whether addresses passed to address() should be tested
614 #pod for domain specific restrictions. Currently, this is limited to
615 #pod certain AOL restrictions that I'm aware of. The default is false.
616 #pod
617 #pod =item mxcheck ( <TRUE>|<FALSE> )
618 #pod
619 #pod Specifies whether addresses passed to address() should be checked
620 #pod for a valid DNS entry. The default is false.
621 #pod
622 #pod =item tldcheck ( <TRUE>|<FALSE> )
623 #pod
624 #pod Specifies whether addresses passed to address() should be checked
625 #pod for a valid top level domains. The default is false.
626 #pod
627 #pod =item address ( <ADDRESS> )
628 #pod
629 #pod This is the primary method which determines whether an email
630 #pod address is valid. Its behavior is modified by the values of
631 #pod mxcheck(), tldcheck(), local_rules(), fqdn(), and fudge(). If the address
632 #pod passes all checks, the (possibly modified) address is returned as
633 #pod a string. Otherwise, undef is returned.
634 #pod In a list context, the method also returns an instance of the
635 #pod Mail::Address class representing the email address.
636 #pod
637 #pod =item details ()
638 #pod
639 #pod If the last call to address() returned undef, you can call this
640 #pod method to determine why it failed. Possible values are:
641 #pod
642 #pod rfc822
643 #pod localpart
644 #pod local_rules
645 #pod fqdn
646 #pod mxcheck
647 #pod tldcheck
648 #pod
649 #pod If the class is not instantiated, you can get the same information
650 #pod from the global C<$Email::Valid::Details>.
651 #pod
652 #pod =back
653 #pod
654 #pod =head1 EXAMPLES
655 #pod
656 #pod Let's see if the address 'maurice@hevanet.com' conforms to the
657 #pod RFC822 specification:
658 #pod
659 #pod print (Email::Valid->address('maurice@hevanet.com') ? 'yes' : 'no');
660 #pod
661 #pod Additionally, let's make sure there's a mail host for it:
662 #pod
663 #pod print (Email::Valid->address( -address => 'maurice@hevanet.com',
664 #pod -mxcheck => 1 ) ? 'yes' : 'no');
665 #pod
666 #pod Let's see an example of how the address may be modified:
667 #pod
668 #pod $addr = Email::Valid->address('Alfred Neuman <Neuman @ foo.bar>');
669 #pod print "$addr\n"; # prints Neuman@foo.bar
670 #pod
671 #pod Now let's add the check for top level domains:
672 #pod
673 #pod $addr = Email::Valid->address( -address => 'Neuman@foo.bar',
674 #pod -tldcheck => 1 );
675 #pod print "$addr\n"; # doesn't print anything
676 #pod
677 #pod Need to determine why an address failed?
678 #pod
679 #pod unless(Email::Valid->address('maurice@hevanet')) {
680 #pod print "address failed $Email::Valid::Details check.\n";
681 #pod }
682 #pod
683 #pod If an error is encountered, an exception is raised. This is really
684 #pod only possible when performing DNS queries. Trap any exceptions by
685 #pod wrapping the call in an eval block:
686 #pod
687 #pod eval {
688 #pod $addr = Email::Valid->address( -address => 'maurice@hevanet.com',
689 #pod -mxcheck => 1 );
690 #pod };
691 #pod warn "an error was encountered: $@" if $@;
692 #pod
693 #pod =head1 CREDITS
694 #pod
695 #pod Significant portions of this module are based on the ckaddr program
696 #pod written by Tom Christiansen and the RFC822 address pattern developed
697 #pod by Jeffrey Friedl. Neither were involved in the construction of this
698 #pod module; all errors are mine.
699 #pod
700 #pod Thanks very much to the following people for their suggestions and
701 #pod bug fixes:
702 #pod
703 #pod Otis Gospodnetic <otis@DOMINIS.com>
704 #pod Kim Ryan <kimaryan@ozemail.com.au>
705 #pod Pete Ehlke <pde@listserv.music.sony.com>
706 #pod Lupe Christoph
707 #pod David Birnbaum
708 #pod Achim
709 #pod Elizabeth Mattijsen (liz@dijkmat.nl)
710 #pod
711 #pod =head1 SEE ALSO
712 #pod
713 #pod L<Mail::Address>, L<Net::DNS>, L<Net::Domain::TLD>, L<perlfaq9|https://metacpan.org/pod/distribution/perlfaq/lib/perlfaq9.pod>
714 #pod
715 #pod L<RFC822|https://www.ietf.org/rfc/rfc0822.txt> -
716 #pod standard for the format of ARPA internet text messages.
717 #pod Superseded by L<RFC2822|https://www.ietf.org/rfc/rfc2822.txt>.
718 #pod
719 #pod =cut
720  
721 __END__
↑ Summary