Here is one way to do it in Perl that I've used for a long time. Actually
there are many better package solutions now but this dates back a very long
time. You should research at CSPAN or check perl.com. Note that this does
not work with the newer 4 digit IP addresses.
There have been at least 2 good articles about mail in the Perl journal:
tpj.com.
Even in this sub there are now much better ways to make the socket and bind
to it.
--
use Socket;
sub sendmail {
my ($from, $reply, $to, $smtp, $subject, $message) = @_;
my ($fromaddr) = $from;
my ($replyaddr) = $reply;
$to =~ s/[ \t]+/, /g; # pack spaces and add comma
$fromaddr =~ s/.*<([^\s]*?)>/$1/; # get from email address
$replyaddr =~ s/.*<([^\s]*?)>/$1/; # get reply email address
$replyaddr =~ s/^([^\s]+).*/$1/; # use first address
$message =~ s/^\./\.\./gm; # handle . as first character
$message =~ s/\r\n/\n/g; # handle line ending
$message =~ s/\n/\r\n/g;
$smtp =~ s/^\s+//g; # remove spaces around $smtp
$smtp =~ s/\s+$//g;
if (!$to) { return -8; }
my($proto) = (getprotobyname('tcp'))[2];
$port = (getservbyname('smtp','tcp'));
my($smtpaddr) = ($smtp =~
/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
? pack('C4',$1,$2,$3,$4)
: (gethostbyname($smtp))[4];
if (!defined($smtpaddr)) { return -1; }
if (!socket(S, AF_INET, SOCK_STREAM, $proto)) { return -2; }
if (!connect(S, pack('Sna4x8', AF_INET, $port, $smtpaddr))) { return -3; }
my($oldfh) = select(S); $| = 1; select($oldfh);
$_ = <S>; if (/^[45]/) { close S; return -4; }
print S "helo localhost\r\n";
$_ = <S>; if (/^[45]/) { close S; return -5; }
print S "mail from: <$fromaddr>\r\n";
$_ = <S>; if (/^[45]/) { close S; return -5; }
foreach (split(/, /, $to)) {
print S "rcpt to: <$_>\r\n";
$_ = <S>; if (/^[45]/) { close S; return -6; }
}
print S "data\r\n";
$_ = <S>; if (/^[45]/) { close S; return -5; }
print S "To: $to\r\n";
print S "From: $from\r\n";
print S "Reply-to: $replyaddr\r\n" if $replyaddr;
print S "X-Mailer: Perl\r\n";
print S "Subject: $subject\r\n\r\n";
print S "$message";
print S "\r\n.\r\n";
$_ = <S>; if (/^[45]/) { close S; return -7; }
print S "quit\r\n";
$_ = <S>;
close S;
return 1;
}
Please visit http://www.ipswitch.com/support/mailing-lists.html
to be removed from this list.