标题:
[转帖]怎样用perl编cgi发email
[打印本页]
作者:
cnangel
时间:
2003-9-3 00:48
标题:
[转帖]怎样用perl编cgi发email
#!usr/bin/perl
#
#这是一个CGI程序,用语将表单中的内容发送到指定信箱.
#表单有两项内容其一为信箱地址"email",其二为内容"content"
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
#把html的输入读如到$buffer中,注意这时的#$buffer是一个长字符串
#CGI程序是使用STDIN和STDOUT输入输出
@pairs=split(/&/,$buffer);
foreach $pair(@pairs){
($name,$value)=split(/=/,$pair);
#把pairs拆分成不同输入框名字和输入内容的关联数组
$value=~s/\+/ /g;
#注意CGI把所有的输入空格转换成+,所以要变换回来
$value=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
#CGI把中文全部变成16进制编码,所以要用pack转换回来
$FORM{$name}=$value;
}
$mailbox=$FORM{"email"};
$mailcontent=$FORM{"content"};
open (MAIL,"|mail $mailbox");
print MAIL $mailcontent;
close(MAIL);
#本程序在Unix(Linux)服务器上成功运行.
#呵呵,很简陋的一个东东,可以做许多补充
#qxb 2000/04/16
作者:
magic
时间:
2003-9-3 16:56
标题:
[转帖]怎样用perl编cgi发email
具体补充一下:
#!/usr/bin/perl
# 请将下面邮箱改为您要接收反馈意见的邮箱,\@不要改动。
$user_email = "你的邮箱\@后缀";
$dd = `date`; chop $dd;
$method = $ENV{"REQUEST_METHOD"};
$type = $ENV{"CONTENT_TYPE"};
$remoteip = $ENV{"REMOTE_ADDR"};
if ($method ne "POST" || $type ne "application/x-www-form-urlencoded")
{
print "Location: http://当提交的时候返回的页面\n\n";
exit;
}
%input_values = &break_input;
$email = &normalize_query($input_values{"EMAIL"});
$message = &normalize_query($input_values{"MESSAGE"});
if ($message eq "") {
$error_msg = "您没有留下意见!<BR>请按返回键再试一次!";
exit &print_error();
}
if ($email eq "") {
$error_msg = "您没有提供e-mail地址,<BR>请填好您的e-mail地址再试一次!";
exit &print_error();
}
open(M,"|/usr/sbin/sendmail $user_email");
# 用的open语法打开邮件管道/usr/sbin/sendmail,所以nt和os一般不行
print M <<EOFX;
From: $email
To: $user_email
Subject: 意见反馈
Date: $dd
E-mail: $email
IP: $remoteip
Message:
$message
EOFX
close (M);
$error_msg = "感谢您对我们提出的建议";
&print_error();
#####################
# SUB-ROUTINES
#####################
sub break_input {
local ($i);
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
@form_names = split('&', $input);
foreach $i (@form_names) {
($html_name, $html_value) = split('=', $i);
$input_values{$html_name} = $html_value;
}
return %input_values;
}
sub normalize_query {
local($value) = @_;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
return $value;
}
sub print_error {
print <<EOP;
Content-type: text/html
<html>
<head>
<title>message</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.main {font-size: 12px}
.main1{ font-size: 14.8px }
.main2{ font-size: 16px }
.main4{font-size: 10px}
A:link {color: #333333; font-style: normal; text-decoration: underline}
A:visited {color: #333333; font-style: normal; text-decoration: underline}
A:active {color: #999999; font-style: normal; text-decoration: none}
A:hover {color: #999999; font-style:bold; text-decoration: none}
-->
</style>
</head>
<body bgcolor="#FFFFFF" text="#666666" leftmargin="0" topmargin="0">
<table width="100%" border="0" height="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="500" border="0" cellpadding="0" cellspacing="0" bgcolor="#C6C6C6" align="center">
<tr>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td bgcolor="#F8F8F8">
<table width="100%" border="0">
<tr>
<td bgcolor="#e6e6e6">
<div align="center" class="main2"><b>message</b></div>
</td>
</tr>
<tr>
<td height="20"></td>
</tr>
<tr>
<td>
<div align="center" class="main">$error_msg</div>
</td>
</tr>
<tr>
<td height="20"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div align="center"><br>
<input style='BORDER-RIGHT: #C6C6C6 1px solid; BORDER-TOP: #C6C6C6 1px solid; FONT-SIZE: 12px; BORDER-LEFT: #C6C6C6 1px solid; COLOR: #666666; BORDER-BOTTOM: #C6C6C6 1px solid; FONT-FAMILY: "宋体","Arial", "Helvetica", "sans-serif"; BACKGROUND-COLOR: #F8F8F8' type="button" value=" 返回 " onClick="javascript:history.back()" name="button">
</div>
</td>
</tr>
</table>
</body>
</html>
EOP
}
复制代码
作者:
magic
时间:
2003-9-3 18:06
标题:
[转帖]怎样用perl编cgi发email
nt的是这样的
#!/usr/bin/perl
use Net::SMTP;
$smtp=Net::SMTP->new("localhost");
$smtp->mail("root");
$smtp->to("bbs\@bbs.mit.edu");
$smtp->data();
$smtp->datasend("To: bbs\n\n");
$smtp->datasend("#name: midi\n");
$smtp->datasend("#password: abc12345\n");
$smtp->datasend("#board: PERL\n");
$smtp->datasend("#title: 灌水\n\n");
#do whatever you want....your body here:-)
$smtp->dataend();
$smtp->quit();
复制代码
欢迎光临 星星博客 (http://bbs.huhoo.net/)
Powered by Discuz! 7.0.0