summaryrefslogtreecommitdiffstats
path: root/check-mail-addr.pl
blob: 4aa1d0b1f3c81462f630ccc70a3be34e05a124da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/perl
use warnings;
use strict;
use IO::Socket::INET;
use File::Basename;
use Net::Domain qw(hostfqdn hostdomain);
use Net::DNS;
use Data::Dumper;

if(@ARGV < 1) {
  print "usage: ", basename($0), " <emailaddress> [<from>]\n";
  exit;
}

autoflush STDOUT 1;

sub post {
  my $socket = shift;
  my $command = shift;
  my $ret;

  print " >> $command";
  print $socket $command;
  while ($ret = $socket->getline) {
    print "<<  ".$ret;
    $ret =~ /[0-9]([- ]).*/;
    last if ($1 eq " ");
  }

}

my $email = $ARGV[0];
my $from = $ARGV[1] ? $ARGV[1] : "";
$email =~ m/.*@(.*)/;
my $domain = $1;

print "Getting MX for $domain\n";
my @mx = mx($domain);

print "Connecting to ".$mx[0]->exchange."\n";
my $socket = IO::Socket::INET->new(
  PeerAddr => $mx[0]->exchange,
  PeerPort => 25,
  Proto => "tcp") || die "Error: couldn't create socket.";

print "<<  ".$socket->getline;
post $socket,"EHLO ".hostfqdn()."\r\n";
post $socket, "MAIL FROM:<$from>\r\n";
post $socket, "RCPT TO:<$email>\r\n";
post $socket, "QUIT\r\n";