| 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 <petef@databits.net>. 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<Mail::Verify> 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<Net::DNS> and L<IO::Socket> 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 '<h1>Form input error: Invalid email address.</h1>'; | ||||||||||
| 63 | } | ||||||||||
| 64 | [...] | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 69 | 1 | ▶ | use IO::Socket; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 70 | 1 | ▶ | use Net::DNS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | |||||||||||
| 72 | 1 | my $VERSION = "0.02"; | |||||||||
| 73 | 1 | my $DEBUG = "0"; | |||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | sub Version { $VERSION } | ||||||||
|
Subroutine
Version: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub CheckAddress { | ||||||||
|
Subroutine
CheckAddress: not called CC=14 SCAR=53.5
Pod
undocumented
| |||||||||||
| 78 | 0 | my $addr = shift; | |||||||||
| 79 | 0 | ▶ | return 1 unless $addr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | return 2 if $extra; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | if( ! @mxhosts ) { # check for an A record... | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | $resolver = new Net::DNS::Resolver; | |||||||||
| 95 | 0 | $dnsquery = $resolver->search( $domain ); | |||||||||
| 96 | 0 | ▶ | return 3 unless $dnsquery; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | foreach $rr ($dnsquery->answer) { | |||||||||
| 98 | 0 | ▶ | next unless $rr->type eq "A"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | push( @mxhosts, $rr->address ); | |||||||||
| 100 | } | ||||||||||
| 101 | 0 | ▶ | return 3 unless @mxhosts; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | } | ||||||||||
| 103 | # DEBUG: see what's in @mxhosts | ||||||||||
| 104 | 0 | ▶ | if( $DEBUG ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | if( $testsmtp ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | $livesmtp = 1; | |||||||||
| 120 | 0 | close $testsmtp; | |||||||||
| 121 | } | ||||||||||
| 122 | } | ||||||||||
| 123 | 0 | ▶ | if( ! $livesmtp ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 0 | return 4; | |||||||||
| 125 | } | ||||||||||
| 126 | 0 | return 0; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 1 | 1; | |||||||||