在unix环境下,cgi程序发email都是用sendmail, 在windows95/98,nt没有sendmail,cgi程序如何发邮件呢?其实方法多得很呢!
1.windows环境下发邮件程序
这类程序很多,如blat,sendmail for windows,cgimail等等,它们的用法和unix下的sendmail极其相似,许多unix下cgi程序不用怎么修改就可以在windows下使用.
blat http://www.blat.com (免费)
sendmail for nt http://www.sendmail.com (商业软件)
cgimail http://www.stalkerlab.ch/ (免费)
sendmail for windows http://www.green-bean.com/bcware/sendmail.htm (共享软件)
另外zeng hui朋友(zeng_h@mail.hb.cnpc.com.cn)给我来信说他发现在sambar 4.1中的bin目录下有个mailit.exe文件,其功能与blat.exe相同,而且不用先安装.
2.采用perl模块net::smtp
系统必须支持smtp协议,nt中可安装exchange server
use net::smtp;
{
my $to = 'preston@home.com';
my $from = $env{user} || $env{username};
my $subject = "another test";
my $smtp = net::smtp->new('mail');
$smtp->mail($from);
$smtp->to($to);
$smtp->data();
$smtp->datasend("to: $to\n");
$smtp->datasend("from: $from\n");
$smtp->datasend("subject: $subject\n");
$smtp->datasend("x-mytoken: abcd\n");
$smtp->datasend("\n");
$smtp->datasend("a simple test message\n");
$smtp->dataend() || print "failed send!\n";
$smtp->quit;
}
print "\nmail sent ok\n";
-----
注意"$env{user}"是unix中用的,而$env{username}"是windows nt中用的
3.用outlook发邮件
下面的程序在perl for win32 (build 316)正常运行过
# sender's name and password
#
my $sender = "microsoft outlook"; # profile to be used
my $passwd = "ms exhange settings password"; #profile password
# create a new mapi session
#
use ole;
$session = createobject ole 'mapi.session' || die $!;
my $err = $session->logon($sender, $passwd);
if ($err) {
die "logon failed: $!";
}
# add a new message to the outbox.
#
$msg = $session->outbox->messages->add();
# add the recipient.
#
$rcpt = $msg->recipients->add();
$rcpt->{name} = 'recepient@host.org'; # email address of recepient
$rcpt->resolve();
# create a subject and a body.
#
$msg->{subject} = "test message";
$msg->{text} =
"this is a sample test message. using nt 4.0.
cheers,
mr. email";
# send the message and log off.
#
$msg->update();
$msg->send(0, 0, 0);
$session->logoff();
发送附件和拷贝也可以
$recipient='user@host.com';
$attachment='c:/attach.doc'
$profile="ms exchange settings";#profile name
$mailmeessage="hello";
$password="ms exhange settings password";#profile password
$subject='test';
system("mapisend -u \"$profile\" -p \"$password\" -f \"$attachment\" -s
\"$subject\" -r $recipient -m \"$mailmessage\"")