全部博文(1144)
分类:
2008-09-21 19:14:04
#!/usr/bin/perl -w
use Socket;
use strict;
my($mailTo) = 'dave@cs.cf.ac.uk';
my($mailServer) = 'mailhost2.cs.cf.ac.uk';
my($mailFrom) = 'dave@cs.cf.ac.uk';
my($realName) = "Ralph Martin";
my($subject) = 'Test';
my($body) = "Test Line One.nTest Line Two.n";
$main::SIG{'INT'} = 'closeSocket';
my($proto) = getprotobyname("tcp") || 6;
my($port) = getservbyname("SMTP", "tcp") || 25;
my($serverAddr) = (gethostbyname($mailServer))[4];
if (! defined($length)) {
die('gethostbyname failed.');
}
socket(SMTP, AF_INET(), SOCK_STREAM(), $proto)
or die("socket: $!");
$packFormat = 'S n a4 x8'; # Windows 95, SunOs 4.1+
#$packFormat = 'S n c4 x8'; # SunOs 5.4+ (Solaris 2)
connect(SMTP, pack($packFormat, AF_INET(), $port, $serverAddr))
or die("connect: $!");
select(SMTP); $| = 1; select(STDOUT); # use unbuffemiles i/o.
{
my($inpBuf) = '';
recv(SMTP, $inpBuf, 200, 0);
recv(SMTP, $inpBuf, 200, 0);
}
sendSMTP(1, "HELOn");
sendSMTP(1, "MAIL From: <$mailFrom>n");
sendSMTP(1, "RCPT To: <$mailTo>n");
sendSMTP(1, "DATAn");
send(SMTP, "From: $realNamen", 0);
send(SMTP, "Subject: $subjectn", 0);
send(SMTP, $body, 0);
sendSMTP(1, "rn.rn");
sendSMTP(1, "QUITn");
close(SMTP);
sub closeSocket { # close smtp socket on error
close(SMTP);
die("SMTP socket closed due to SIGINTn");
}
sub sendSMTP {
my($debug) = shift;
my($buffer) = @_;
print STDERR ("> $buffer") if $debug;
send(SMTP, $buffer, 0);
recv(SMTP, $buffer, 200, 0);
print STDERR ("< $buffer") if $debug;
return( (split(/ /, $buffer))[0] );
}
This program displays:
> HELO
< 250 sentinel.cs.cf.ac.uk Hello dave@miles.cs.cf.ac.uk
[207.3.100.120], pleased to meet you
> MAIL From:
< 250... Sender ok
> RCPT To:
< 250... Recipient ok
> DATA
< 354 Enter mail, end with "." on a line by itself
>
.
< 250 TAA12656 Message accepted for delivery
> QUIT
< 221 sentinel.cs.cf.ac.uk closing connection
The lines in bold are the commands that were sent to the server. The body of the letter is not shown in the output.