这是别人给我的
怎么看不懂............
#!/usr/bin/perl
#
# Script setup:
# -------------
#
# Change the first line of this file, if necessary. Either
# '#!/usr/local/bin/perl' or '#!/usr/bin/perl' should work on any unix
# system.
# Change the configuration part below as described below.
# Upload this file as ASCII to your cgi-bin and chmod it to 755, if
# you have a unix server.
# ( user: [x]read [x]write [x]execute
# group: [x]read [ ]write [x]execute
# other: [x]read [ ]write [x]execute )
#
#
# Form setup:
# -----------
#
# Required fields:
#
#
#
# The first line is the location of the mail script. The second line is the
# alias of the recipient as specified in %alias (see cofiguration below).
# With version 1.1 and higher you can specify as many recipients as you like.
# Just seperate them by comata (,). Do not use any blanks. Remember: Do not
# use real email addresses, but alias names! Example:
#
#
# Note that the alias stuff is a security feature to keep bad people
# from using the script as an anonymous remailer. Other form mailers
# use the referer mechanism to accomplish the same thing. This is not a good
# idea, because the referer information is provided by the client and can
# hence be easily faked.
#
# All other fields are optional, but i guess it would be a good idea to include
# some. :)
#
# Configuration fields:
#
# - linkURL
# This will be used for a link on the confirmation page. Default is
# your base directory /. Example:
#
# value="http://your.server.com/you/index.html">
#
# - linkName
# This will be used as name for the link specified above. Default is 'home'.
# Example:
#
# - e-mail:
# The email address of your visitor. This will appear in the 'From' field of
# the messages header. Example:
#
# - name
# The realname of your visitor. This will appear in the 'From' field of the
# messages header. Example:
#
# - subject
# The subject of the message. This will appear in the 'Subject' field of the
# messages header. Example:
#
# - required
# Specifies required fields. The script will only send mail, if all
# required fields are filled out. Example:
#
#
# - empty_fields (v1.1)
# Determines wether empty fields should get mailed (yes) or not (no).
# Default option is no. Example:
#
#
# - order (v1.1)
# Determines the order of the form fields in the message. Seperate the form
# field names with a coma (,); do not insert blanks. All form fields that are
# not specified here will be mailed in a random order after the ordered ones.
# Example:
#
#
# - bgcolor
# Specifies the background color of the confirmation and error pages.
# Default value is white. Example:
#
#
# - color
# Specifies the font color of the confirmation and error pages.
# Default value is black. Example:
#
#
# - face
# Specifies the font face used for the confirmation and error pages.
# Default value is Arial. Example:
#
#
# - title
# Specifies the title of the confirmation page. Default value is
# 'message sent'. Example:
#
#
# - redirect
# Use it, if you want to use your own confirmation page. Example:
#
# value="http://www.server.com/you/thank_you.html">
#
# All other form fields will get mailed to you. You can include as many
# as you like. Example:
# your homepage:
# will get mailed as
# [ hp: ]
# .... <--- here comes the user's input
#
# Don't forget to insert a submit button
# and to close the form tag with ... and maybe setup a link to my
# website
, if you think that this script
# or my site is worth it ;)
#
# enjoy ... bernd
######################################
# Script Configuration
# Uncomment the next line (ie remove the '#' from the line '# use CGI...'),
# if you get a server error message while running the script. The error
# message will then be redirected to the browser so that you can debug
# the script more easily.
# use CGI::Carp qw(fatalsToBrowser);
# Uncomment the next four lines, if you don't have access
# to a sendmail program like 'sendmail' or 'qmail-inject'
# and change $smtp_server to your SMTP server and $LOCALHOST
# to the name of your computer.
use Socket;
$LOCALHOST = 'localhost';
$smtp_server = 'mail.wxcsdz.com';
$smtp_port = 25;
# Uncomment and change the next line to your sendmail program,
# if you have one. If you don't have one specify a smtp_server above.
# $sendmail = "/usr/sbin/sendmail -t";
# Choose an alias for every recipient and enter it as shown
# (Format: 'alias_name' => 'name@server.com').
# The alias has to be in the form field
# .
%alias = ( 'to' => 'wxcsdz@wxcsdz.com',
'someone' => 'someone@somewhere.com' );
#######################################
# No need to change anything below this line ...
#
%config = ( 'e-mail' => '', 'subject' => 'Form Feedback',
'name' => '', 'bgcolor' => 'white',
'redirect' => '', 'title' => '递交成功',
'required' => '', 'face' => 'Arial',
'to' => '', 'color' => 'black',
'linkURL' => '/', 'linkName' => 'home',
'order' => 'none', 'empty_fields' => 'no' );
&parse_form;
if( $in{'to'} =~ m/\,/ )
{
@recipients = split( /\,/, $in{'to'} );
$in{'to'} = shift( @recipients );
}
if( $alias{$in{'to'}} ) { $in{'to'} = $alias{$in{'to'}} }
else { &unknown_user( $in{'to'} ) }
&check_required;
&parse_config;
&build_message;
&send_message( $config{'to'} );
if( $#recipients > -1 )
{
foreach( @recipients )
{
if( $alias{$_} ) { $to = $alias{$_} }
else { &unknown_user( $_ ) }
$msg =~ s/^.*\n//;
&send_message( $to )
}
}
$msg =~ s/^.*\n//;
&return_html;
exit;
sub return_html
{
if( $config{'redirect'} ) { print "Location: $config{'redirect'}\n\n"; exit; }
print <<"__STOP__";
Content-Type: text/html
$config{'title'}
$config{'title'}
以下信息已经被寄往 $config{'to'}:
$msg
[ $config{'linkName'} ]
__STOP__
}
sub missing_fields
{
my $err = join( ", ", @_ );
print <<"__STOP__";
Content-Type: text/html
出错:资料不完整
出错:资料不完整
以下栏目没有填写或填写格式不对:
$err
__STOP__
exit
}
sub unknown_user
{
my $user = shift;
print <<"__STOP__";
Content-Type: text/html
ERROR - unknown user
ERROR - unknown user
$user is not a registered user!
[ $config{'linkName'} ]
__STOP__
exit
}
sub build_message
{
my $now_string = gmtime;
$msg .= "From: $config{'e-mail'} ($config{'name'})\n";
$msg .= "Subject: $config{'subject'}\n\n";
if( $config{'order'} ne 'none' )
{
my @ordered_fields = split( /\,/, $config{'order'} );
foreach( @ordered_fields )
{
&add_to_msg( $_ );
delete( $in{$_} )
}
}
foreach ( keys %in )
{
&add_to_msg( $_ );
}
$msg .= "---\n";
$msg .= "递交者的IP地址:$ENV{'REMOTE_ADDR'}\n";
$msg .= "递交时间:$now_string (GMT).\n"
}
sub send_message
{
my $to = shift;
$msg = "To: $to\n" . $msg;
if( defined( $sendmail ) )
{
open( MAIL, "| $sendmail" ) or die( "Can't open $sendmail, $!\n" );
print MAIL $msg;
close( MAIL )
}
elsif( $smtp_server && $smtp_port )
{
send_smtp_mail( $to, $config{'e-mail'}, $msg );
# or die( "Can't send mail via smtp! Check you settings.\n" );
}
else
{
die( "Don't know how to send mail!\n" .
"Specify a valid smtp_host or sendmail program!\n" )
}
}
sub check_required
{
return unless( $in{'required'} );
my @missing;
my @required = split( /\,/, $in{'required'} );
foreach ( @required )
{
if( $_ eq 'e-mail' ) { push( @missing, $_ ) unless( &check_mail($in{$_}) ) }
elsif( !$in{$_} ) { push( @missing, $_ ) }
}
if( @missing ) { missing_fields( @missing ) }
}
sub check_mail
{
if( $_[0] =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
$_[0] !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/ )
{
return 0
}
else { return 1 }
}
sub parse_config
{
foreach ( keys %config )
{
if( $in{$_} )
{
$config{$_} = $in{$_};
delete $in{$_}
}
}
}
sub parse_form
{
my( @pairs, $value, $name, $buffer );
if( $ENV{'REQUEST_METHOD' } eq 'GET' )
{
@pairs = split( /&/, $ENV{'QUERY_STRING'} );
}
elsif( $ENV{'REQUEST_METHOD'} eq 'POST' )
{
read( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );
@pairs = split( /&/, $buffer )
}
else { die( "unknown REQUEST_METHOD!\n" ) }
foreach $pair (@pairs)
{
($name, $value) = split( /=/, $pair );
$name =~ tr/+/ /;
$value =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ s///g;
$value =~ s///g;
if( $in{$name} && $value ) { $in{$name} = "$in{$name}, $value" }
else { $in{$name} = $value }
}
}
sub send_smtp_mail
{
my( $to, $from, $message ) = @_;
my( $iaddr, $paddr, $proto ) = undef;
$iaddr = inet_aton( $smtp_server );
$paddr = sockaddr_in( $smtp_port, $iaddr );
$proto = getprotobyname( 'tcp' );
socket( SOCK, PF_INET, SOCK_STREAM, $proto ) or return 0;
connect( SOCK, $paddr ) or return 0;
receive_from_server( \*SOCK ) or return 0;
send_to_server( \*SOCK, "HELO $LOCALHOST" ) or return 0;
receive_from_server( \*SOCK ) or return 0;
send_to_server( \*SOCK, "MAIL FROM: <$from>") or return 0;
receive_from_server( \*SOCK ) or return 0;
send_to_server( \*SOCK, "RCPT TO: <$to>" ) or return 0;
receive_from_server( \*SOCK ) or return 0;
send_to_server( \*SOCK, "DATA" ) or return 0;
receive_from_server( \*SOCK ) or return 0;
send_to_server( \*SOCK, "$message\n." ) or return 0;
receive_from_server( \*SOCK ) or return 0;
send_to_server( \*SOCK, "QUIT" ) or return 0;
receive_from_server( \*SOCK ) or return 0;
close( SOCK ) or return 0;
return 1
}
sub send_to_server
{
my( $socket ) = shift;
my( $message ) = shift;
send( $socket, "$message\n", 0 ) or return 0;
return 1
}
sub receive_from_server
{
my( $socket ) = shift;
my( $reply );
while( $reply = <$socket> )
{
return 0 if $reply =~ /^5/;
last if $reply =~ /^\d+ /;
}
return 1
}
sub add_to_msg
{
my $text = shift;
if( $in{$text} or $config{'empty_fields'} eq 'yes' )
{
$in{$text} =~ s/\r\n/\n/g;
$msg .= " [ $text: ]\n";
$msg .= "$in{$text}\n\n"
}
}