Brandon wrote: > Good Morning, > > I'm having a total brain dead morning and can't figure out what I'm doing > wrong when it comes to creating a session to an SMTP server with > IO::Socket. The mail system asks me to end DATA portion with a "." on a > single line. When I telnet in, I'm able to end the session with a single > dot on a line by itself. But when I run the script I built below, I have a > problem doing so. Here is the output: > > MESSAGE: 172.18.252.18 > localaddr = 172.18.252.18 > 220 testlab ESMTP CommuniGate Pro 4.2.1 is glad to see you! > 250 testlab we trust you ganymede > 250 [EMAIL PROTECTED] sender accepted > 250 [EMAIL PROTECTED] will leave the Internet > 354 Enter mail, end with "." on a line by itself > > Can anyone see anything that I'm blatantly doing wrong? > > > #!/usr/bin/perl > use strict; > use warnings; > use diagnostics; > use IO::Socket; > > # Turn on autoflush to get around holding data too long to fill a buffer. > $| = 1; > > # Some configuration options > my %cfg; > $cfg{PeerAddr} = "172.18.252.21"; # Destination to test > $cfg{PeerPort} = "25"; # Port > $cfg{Proto} = "tcp"; # Protocol > $cfg{LocalAddr} = "127.0.0.1"; # Default IP Address - this breaks > by default > $cfg{Timeout} = "5"; # Socket Timeout > $cfg{LOGFILE} = "/tmp/mail.log"; # Mail log / debug log > $cfg{IPFILE} = "/tmp/ip.db"; # IP Database of local ip's > $cfg{DEBUG} = 15; # Debug Level > > initialize(); > > > sub initialize { > logger(5, "Intializing $0"); > my $val; > my $msg; > my $ip; > ($val,$ip) = calc_source_ip(); > if ($val) { > logger(15, "calc_source_ip() returned val=$val, msg=$msg"); > } > ($val,$msg) = init_socket($ip); > if (!$val) { > exit; > } > > > }
Check the return codes from these subs - they don't look uniform. > # Figure out what source IP address to send as. This file will be full of > them for testing purposes. > sub calc_source_ip { > my $msg; > my $val; > open (IPDB, "$cfg{IPFILE}") || ($val = 1); > while (<IPDB>) { > if ( /(\d+\.\d+\.\d+\.\d+)/ ) { > $msg = $1; > print "MESSAGE: $msg\n"; > } > } > close (IPDB); > return($val,$msg); > } > # Build the socket, talk to the SMTP Server. Will move this into two > subroutines. > sub init_socket { > my $localaddr = shift; > print "localaddr = $localaddr"; > my $msg; > my $val; > my $subject; > my $rdata; > my $tdata; > my $socket = IO::Socket::INET->new(PeerAddr => $cfg{PeerAddr}, > PeerPort => $cfg{PeerPort}, > Proto => $cfg{Proto}, > Type => SOCK_STREAM, > LocalAddr => $localaddr, > ) > or return(1,$@); > > print "\n"; > > $rdata = <$socket>; > print $rdata; Try printing both sides of the conversation to start with : print 'RCVD: ' . $rdata; print 'SENT: ' . "helo ganymede\n"; You should be checking the return status codes before sending more data. I'll send some code separately for this area that you can modify. > print $socket "helo ganymede\n"; > $rdata = <$socket>; > print $rdata; > print $socket "mail from:[EMAIL PROTECTED]"; > $rdata = <$socket>; > print $rdata; > print $socket "rcpt to:[EMAIL PROTECTED]"; > $rdata = <$socket>; > print $rdata; > print $socket "data\n"; > $rdata = <$socket>; > print $rdata; > print $socket "Subject: This is a test message\n"; > $rdata = <$socket>; > print $rdata; > print $socket "Hello from ganymede. This is to test the spam > engine\n"; > $rdata = <$socket>; > print $rdata; > # this should be the "." on the single line, but what the heck? > print $socket "\r\n.\r\n"; > print $socket "\r\n.\r\n"; > $rdata = <$socket>; > print $rdata; > print $socket "quit\n"; > $rdata =<$socket>; > print $rdata; > return 0; > } > # logging subroutine > sub logger { > my $dval = shift; > my $log = shift; > if ($cfg{DEBUG} >= $dval) { > $log =~ s/\n//g; > open(FOUT, ">>$cfg{LOGFILE}"); > print FOUT (scalar(localtime()) . ": $log\n"); > close(FOUT); > } > } -- ,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED] (_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff) _______________________________________________ Perl-Unix-Users mailing list Perl-Unix-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs