#!/usr/bin/perl
#以二进制模式传送附件。
binmode(STDIN);
binmode(STDOUT);
binmode(STDERR);
#sender.pm模块位置
$ProgDir="c:\\httpd\\htdocs\\cgi-bin\\clmail";
#附件目录
$AttachDir="c:\\worktemp\";
#发信按钮图像
$MailButton='http://127.0.0.1/mailimage/mail_b.JPG';
#附件按钮图像
$AttachButton='http://127.0.0.1/mailimage/attach_b.JPG';
#页面标题
$title="免费邮件";
#SMTP服务器
$SmtpSever='192.192.194.1'; #这是一个虚构的服务器,请使用你的发信服务器代替它。
#ShowAttachForm();
#sender模块位置
push(@INC,$ProgDir);
my($From,$To,$Subject,$Cc,$Bcc,$AttachFile,$MailContent,$AttachNum,$AttachFile1,$AttachFile2,$AttachFile3);
use CGI;
$input=new CGI;
if($input->param('attachbutton.x') ne ""){
&ReadInput;
ShowAttachForm($From,$To,$Subject,$Cc,$Bcc,$AttachFile,$MailContent,$AttachNum,$AttachFile1,$AttachFile2,$AttachFile3,0);
}
#($ENV{'QUERY_STRING'} eq 'showattach') and &ShowAttachForm;
#&ShowAttachForm;
if($input->param('showmailform') eq 'yes')
{
&ShowMailForm;
exit;
}
if($input->param('beattachfile') eq 'yes'){
&AttachFile;
exit;
}
if ( $input->param('mailbutton.x') ne '' ) {
#&ReadInput;
&SendMail;
exit;
}
&ShowMailForm;
sub print_head{
print<<"EOF";
content-type:text/html\n
<html><head><title>$title</title></head>
<body>
EOF
}
sub print_foot{
print<<"EOF";
</body>
</html>
EOF
}
############################################################################
sub ShowAttachForm #4/14/00 4:02PM
############################################################################
{
my ($From,$To,$Subject,$Cc,$Bcc,$AttachFile,$MailContent,$AttachNum,$AttachFile1,$AttachFile2,$AttachFile3,$TooMany)=@_;
&print_head;
if ($TooMany eq 1) {
print<<"EOF";
<p><strong><font size="4" color="#FF0000">对不起,附件不能超过3个,本附件不能传送。</font></strong></p>
EOF
}
print<<"EOF";
<p><font size="3" color="#008000">按以下步骤发送附件:</font></p>
<form action="$ENV{'SCRIPT_NAME'}" method="post" align="center"
ENCTYPE="multipart/form-data">
<input type="hidden" name="beattachfile" value="yes"><input type="hidden" name="from"
value="$From"><input type="hidden" name="to" value="$To"><input type="hidden"
name="subject" value="$Subject"><input type="hidden" name="attachfile" value="$AttachFile"><input
type="hidden" name="cc" value="$Cc"><input type="hidden" name="bcc" value="$Bcc"><input
type="hidden" name="mailcontent" value="$MailContent"><input type="hidden"
name="attachnum" value="$AttachNum"><input type="hidden" name="attachfile1"
value="$AttachFile1"><input type="hidden" name="attachfile2" value="$AttachFile2"><input
type="hidden" name="attachfile3" value="$AttachFile3"><table border="0" cellpadding="0" cellspacing="0"
width="57%" height="76" style="font-size: 9pt; letter-spacing: 2px">
<tr>
<td width="50%" height="18">1、在以下框中点击“浏览”并在随后弹出的对话框中选择附件。</td>
</tr>
<tr>
<td width="50%" height="1"><input type="file" name="newfile" size="30"></td>
</tr>
<tr>
<td width="50%" height="18">2、点击“粘贴”键,将附件贴到邮件上。<input
type="submit" value="粘贴"></td>
</tr>
</table>
</form>
<form action="$ENV{'SCRIPT_NAME'}" method="post" align="center">
<input type="hidden" name="showmailform" value="yes"><input type="hidden" name="from"
value="$From"><input type="hidden" name="to" value="$To"><input type="hidden"
name="subject" value="$Subject"><input type="hidden" name="attachfile" value="$AttachFile"><input
type="hidden" name="cc" value="$Cc"><input type="hidden" name="bcc" value="$Bcc"><input
type="hidden" name="mailcontent" value="$MailContent"><input type="hidden"
name="attachnum" value="$AttachNum"><input type="hidden" name="attachfile1"
value="$AttachFile1"><input type="hidden" name="attachfile2" value="$AttachFile2"><input
type="hidden" name="attachfile3" value="$AttachFile3"><table
style="font-size: 9pt; letter-spacing: 2px" width="435" cellspacing="0" cellpadding="0">
<tr>
<td width="431" height="18">3、如有多个附件,重复以上步骤。</td>
</tr>
<tr>
<td width="431" height="18">4、点击“完成”,以完成附件操作。<input
type="submit" value="完成"></td>
</tr>
<tr>
<td width="431" height="18"><font size="3" color="#008000">附件列表:</font><p><select
name="fj" size="3">
<option value="AttachFile1">$AttachFile1</option>
<option value="AttachFile2">$AttachFile2</option>
<option value="AttachFile3">$AttachFile3</option>
</select></td>
</tr>
</table>
</form>
EOF
&print_foot;
exit;
} ##ShowAttachForm
############################################################################
sub ReadInput #4/14/00 2:34PM
############################################################################
{
$From= $input->param('from');
$To=$input->param('to');
$Subject=$input->param('subject');
$Cc=$input->param('cc');
$Bcc=$input->param('bcc');
$AttachFile=$input->param('attachfile');
$MailContent=$input->param('mailcontent');
$AttachNum=$input->param('attachnum');
$NewFile=$input->param('newfile');
$AttachFile1=$input->param('attachfile1');
$AttachFile2=$input->param('attachfile2');
$AttachFile3=$input->param('attachfile3');
} ##ReadInput
############################################################################
sub SendMail #4/13/00 10:14AM
############################################################################
{
&ReadInput;
#读入附件信息
if($AttachNum ne ""){ @AttachFileName=split(/;/,$AttachFile);}
use Sender;
$Send = new Sender({from=>$From,
smtp=>$SmtpSever});
if ( !( ref $Sender) =~ /Sender/i ) {
die $Sender::Error;
}
$Send->OpenMultipart({to=>$To,
subject=>$Subject,
replyaddr=>$Cc});
$Send->Body;
$Send->SendLine($MailContent);
if ($AttachNum ne "") {
for ( $i=1; $i<=$AttachNum; $i++) {
$Send->SendFile({description=>'邮件附件',
encoding=>'Base64',
disposition=>"attachment;filename=$AttachFileName[$i-1]",
file=>"$AttachDir$AttachFileName[$i-1]"});
}
}
$Send->Close;
#&print_head;
#print<<"EOF";
#$From,$To,$Subject,$Cc,$Bcc,$AttachFile,$MailContent,$AttachNum,$SmtpSever
#EOF
#&print_foot;
if ($Send->{'error'} <0 ) {
mail_error('i',$Send->{'error'});
}
&ShowMailSuccess;
}
sub mail_error #3/24/00 4:24PM
{
my ($user,$errorCode)=@_;
&print_head;
($errorCode eq -1) and print "SMTP错误代码:-1";
($errorCode eq -2) and print "SMTP错误代码:-2";
($errorCode eq -3) and print "SMTP错误代码:-3";
($errorCode eq -4) and print "SMTP错误代码:-4";
($errorCode eq -5) and print "SMTP错误代码:-5";
($errorCode eq -6) and print "SMTP错误代码:-6";
($errorCode eq -7) and print "SMTP错误代码:-7";
($errorCode eq -8) and print "SMTP错误代码:-8";
($errorCode eq -9) and print "SMTP错误代码:-9";
($errorCode eq -10) and print "SMTP错误代码:-10";
($errorCode eq -11) and print "SMTP错误代码:-11";
($errorCode eq -12) and print "SMTP错误代码:-12";
&print_foot;
exit;
}
############################################################################
sub ShowMailSuccess #4/15/00 7:46PM
############################################################################
{
&print_head;
print<<"EOF";
成功发信
EOF
&print_foot;
exit;
} ##ShowMailSuccess
############################################################################
sub AttachFile #4/13/00 4:04PM
############################################################################
{
&ReadInput;
($AttachNum >= 3) and (ShowAttachForm($From,$To,$Subject,$Cc,$Bcc,$AttachFile,$MailContent,$AttachNum,$AttachFile1,$AttachFile2,$AttachFile3,1));
my $AbsName = $NewFile;
$AbsName =~ s!^.*(\\|\/)!!;
open (FILE, ">$AttachDir$AbsName");
binmode(FILE);
while (my $bytesread = read($NewFile, my $buffer, 1024)) {
print FILE $buffer;
}
close (FILE);
$AttachFile .= $AbsName.';';
$AttachNum += 1;
if ($AttachNum eq 1) {$AttachFile1=$AbsName;}
if ($AttachNum eq 2) {$AttachFile2=$AbsName;}
if ($AttachNum eq 3) {$AttachFile3=$AbsName;}
ShowAttachForm($From,$To,$Subject,$Cc,$Bcc,$AttachFile,$MailContent,$AttachNum,$AttachFile1,$AttachFile2,$AttachFile3,0);
}
############################################################################
sub ShowMailForm #4/13/00 4:50PM
############################################################################
{
#my ($From,$To,$Subject,$Cc,$Bcc,$AttachFile,$MailContent,$AttachNum) = @_;
&ReadInput;
&print_head;
print<<"EOF";
<form action="$ENV{'SCRIPT_NAME'}" align="center" method="post">
<input type="hidden" name="attachnum" value="$AttachNum"><input type="hidden" name="attachfile1"
value="$AttachFile1"><input type="hidden" name="attachfile2" value="$AttachFile2"><input
type="hidden" name="attachfile3" value="$AttachFile3">
<table border="0" cellpadding="0" cellspacing="0" width="69%">
<tr>
<td width="22%" align="right" bgcolor="#008000"><font color="#FFFFFF">发信人:</font></td>
<td width="78%" bgcolor="#F0FCC0"><input type="text" name="from" value="$From" size="38"></td>
</tr>
<tr>
<td width="22%" align="right" bgcolor="#008000"><font color="#FFFFFF">收件人:</font></td>
<td width="78%" bgcolor="#F0FCC0"><input type="text" name="to" value="$To" size="38"></td>
</tr>
<tr>
<td width="22%" align="right" bgcolor="#008000"><font color="#FFFFFF">主题:</font></td>
<td width="78%" bgcolor="#F0FCC0"><input type="text" name="subject" value="$Subject" size="38"></td>
</tr>
<tr>
<td width="22%" align="right" bgcolor="#008000"><font color="#FFFFFF">抄送:</font></td>
<td width="78%" bgcolor="#F0FCC0"><input type="text" name="cc" value="$Cc" size="38"></td>
</tr>
<tr>
<td width="22%" align="right" bgcolor="#008000"><font color="#FFFFFF">暗送:</font></td>
<td width="78%" bgcolor="#F0FCC0"><input type="text" name="bcc" value="$Bcc" size="38"></td>
</tr>
<tr>
<td width="22%" align="right" bgcolor="#008000"><font color="#FFFFFF">附件:</font></td>
<td width="78%" bgcolor="#F0FCC0"><input type="text" name="attachfile" value="$AttachFile"
size="38"></td>
</tr>
<tr>
<td width="22%" align="right">
</td>
<td width="78%" valign="bottom" align="left" ><input alt="发信" name="mailbutton"
src="$MailButton" type="image" > <input alt="附件" name="attachbutton"
src="$AttachButton" type="image" >
</td>
</tr>
<tr>
<td width="22%" align="right">邮件正文:</td>
<td width="78%"></td>
</tr>
<tr>
<td width="100%" colspan="2" align="center"><textarea rows="11" name="mailcontent" cols="47">$MailContent</textarea></td>
</tr>
</table>
</form>
EOF
&print_foot;
} ##ShowMailForm