在当今网络众横的时代,不管是网络高手还是网络新手,在节日时经常会收到一些远方朋友通过网上送来的电子贺卡,而你又知不知道这贺卡是如何生成的呢?很多免费贺卡的网站上都有“你也自己亲手为你的朋友选一张贺卡”的链接,只要填上名称、邮件地址即可让对方收到你的电子贺卡。再深入一些,其实这些贺卡完全通过CGI脚本程序来实现的,曾经拥有过个人主页(拥有CGI执行权)的网友可能深有体会,几乎所有的免费贺卡脚本程序都工作在Unix系统下,而那些使用 Windows NT服务器的压根用不成,因为没有Unix下的SENDMAIL(为了区别两个不同的软件,大写为Unix下的SENDMAIL服务器,小写为小林的 sendmail)来发邮件,包括反馈表邮件回执等都无法应用,后来国外有个程序员写了一个Blat,可以代替SENDMAIL的某些工作,对于反馈表邮件回执等一些小问题基本上得到解决,但在设置上却比较麻烦,必须在服务器上先安装好以及将动态链接库文件存放在WinNTSystem32目录等,一般的网友想实现都很困难。随着Internet的发展与普及,又有个程序员开发了一个新的作品Stalker's Mailers,这个程序在使用上效为简单,程序小功能显得也少了。小林在学习Sockets过程中,也编了这样一个小程序,叫sendmail for NT,功能跟SMailets和Blat类似,仅支持简单的SMTP协议,需要SMTP服务器的支持,正确的说应该是Client程序。在应用方面比 SENDMAIL要麻烦些,由于需要先建立临时文件,然后再通过Client程序发送。下面将介绍一个简单的贺卡CGI脚本程序的实现方法:
* card2.pl 文件
#!/usr/local/bin/perl
###############################################################
# Program - A Postcard - English version 1.0 for Unix #
# Written by Jason W. Maloney http://www2.bc.edu/~malonejr #
# ----------------------------------------------------------- #
# Program - A Postcard - Chinese version 1.0 for NT #
# Written by Lin's Xinglu http://www.netease.com/~lin #
###############################################################
# 本脚本文件URL
$cgi = "http://www.softsite.com.cn/cgi-bin/postcard/card2.pl";
# 图片文件URL
$basepic = "http://202.130.5.31/cgi-bin/postcard/";
# 贺卡文件URL
$baseurl = "http://www.softsite.com.cn/cgi-bin/postcard/";
# 贺卡文件绝对路径
$basedir = "c:\inetpub\wwwroot\download\cgi-bin\postcard";
# 设置贺卡保留天数
$days = '15';
# 设置sendmail for NT的临时文件(绝对路径)
$filename = "c:\inetpub\wwwroot\cgi-bin\postcard\post.txt";
# 设置Sendmail for NT的绝对路径
$mailprog = "c:\inetpub\wwwroot\cgi-bin\sendmail\sendmail.exe";
# 设置发卡邮递员E-Mail地址
$webmaster = "WebMaster@softsite.com.cn";
# 发送邮件服务器IP
$smtp = "168.160.65.138";
###############################################################
$html = '.html';
$i=0;
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
@pairs = split(/&/,$buffer);
foreach $pair (@pairs){
($name,$value) = split(/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$FORM{$name} = $value;
}
#############
# 预览贺卡
#############
if ($FORM{'action'} eq 'preview') {
############################################################
&no_email unless $FORM{'mailfrom'} && $FORM{'mailto'};
############################################################
$pic = $FORM{'picture'};
$picture = "$basepic$pic";
print "Content-Type: text/htmlnn";
print "<HTML>n";
print "<TITLE>预览你的贺卡</TITLE>nn";
print "<BODY BGCOLOR=#FFFFFF>n";
print "<CENTER>n";
print "<TABLE BORDER=2>n";
print " <TD><IMG SRC=$basepic$FORM{'picture'} BORDER=0></TD>n";
print " <TD WIDTH=380 VALIGN=TOP>n";
print " <TABLE CELLPADDING=10>n<BR><BR>n";
print "<BLOCKQUOTE>n";
print "<b>$FORM{'recipient'}</b>:";
print "$FORM{'message'}";
print "<b>$FORM{'sender'}</b></TD>n";
print "</BLOCKQUOTE>n</TABLE>n</TABLE>n<BR>n";
print "<CENTER><TABLE WIDTH=500>nn";
print "<FORM METHOD=POST ACTION="$cgi">n";
print "<INPUT TYPE=HIDDEN NAME="action" VALUE="send">n";
print "<INPUT TYPE=HIDDEN VALUE="$basepic$FORM{'picture'}" NAME="picture">n";
print "<INPUT TYPE=HIDDEN VALUE="$FORM{'sender'}" NAME="sender">n";
print "<INPUT TYPE=HIDDEN VALUE="$FORM{'mailfrom'}" NAME="mailfrom">n";
print "<INPUT TYPE=HIDDEN VALUE="$FORM{'recipient'}" NAME="recipient">n";
print "<INPUT TYPE=HIDDEN VALUE="$FORM{'mailto'}" NAME="mailto">n";
print "<INPUT TYPE=HIDDEN VALUE="$FORM{'message'}" NAME="message">n";
print "<CENTER>满意吗?单击 Send 发送你的贺卡吧!";
print "<CENTER><INPUT TYPE="submit" VALUE=" Send "></FORM>n";
print "</TABLE>";
print "</BODY></HTML>";
}
##########
# 发送贺卡
##########
if ($FORM{'action'} eq 'send') {
########################
# 为贺卡取文件名
########################
($sec,$min,$hr,$day,$month,$year,$day_of_week,$day_of_year,$some) = localtime(time);
$minute = $min + 10;
$hour = $hr + 10;
$name_of_card = "$basedir$day_of_year$hour$minute$sec$html";
$url_of_card = "$baseurl$day_of_year$hour$minute$sec$html";
########################
# 制作贺卡
########################
open(POSTCARD,">>$name_of_card") ;
print POSTCARD "<HTML>n";
print POSTCARD "<TITLE>A PostCard For $FORM{'recipient'}</TITLE>nn";
print POSTCARD "<BODY BGCOLOR=#FFFFFF>n";
print POSTCARD "<CENTER>n";
print POSTCARD "<TABLE BORDER=2>n";
print POSTCARD " <TD><IMG SRC=$FORM{'picture'} BORDER=0></TD>n";
print POSTCARD " <TD WIDTH=380 VALIGN=TOP>n";
print POSTCARD " <TABLE CELLPADDING=10>n <BR><BR>";
print POSTCARD "<BLOCKQUOTE>";
print POSTCARD "<b>$FORM{'recipient'}</b>:";
print POSTCARD "$FORM{'message'}";
print POSTCARD "<b>$FORM{'sender'}</b></TD>n";
print POSTCARD "</TABLE>n</TABLE>n<BR></BLOCKQUOTE>nn";
print POSTCARD "<CENTER>n<TABLE WIDTH=500>nn<BR><BR>";
print POSTCARD " <TD>这个贺卡是你的朋友";
print POSTCARD "<A HREF=mailto:$FORM{'mailfrom'}>";
print POSTCARD "$FORM{'sender'}</A>送给你的, $FORM{'recipient'}如果你";
print POSTCARD "也希望用同样的方法给某个朋友发张贺卡,可到<A HREF=$baseurl>Lin's 明星贺卡中心</A>";
print POSTCARD "选择一张拥有自己风格的免费贺卡喔!^_^nn<BR><BR>";
print POSTCARD "</TABLE>n</BODY>n</HTML>n";
close(POSTCARD);
##########################
# 显示感谢你页面
##########################
print "Content-Type: text/htmlnn";
print "<HTML>n";
print "<TITLE>感谢你发送了一个贺卡</TITLE>nn";
print "<BODY BGCOLOR=#FFFFFF>n";
print "<CENTER>n";
print "<H1><B>感谢你发送了一个贺卡!</B></H1>n";
print "邻取贺卡通知邮件已经发送给$FORM{'recipient'}!<BR><BR>n";
print "想再看看你刚发送出去的这张贺卡<A HREF=$url_of_card>$url_of_card</A>n";
##########################
# 发送取卡通知给收件人
##########################
open (FILE, ">$filename") || die " Error: Not Found $filename Files!";
print FILE " n恭喜你$FORM{'recipient'},nn";
print FILE " 你的朋友$FORM{'sender'}已在Lin's 明星贺卡中心选择nn";
print FILE " 了一张漂亮的贺卡给你!你可以轻松的通过浏览器取到nn";
print FILE " 你的卡片,链接地址如下:nn";
print FILE " $url_of_cardnn";
print FILE " 这个贺卡保存在服务器上的时间大约为$days天, 你可以nn";
print FILE " 自由的打印出来或保存起来.nn";
print FILE " ---------------------------------------------------n";
print FILE " 星网情真 Xing2 WebSite http://www.softsite.com.cnn";
print FILE " Copyright (C) 1995-1998 Crackajack Software Studio.";
close (FILE);
open (MAIL, "|$mailprog -s $smtp -f $webmaster -i $filename -t $FORM{'mailto'} -u "A PostCard!" ") || die "Can't open $mailprog!n";
close(MAIL);
########################
# 自动删除旧的贺卡
########################
local(@items, $pair);
opendir(PICKUP, "$basedir");
@items = grep(/[0-9].html/,readdir(PICKUP));
closedir(PICKUP);
foreach $pair (@items)
{
if (-M "$basedir$pair" > $days)
{
unlink("$basedir$pair");
}
}
}
########################
# 没有填写邮件地址
########################
sub no_email {
print "Content-type: text/htmlnn";
print "<HTML>n<TITLE>错误</TITLE>n";
print "<BODY BGCOLOR=#FFFFFF>n";
print "<font color=red>不能继续!</font><BR>n";
print "你没有填写收卡人或发卡人邮件地址.";
exit;
}
###############################################################
上面这个脚本程序的应用通过
******** test.html 文件
<HTML>
<HEAD><TITLE>Lin's 明星贺卡中心</TITLE></HEAD>
<BODY>
<FORM METHOD="POST" ACTION="/cgi-bin/postcard/card.pl">
<IMG SRC="card001.gif" BORDER=1>
<INPUT TYPE="radio" NAME="picture" VALUE="card001.jpg" checked>
<IMG SRC="card002.gif" BORDER=1>
<INPUT TYPE="radio" NAME="picture" VALUE="card002.jpg">
<IMG SRC="card003.gif" BORDER=1>
<INPUT TYPE="radio" NAME="picture" VALUE="card003.jpg">
<IMG SRC="card004.gif" BORDER=1>
<INPUT TYPE="radio" NAME="picture" VALUE="card004.jpg">
<BR>
收卡人:
Name: <INPUT TYPE="TEXT" NAME="recipient">
E-Mail: <INPUT TYPE="TEXT" NAME="mailto">
送卡人:
Name: <INPUT TYPE="TEXT" NAME="sender">
E-Mail: <INPUT TYPE="TEXT" NAME="mailfrom">
曾言:
<TEXTAREA NAME="message" WRAP=VIRTUAL ROWS=4 COLS=40></TEXTAREA>
<INPUT TYPE="submit" VALUE=" 预览 "> <INPUT TYPE="reset" value=" 取消 ">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="preview">
</FORM>
</BODY></HTML>
--------------------------------------------------------
在上面的例子中,文件card001.gif至card004.gif为可见选择,而card001.jpg至card004.jpg
为贺卡图片文件,所有材料可由小林的软件站点:http://www.softsite.com.cn/取到。
只要再这个基础上修改,想必再漂亮的贺卡都可能做出来,怎么样?大家先试为快吧!
相关网址:
SENDMAIL:http://www.sendmail.org & http://www.sendmail.com
Blat for NT:http://gepasi.dbs.aber.ac.uk/softw/Blat.html
SMailers for NT:http://www.stalkerlab.ch/SMailers/index.html
sendmail for NT:http://www.netease.com/~lin 或 http://www.softsite.com.cn