# 以cgi传mail

Posted by dino on July 09, 1999 at 15:31:13:

In Reply to: Re: ActiveX DLL 是欲应用于ASP程式中(使用MTS(Mircosoft Transactive Server))? posted by Smith on July 09, 1999 at 09:58:58:

: : 非常应感谢小吴的回答.我的意思是:
: : 若于纲页上输入传送内容, 且指定 E-Mail 位址, 则此 ActiveX DLL 便将此邮件传送出去.
: 若是如此，小弟建议改为cgi来作比较方便。

提供以下source code在unix或linux传mail
sub sendemail {
use Socket;
$TO=$_[0]; @TO=split('\0',$TO);
$SUBJECT=$_[1];
$REPLYTO=$_[2];
$REMOTE = $_[3];
$THEMESSAGE = $_[4];
if ($REMOTE =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {
$addr = pack('C4', $1, $2, $3, $4);
}
else { die("Bad IP address: $!"); }
$port = 25 unless $port;
$port = getservbyname($port,'tcp') if $port =~ /\D/;
$proto = getprotobyname('tcp');
socket(S, PF_INET, SOCK_STREAM, $proto) or die("Socket failed: $!");
# $paddr = sockaddr_in($port, $addr);
# connect(S, $paddr) or die("Unable to connect: $!");
$sockaddr = 'S n a4 x8'; # shouldn't this be in Socket.pm?
connect(S, pack($sockaddr, AF_INET, $port, $addr)) or die("无法连接 : $!");

select(S); $| = 1; select(STDOUT);
$a=;
print S "HELO ${SERVERNAME}\n";
$a=;
print S "MAIL FROM:\n";
$a=;
print S "RCPT TO:<$TO[0]>\n";
$a=;
if ($#TO > 0) { foreach (1..$#TO) { print S "RCPT TO: $TO[$_]\n";$a=; }
}
print S "DATA \n";
$a=;
print S "To: $TO[0]\n";
if ($#TO > 0) { foreach (1..$#TO) { print S "Cc: $TO[$_]\n"; }
}
print S "Subject: $SUBJECT\n";
print S "Reply-To: $REPLYTO\n";

# Print the body
print S "$THEMESSAGE\n";
print S ".\n";
$a=;
print S "QUIT";
close (S);
}

呼叫时:
&sendemail($form{'to_email'}, $mail_title, $form{'myemail'}, $mailserver, "this is mail content") ;

当然server要有perl complier

Follow Ups: