File Coverage

blib/lib/Data/Validate/Mailbox.pm
Criterion Covered Total %
statement 11 30 36.6
branch 0 14 0.0
condition n/a
subroutine 4 6 66.6
pod 2 2 100.0
total 17 52 32.6


line stmt bran cond sub pod time code
1             package Data::Validate::Mailbox;
2              
3 1     1   66464 use 5.006;
  1         4  
4 1     1   5 use strict;
  1         2  
  1         20  
5 1     1   507 use Net::DNS;
  1         100197  
  1         119  
6 1     1   604 use Net::SMTP;
  1         62833  
  1         405  
7              
8             =head1 NAME
9              
10             Data::Validate::Mailbox - Verify that the given mailbox exists
11              
12             =head1 VERSION
13              
14             Version 0.10
15              
16             =cut
17              
18             our $VERSION = '0.10';
19              
20              
21             =head1 SYNOPSIS
22              
23             Verify that the given mailbox exists.
24              
25             If you find any issues in using the module, please don't hesitate to email me: Jefp@gmx.de
26              
27              
28             use Data::Validate::Mailbox;
29              
30             my $mbx = Data::Validate::Mailbox->new;
31              
32             # or,
33             my $mbx = Data::Validate::Mailbox->new(debug => 1,
34             localhost => 'foo.org',
35             localuser => 'user@foo.org',
36             );
37              
38             my $res;
39             $res = $mbx->validate('tester@gmx.de');
40             $res = $mbx->validate('tester@gmail.com');
41             $res = $mbx->validate('tester@orange.fr');
42             $res = $mbx->validate('tester@hotmail.com');
43              
44             # 1 means existing, 0 means non-existing
45             print $res;
46              
47              
48             Please note,
49              
50             1. This module just uses Net::SMTP to try to deliver messages to peer MTA. If the remote mailbox doesn't exist, peer MTA will return a message such as "mailbox unavailable".
51              
52             2. Some email providers don't behave like above, such as Yahoo/AOL, so this module won't work for them.
53              
54              
55             =head1 SUBROUTINES/METHODS
56              
57             =head2 new
58              
59             New the object.
60              
61             Please note,
62              
63             For many email providers, you have to provide the correct local hostname for sending email to them. The hostname must match:
64              
65             1. It is your valid domain/host name.
66              
67             2. The hostname has an IP address, and a correct PTR for this IP (PTR match back to hostname).
68              
69             3. The domain has valid MX records and/or SPF records.
70              
71             4. The IP has good reputation (not listed in any DNSBL).
72              
73             If you can't send messages to those providers, please setup your right localhost and localuser options in new() method.
74              
75              
76             =head2 validate
77              
78             Validate if the given mailbox exists. Return 1 for existing, 0 for non-existing.
79              
80              
81             =cut
82              
83             sub new {
84 0     0 1   my $class = shift;
85 0           my %args = @_;
86              
87 0           bless \%args,$class;
88             }
89              
90             sub validate {
91 0     0 1   my $self = shift;
92 0           my $mailbox = shift;
93              
94 0 0         my $debug = defined $self->{debug} ? $self->{debug} : 0;
95 0 0         my $localhost = defined $self->{localhost} ? $self->{localhost} : 'bar.org';
96 0 0         my $localuser = defined $self->{localuser} ? $self->{localuser} : 'user'. int(rand(999)) . '@' . $localhost;
97              
98 0           my (undef,$domain) = split/\@/,$mailbox;
99              
100             # Use your own resolver object.
101 0           my $res = Net::DNS::Resolver->new;
102 0           my @mx = mx($res, $domain);
103              
104             #The list will be sorted by preference. Returns an empty list if the query failed or no MX record was found.
105 0 0         if (@mx) {
106 0           my $exchange = $mx[0]->exchange;
107              
108 0 0         my $smtp = Net::SMTP->new($exchange,
109             Hello => $localhost,
110             Timeout => 30,
111             Debug => $debug,
112             ) or die "can't make smtp connection to remote host";
113              
114 0 0         $smtp->mail($localuser) or die $smtp->message();
115              
116 0 0         my $status = $smtp->to($mailbox) ? 1 : 0;
117 0           $smtp->quit;
118              
119 0           return $status;
120              
121             }else {
122 0           die "query failed or no MX record was found";
123             }
124             }
125              
126              
127             =head1 AUTHOR
128              
129             Yonghua Peng, C<< >>
130              
131             =head1 BUGS
132              
133             Please report any bugs or feature requests to C, or through
134             the web interface at L. I will be notified, and then you'll
135             automatically be notified of progress on your bug as I make changes.
136              
137              
138              
139              
140             =head1 SUPPORT
141              
142             You can find documentation for this module with the perldoc command.
143              
144             perldoc Data::Validate::Mailbox
145              
146              
147             You can also look for information at:
148              
149             =over 4
150              
151             =item * RT: CPAN's request tracker (report bugs here)
152              
153             L
154              
155             =item * AnnoCPAN: Annotated CPAN documentation
156              
157             L
158              
159             =item * CPAN Ratings
160              
161             L
162              
163             =item * Search CPAN
164              
165             L
166              
167             =back
168              
169              
170             =head1 ACKNOWLEDGEMENTS
171              
172              
173             =head1 LICENSE AND COPYRIGHT
174              
175             This software is Copyright (c) 2023 by Yonghua Peng.
176              
177             This is free software, licensed under:
178              
179             The Artistic License 2.0 (GPL Compatible)
180              
181              
182             =cut
183              
184             1; # End of Data::Validate::Mailbox