Debian Codegraph @

tutorial/6.A.smtp-verify

raw
1 #!/usr/bin/perl -w
2 # Here's something that is of actual use, and should be relatively
3 #portable.
4 # Given an email address and a mail server name check to see if
5 #the address is deliverable on that box. This can be used for address
6 #verification or spam relay checking.
7 #
8 # The point of this is to show how you can take a generic handle
9 #and interact with it. In this example a socket is used.
10
11
12 use Expect;
13 use IO::Socket;
14
15 # $Expect::Debug=1
16 # $Expect::Exp_Internal=1;
17 # $Expect::Log_Stdout=0; # On by default. This does not affect Expect
18 # objects created with Expect->exp_init() however. By default the output of
19 # those handles will not be output to the screen. use $handle->log_stdout(1)
20 # to turn that on after you initialize the handle.
21
22 # Arg. 0 hostname of mail server
23 $mail_server=shift(@ARGV);
24 # Remaining args will be email addresses.
25 @addresses=@ARGV;
26 die "Usage: $0 mail_server address1 [address2 address3.. addressN]\n" unless
27 @addresses;
28
29 # Connect to mail server. This is right out of perldoc IO::Socket.
30 $smtp_sock = IO::Socket::INET->new(PeerAddr => "$mail_server:smtp(25)");
31 die "Couldn't connect to $mail_server, $!" unless defined $smtp_sock;
32
33 # Turn the socket in to an expect object.
34 $smtp_session=Expect->exp_init($smtp_sock);
35 # By default Expect doesn't print out the output of an exp_inited item.
36 # Generally you don't want handles jabbering at you. In this case
37 # we might turn it on so we can watch what happens.
38 #$smtp_session->log_stdout(1);
39
40 # Watch debugging?
41 #$smtp_session->exp_internal(1);
42
43 # Ok, now let's see if the mail server wants to talk to us:
44 $smtp_session->expect(30,'-re','^220.*\n')||die "Bad response from server\n";
45
46 # Cool. Now let's introduce ourselves to the server.
47 # There are many other ways to gain the FQDN of this box. This is mine,
48 # and it's easy. This of course assumes you have uname and that -n returns
49 # your hostname.
50 $my_hostname = `uname -n`; chomp $my_hostname;
51
52 print $smtp_session "HELO $my_hostname\n";
53
54 # My server responds with a 250 + stuff. Presumably that's RFC compliant.
55 # Feel free to go look :-)
56 $smtp_session->expect(30,'-re','^250.*\n')||die "Bad response after HELO\n";
57
58 # Try sending mail.. I should probably use my username rather than user@
59 # but I'm too lazy.
60 print $smtp_session "MAIL FROM:<user\@$my_hostname>\n";
61 $smtp_session->expect(30,'-re','^250.*\n')||die "Bad response after FROM\n";
62
63 # Now to check each address...
64 foreach $address (@addresses) {
65 print $smtp_session "RCPT TO:<$address>\n";
66 # Now check the status...
67 ($match_num,$error,$match)=$smtp_session->expect(30,'-re','^\d\d\d');
68 die "Never got response back after trying RCPT to $address\n" if $error;
69 $status = $match;
70 # Read to the newline so the server will be ready for the next address.
71 # If the server spit back something other than 250 we'll display the
72 # Whole error.
73 ($match_num,$error,$match)=$smtp_session->expect(30,'-re','.*\n');
74 die "Server seems to have hung after trying address $address\n" if $error;
75 if ($status == 250) {
76 $status = "ok\n";
77 } else {
78 $status.=$match;
79 }
80 print "Status of address $address: $status";
81 }
82
83 # Be good citizens, send a quit.
84 print $smtp_session "QUIT\n";
85
86 # At which point it should die nicely.
87 $smtp_session->soft_close();