File Coverage

blib/lib/String/Validator/Email.pm
Criterion Covered Total %
statement 93 94 98.9
branch 36 38 94.7
condition 1 2 50.0
subroutine 14 14 100.0
pod 3 3 100.0
total 147 151 97.3


line stmt bran cond sub pod time code
1             package String::Validator::Email;
2             $String::Validator::Email::VERSION = '2.01';
3 7     7   423983 use 5.008;
  7         120  
4 7     7   34 use strict;
  7         13  
  7         151  
5 7     7   30 use warnings;
  7         20  
  7         203  
6 7     7   44 no warnings 'uninitialized';
  7         12  
  7         252  
7 7     7   3122 use String::Validator::Common 2.00;
  7         258864  
  7         234  
8 7     7   3954 use Regexp::Common qw /net/;
  7         29512  
  7         29  
9 7     7   23603 use Net::DNS;
  7         633497  
  7         1792  
10 7     7   4352 use Email::Valid;
  7         214138  
  7         640  
11              
12             # ABSTRACT: String::Validator for checking Email Addresses.
13              
14             my $email_messages = {
15             email_fqdn => 'Does not appear to contain a Fully Qualified Domain Name.',
16             email_rfc822_noat => 'Missing @ symbol',
17             email_rfc822 => 'Does not look like an email address.',
18             email_tld => 'This TLD (Top Level Domain) is not recognized.',
19             email_nomx1 => 'Mail Exchanger for ',
20             email_nomx2 => ' is missing from Public DNS. Mail cannot be delivered.',
21             };
22              
23             sub new {
24 10     10 1 8411 my $class = shift ;
25 10         41 my $self = { @_ } ;
26 7     7   72 use base ( 'String::Validator::Common' ) ;
  7         23  
  7         5822  
27 10 50       50 unless ( defined $self->{ min_len } )
28 10         103 { $self->{ min_len } = 6 ; }
29 10 100       49 unless ( defined $self->{ max_len } )
30 9         26 { $self->{ max_len } = 64 ; }
31             # allow_ip wont work with fqdn or tldcheck
32 10 100       148 if ( $self->{ allow_ip } ) {
33 1         5 $self->{ mxcheck } = 0 ;
34 1         4 $self->{ fqdn } = 0 ;
35 1         4 $self->{ tldcheck } = 0 ;
36             }
37             # Converts String::Validator Switches to Email::Valid Switches.
38 10         82 my %switchhash = () ;
39 10         100 for ( qw / tldcheck fqdn allow_ip /) {
40 30         74 my $dashstr = '-' . $_ ;
41 30 100       91 if ( defined $self->{ $_ } )
42 9         23 { $switchhash{ $dashstr } = $self->{ $_ } }
43             }
44 10 100       35 unless( defined $self->{ tldcheck } ) {
45 6         21 $switchhash{ '-tldcheck' } = 1 }
46 10         27 $self->{ switchhash } = \%switchhash ;
47 10 100       36 if( $self->{ mxcheck } ) {
48 1         3 $self->{ fqdn } = 1 ; #before mx, must pass fqdn.
49 1         17 $self->{ NetDNS } = Net::DNS::Resolver->new;
50             }
51             $self->{messages}
52             = String::Validator::Common::_Messages(
53 10         178 $email_messages, $self->{language}, $self->{custom_messages} );
54 10         591 bless $self, $class ;
55 10         55 return $self ;
56             }
57              
58             # Email::Valid has very terse error codes.
59             # Not an OO method
60             sub _expound {
61 10     10   21 my $self = shift ;
62 10   50     32 my $errors = shift || '';
63 10         16 my $string = shift ;
64 10         21 my $expounded = '' ;
65 10 100       40 if ( $errors =~ m/fqdn/ ) {
66 5         17 $expounded .= $self->{messages}{ email_fqdn } }
67 10 100       35 if ( $errors =~ m/rfc822/ ) {
68 1 50       4 unless ( $string =~ /\@/ ) {
69 1         4 $expounded .= $self->{messages}{ email_rfc822_noat } }
70             else {
71 0         0 $expounded .= $self->{messages}{ email_rfc822 } }
72             }
73 10 100       35 if ( $errors =~ m/tld/ ) {
74 1         8 $string =~ m/\.(.*)$/;
75 1         10 $expounded .= "'$1' : $self->{messages}{ email_tld }" ;
76             }
77 10 100       35 if ( $errors =~ m/mx/ ) {
78             $expounded .= $self->{messages}{ email_nomx1 } . $string .
79 3         13 $self->{messages}{ email_nomx2 } ;
80             }
81 10         34 return $expounded ;
82             }
83              
84             sub _rejectip {
85 12     12   23 my $self = shift ;
86 12 100       115 if ( $self->{ string } =~ /$RE{net}{IPv4}/ ) {
87 2         396 $self->IncreaseErr(
88             "$self->{ string } Looks like it contains an IP Address." ) }
89             }
90              
91             sub Check{
92 21     21 1 9876 my ( $self, $string1, $string2 ) = @_ ;
93             #not standard hashvar so not inited by inherited method in CheckCommon.
94 21         75 $self->{ expounded } = '' ;
95 21 100       109 if ( $self->CheckCommon( $string1, $string2 ) ) {
96 1         31 return $self->{ error } }
97 20         905 my %switchhash = %{ $self->{switchhash} } ;
  20         83  
98 20         54 $switchhash{ -address } = $self->{ string } ;
99 20         33 my $fail = 0;
100 20         121 my $addr = Email::Valid->address( %switchhash );
101 20 100       52542 $fail = $Email::Valid::Details unless $addr;
102 20 100       54 if ( $fail ) {
103 7         42 $self->IncreaseErr( $fail ) ;
104             $self->{ expounded } = $self->_expound(
105 7         74 $fail, $self->{ string } ) ;
106             }
107             else {
108 13 100       38 unless ( $self->{ allow_ip } ) {
109 12         40 $self->_rejectip() }
110             }
111             # Need maildomain for mxcheck.
112 20         1733 ( my $discard, $self->{maildomain} ) = split( /\@/, $self->{ string } );
113 20         60 $self->{maildomain} =~ tr/\>//d ; #clean out unwanted chars.
114 20 100       53 if ( $self->{ mxcheck } ) {
115 6 100       19 if ( $self->{ error } == 0 ) {
116 5 100       30 unless ( mx( $self->{ NetDNS }, $self->{ maildomain } ) ) {
117 3         36457 $self->IncreaseErr( "MX" ) ;
118             $self->{ expounded } =
119 3         32 $self->_expound( 'mx', $self->{ maildomain} ) ;
120             }
121             }
122             }
123 20         26836 return $self->{ error } ;
124             }
125              
126             sub Expound {
127 6     6 1 4533 my $self = shift ;
128 6         46 return $self->{ expounded } ;
129             }
130              
131              
132             1; # End of String::Validator::Email
133              
134             __END__