line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Mail::Verify.pm |
2
|
|
|
|
|
|
|
# $Id: Verify.pm,v 1.4 2002/06/09 15:42:32 petef Exp $ |
3
|
|
|
|
|
|
|
# Copyright (c) 2001 Pete Fritchman . All rights |
4
|
|
|
|
|
|
|
# reserved. This program is free software; you can redistribute it and/or |
5
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Mail::Verify; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Mail::Verify - Utility to verify an email address |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Mail::Verify; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 DESCRIPTION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
C provides a function CheckAddress function for verifying email |
20
|
|
|
|
|
|
|
addresses. First the syntax of the email address is checked, then it verifies |
21
|
|
|
|
|
|
|
that there is at least one valid MX server accepting email for the domain. Using |
22
|
|
|
|
|
|
|
L and L a list of MX records (or, falling back on a hosts |
23
|
|
|
|
|
|
|
A record) are checked to make sure at least one SMTP server is accepting |
24
|
|
|
|
|
|
|
connections. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 ERRORS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Here are a list of return codes and what they mean: |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=item 0 |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
The email address appears to be valid. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=item 1 |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
No email address was supplied. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item 2 |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
There is a syntaxical error in the email address. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=item 3 |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
There are no DNS entries for the host in question (no MX records or A records). |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item 4 |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
There are no live SMTP servers accepting connections for this email address. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 EXAMPLES |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
This example shows obtaining an email address from a form field and verifying |
53
|
|
|
|
|
|
|
it. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
use CGI qw/:standard/; |
56
|
|
|
|
|
|
|
use Mail::Verify; |
57
|
|
|
|
|
|
|
my $q = new CGI; |
58
|
|
|
|
|
|
|
[...] |
59
|
|
|
|
|
|
|
my $email = $q->param("emailaddr"); |
60
|
|
|
|
|
|
|
my $email_ck = Mail::Verify::CheckAddress( $email ); |
61
|
|
|
|
|
|
|
if( $email_ck ) { |
62
|
|
|
|
|
|
|
print 'Form input error: Invalid email address.'; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
[...] |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
1
|
|
8440
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
69
|
1
|
|
|
1
|
|
1120
|
use IO::Socket; |
|
1
|
|
|
|
|
40388
|
|
|
1
|
|
|
|
|
8
|
|
70
|
1
|
|
|
1
|
|
1715
|
use Net::DNS; |
|
1
|
|
|
|
|
85310
|
|
|
1
|
|
|
|
|
588
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $VERSION = "0.02"; |
73
|
|
|
|
|
|
|
my $DEBUG = "0"; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
0
|
0
|
|
sub Version { $VERSION } |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub CheckAddress { |
78
|
0
|
|
|
0
|
0
|
|
my $addr = shift; |
79
|
0
|
0
|
|
|
|
|
return 1 unless $addr; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
my ($rr, @mxhosts, @mxrr, $resolver, $dnsquery, $livesmtp, $mx); |
82
|
0
|
|
|
|
|
|
my ($testsmtp); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# First, we check the basic syntax of the email address. |
85
|
0
|
|
|
|
|
|
my ($user, $domain, $extra); |
86
|
0
|
|
|
|
|
|
($user, $domain, $extra) = split /\@/, $addr; |
87
|
0
|
0
|
|
|
|
|
return 2 if $extra; |
88
|
0
|
|
|
|
|
|
@mxrr = Net::DNS::mx( $domain ); |
89
|
|
|
|
|
|
|
# Get the A record for each MX RR |
90
|
0
|
|
|
|
|
|
foreach $rr ( @mxrr ) { |
91
|
0
|
|
|
|
|
|
push( @mxhosts, $rr->exchange ); |
92
|
|
|
|
|
|
|
} |
93
|
0
|
0
|
|
|
|
|
if( ! @mxhosts ) { # check for an A record... |
94
|
0
|
|
|
|
|
|
$resolver = new Net::DNS::Resolver; |
95
|
0
|
|
|
|
|
|
$dnsquery = $resolver->search( $domain ); |
96
|
0
|
0
|
|
|
|
|
return 3 unless $dnsquery; |
97
|
0
|
|
|
|
|
|
foreach $rr ($dnsquery->answer) { |
98
|
0
|
0
|
|
|
|
|
next unless $rr->type eq "A"; |
99
|
0
|
|
|
|
|
|
push( @mxhosts, $rr->address ); |
100
|
|
|
|
|
|
|
} |
101
|
0
|
0
|
|
|
|
|
return 3 unless @mxhosts; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
# DEBUG: see what's in @mxhosts |
104
|
0
|
0
|
|
|
|
|
if( $DEBUG ) { |
105
|
0
|
|
|
|
|
|
foreach( @mxhosts ) { |
106
|
0
|
|
|
|
|
|
$mx = $_; |
107
|
0
|
|
|
|
|
|
print STDERR "\@mxhosts -> $mx\n"; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
# make sure we have a living smtp server on at least one of @mxhosts |
111
|
0
|
|
|
|
|
|
$livesmtp = 0; |
112
|
0
|
|
|
|
|
|
foreach $mx (@mxhosts) { |
113
|
0
|
|
|
|
|
|
$testsmtp = IO::Socket::INET->new( Proto=>"tcp", |
114
|
|
|
|
|
|
|
PeerAddr=> $mx, |
115
|
|
|
|
|
|
|
PeerPort=> 25, |
116
|
|
|
|
|
|
|
Timeout => 10 |
117
|
|
|
|
|
|
|
); |
118
|
0
|
0
|
|
|
|
|
if( $testsmtp ) { |
119
|
0
|
|
|
|
|
|
$livesmtp = 1; |
120
|
0
|
|
|
|
|
|
close $testsmtp; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
0
|
0
|
|
|
|
|
if( ! $livesmtp ) { |
124
|
0
|
|
|
|
|
|
return 4; |
125
|
|
|
|
|
|
|
} |
126
|
0
|
|
|
|
|
|
return 0; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |