File Coverage

blib/lib/String/Validator/Email.pm
Criterion Covered Total %
statement 90 91 98.9
branch 34 36 94.4
condition 1 2 50.0
subroutine 14 14 100.0
pod 3 3 100.0
total 142 146 97.2


line stmt bran cond sub pod time code
1             package String::Validator::Email;
2              
3 7     7   199455 use 5.006;
  7         32  
  7         309  
4 7     7   47 use strict;
  7         14  
  7         261  
5 7     7   42 use warnings;
  7         17  
  7         431  
6 7     7   7273 use String::Validator::Common ;
  7         11909  
  7         294  
7 7     7   7483 use Regexp::Common qw /net/;
  7         59856  
  7         43  
8 7     7   42223 use Net::DNS;
  7         930673  
  7         1194  
9 7     7   8921 use Email::Valid;
  7         322314  
  7         464  
10 7     7   7615 use Email::Address;
  7         69589  
  7         1107  
11              
12             our $VERSION = '0.98';
13              
14             =head1 VERSION
15              
16             Version 0.98
17              
18             =cut
19              
20             sub new {
21 10     10 1 5894 my $class = shift ;
22 10         45 my $self = { @_ } ;
23 7     7   102 use base ( 'String::Validator::Common' ) ;
  7         16  
  7         7332  
24 10 50       56 unless ( defined $self->{ min_len } )
25 10         39 { $self->{ min_len } = 6 ; }
26 10 100       43 unless ( defined $self->{ max_len } )
27 9         27 { $self->{ max_len } = 64 ; }
28             # allow_ip wont work with fqdn or tldcheck
29 10 100       66 if ( $self->{ allow_ip } ) {
30 1         3 $self->{ mxcheck } = 0 ;
31 1         2 $self->{ fqdn } = 0 ;
32 1         3 $self->{ tldcheck } = 0 ;
33             }
34             # Converts String::Validator Switches to Email::Valid Switches.
35 10         34 my %switchhash = () ;
36 10         36 for ( qw / tldcheck fqdn allow_ip /) {
37 30         67 my $dashstr = '-' . $_ ;
38 30 100       110 if ( defined $self->{ $_ } )
39 9         33 { $switchhash{ $dashstr } = $self->{ $_ } }
40             }
41 10 100       43 unless( defined $self->{ tldcheck } ) {
42 6         20 $switchhash{ '-tldcheck' } = 1 }
43 10         37 $self->{ switchhash } = \%switchhash ;
44 10 100       55 if( $self->{ mxcheck } ) {
45 1         3 $self->{ fqdn } = 1 ; #before mx, must pass fqdn.
46 1         26 $self->{ NetDNS } = Net::DNS::Resolver->new;
47             }
48            
49 10         112 bless $self, $class ;
50 10         116 return $self ;
51             }
52              
53             # Email::Valid has very terse error codes.
54             # Not an OO method must use &
55             sub _expound {
56 10   50 10   93 my $errors = shift || '';
57 10         101 my $string = shift ;
58 10         82 my $expounded = '' ;
59 10 100       57 if ( $errors =~ m/fqdn/ ) {
60 5         17 $expounded .= 'Does not appear to contain a Fully Qualified Domain Name.' }
61 10 100       38 if ( $errors =~ m/rfc822/ ) {
62 1 50       6 unless ( $string =~ /\@/ ) { $expounded .= 'Missing @ symbol' }
  1         3  
63             else {
64 0         0 $expounded .= 'Does not look like an email address.' }
65             }
66 10 100       43 if ( $errors =~ m/tld/ ) {
67 1         4 $expounded .=
68             'The TLD (Top Level Domain) is not recognized.' ;
69             }
70 10 100       45 if ( $errors =~ m/mx/ ) {
71 3         18 $expounded .= "Mail Exchanger for $string " .
72             "is missing from Public DNS. Mail cannot be delivered." ;
73             }
74 10         47 return $expounded ;
75             }
76              
77             sub _rejectip {
78 12     12   29 my $self = shift ;
79 12 100       143 if ( $self->{ string } =~ /$RE{net}{IPv4}/ ) {
80 2         545 $self->IncreaseErr(
81             "$self->{ string } Looks like it contains an IP Address." ) }
82             }
83              
84             sub Check{
85 21     21 1 21385 my ( $self, $string1, $string2 ) = @_ ;
86             #not standard hashvar so not inited by inherited method in CheckCommon.
87 21         81 $self->{ expounded } = '' ;
88 21 100       121 if ( $self->CheckCommon( $string1, $string2 ) ) {
89 1         28 return $self->{ error } }
90 20         909 my %switchhash = %{ $self->{switchhash} } ;
  20         108  
91 20         95 $switchhash{ -address } = $self->{ string } ;
92 20         217 my $addr = Email::Valid->address( %switchhash );
93 20 100       59505 unless ( $addr ) {
94 7         62 $self->IncreaseErr( $Email::Valid::Details ) ;
95             $self->{ expounded } = &_expound(
96 7         85 $Email::Valid::Details, $self->{ string } ) ;
97             }
98             else {
99 13 100       124 unless ( $self->{ allow_ip } ) {
100 12         53 $self->_rejectip() }
101             }
102             # Need maildomain for mxcheck.
103 20         2698 ( my $discard, $self->{maildomain} ) = split( /\@/, $self->{ string } );
104 20         176 $self->{maildomain} =~ tr/\>//d ; #clean out unwanted chars.
105 20 100       74 if ( $self->{ mxcheck } ) {
106 6 100       24 if ( $self->{ error } == 0 ) {
107 5         14 my $res = $self->{ NetDNS };
108 5 100       106 unless ( mx( $res, $self->{ maildomain } ) ) {
109 3         25556 $self->IncreaseErr( "MX" ) ;
110             $self->{ expounded } =
111 3         43 &_expound( 'mx', $self->{ maildomain} ) ;
112             }
113             }
114             }
115 20         177348 return $self->{ error } ;
116             }
117              
118             sub Expound {
119 6     6 1 15099 my $self = shift ;
120 6         58 return $self->{ expounded } ;
121             }
122              
123             =pod
124              
125             =head1 NAME
126              
127             String::Validator::Email - Check if a string is an email address.
128              
129             =head1 SYNOPSIS
130              
131             String::Validator::Email is part of the String Validator Collection. It will
132             check a string against any number of email validation rules, and optionally
133             against a second string (as in a confirmation box on a webform).
134              
135             =head1 String::Validator Methods and Usage
136              
137             Provides and conforms to all of the standard String::Validator methods,
138             please see String::Validator for general documentation, and
139             String::Validator::Common for information on the base String::Validator Class.
140              
141             =head1 Methods Specific to String::Validator::Email
142              
143             =head2 Parameters to New with (default) behaviour.
144              
145             mxcheck (OFF) : Perform MX Lookup for Domain Given.
146             tldcheck (ON ) : Validate TLD against a List.
147             fqdn (ON ) : Require a Fully Qualified Domain Name.
148             allow_ip (OFF) : Allow @[ip] (forces tld & fqdn off.)
149             min_len (OFF)
150             max_len (OFF)
151              
152             Important notes -- SVE uses Email::Valid, however, tldcheck is defaulted to on.
153             The choice to turn tldcheck should be obvious. The fudge and local_rules
154             options are specific to aol and compuserve, and are not supported.
155             Finally mxcheck is not tried if there is already an error, since Email::Valid's
156             DNS check does not work, that is performed directly through Net::DNS.
157              
158             =head2 Expound
159              
160             Email::Valid provides very terse errors, Expound provides errors more appropriate
161             for returning to an end user.
162              
163             =head1 Example
164              
165             use String::Validator::Email ;
166             my $Validator = String::Validator::Email->new() ;
167             if ( $Validator->Is_Valid( 'real@address.com' ) { say "good" }
168             if ( $Validator->IsNot_Valid( 'bad@address=com') { say $Validator->Errstr() }
169              
170             =head1 ToDo
171              
172             The major TO DO items are to replace Email::Valid methods, return an Email::Address object and to use it to create methods for returning information
173             from an extended mail string like: Jane Brown .
174              
175             =head1 AUTHOR
176              
177             John Karr, C<< >>
178              
179             =head1 BUGS
180              
181             Please report any bugs or feature requests to C, or through
182             the web interface at L. I will be notified, and then you'll
183             automatically be notified of progress on your bug as I make changes.
184              
185              
186              
187              
188             =head1 SUPPORT
189              
190             You can find documentation for this module with the perldoc command.
191              
192             perldoc String::Validator::Email
193              
194              
195             You can also look for information at:
196              
197             =over 4
198              
199             =item * RT: CPAN's request tracker (report bugs here)
200              
201             L
202              
203             =item * AnnoCPAN: Annotated CPAN documentation
204              
205             L
206              
207             =item * CPAN Ratings
208              
209             L
210              
211             =item * Search CPAN
212              
213             L
214              
215             =back
216              
217              
218             =head1 ACKNOWLEDGEMENTS
219              
220              
221             =head1 LICENSE AND COPYRIGHT
222              
223             Copyright 2012 John Karr.
224              
225             This program is free software; you can redistribute it and/or modify
226             it under the terms of the GNU General Public License as published by
227             the Free Software Foundation; version 3 or at your option
228             any later version.
229              
230             This program is distributed in the hope that it will be useful,
231             but WITHOUT ANY WARRANTY; without even the implied warranty of
232             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
233             GNU General Public License for more details.
234              
235             A copy of the GNU General Public License is available in the source tree;
236             if not, write to the Free Software Foundation, Inc.,
237             59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
238              
239              
240             =cut
241              
242             1; # End of String::Validator::Email