• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

need help on writting cgi-script for email receipt

akersche

New Member
What I basically want to write is a simple script, that sends me an email with the cgi-environment variables.
BUT I don't wanna get the email, when I am the one running the script (checking IP).
I got the script working on virtualave. switched now to tera-byte and added the ip checking.
what am i doing wrong?!
I also get an error without checking for the IP....

Here is the script:


#!/usr/bin/perl
# http://www.kersche.net/receipt.cgi?email=tst #

# Define Variables #
# $mailprog defines the location of your sendmail program on your unix #
# system. #
$mailprog = '/usr/sbin/sendmail';
# Done #
##############################################################################

# First, get the CGI variables into a list of strings
%cgivars= &getcgivars ;

# Retrieve Date
&get_date;

# Send E-Mail
&send_mail;



#----------------- start of &getcgivars() module ----------------------

# Read all CGI vars into an associative array.
# If multiple input fields have the same name, they are concatenated into
# one array element and delimited with the \0 character (which fails if
# the input has any \0 characters, very unlikely but conceivably possible).
# Currently only supports Content-Type of application/x-www-form-urlencoded.
sub getcgivars {
local($in, %in) ;
local($name, $value) ;


# First, read entire string of CGI vars into $in
if ( ($ENV{'REQUEST_METHOD'} eq 'GET') ||
($ENV{'REQUEST_METHOD'} eq 'HEAD') ) {
$in= $ENV{'QUERY_STRING'} ;

} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
if ($ENV{'CONTENT_TYPE'}=~ m#^application/x-www-form-urlencoded$#i) {
$ENV{'CONTENT_LENGTH'} ;
read(STDIN, $in, $ENV{'CONTENT_LENGTH'}) ;
}
}

# Resolve and unencode name/value pairs into %in
foreach (split('&', $in)) {
s/\+/ /g ;
($name, $value)= split('=', $_, 2) ;
$name=~ s/%(..)/chr(hex($1))/ge ;
$value=~ s/%(..)/chr(hex($1))/ge ;
$in{$name}.= "\0" if defined($in{$name}) ; # concatenate multiple vars
$in{$name}.= $value ;
}

return %in ;

}



sub get_date {

# Define arrays for the day of the week and month of the year. #
@days = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June','July',
'August','September','October','November','December');

# Get the current time and format the hour, minutes and seconds. Add #
# 1900 to the year to get the full 4 digit year. #
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
$year += 1900;

# Format the date. #
$date = "$days[$wday], $months[$mon] $mday, $year at $time";

}




sub send_mail {
# Localize variables used in this subroutine. #
#local($print_config,$key,$sort_order,$sorted_field,$env_report);

#if not MY IP then sent the mail!!
if ($ENV{'REMOTE_ADDR'} != '212.17.104.124') {
# Open The Mail Program
open(MAIL,"|$mailprog");

print MAIL "To: arno\@kersche.net (Arno Kersche)\n";
print MAIL "From: arno\@kersche.net (Arno Kersche)\n";


print MAIL "Subject: Mail was retrieved by recipient\n";

print MAIL "Below is the information of this return receipt:\n";
print MAIL "Email viewed on $date\n";
print MAIL "add 8 hours to get CET (correct time diffrence)\n"
print MAIL "-" x 75 . "\n\n";


# Print the CGI variables sent by the user.
# Note that the order of variables is unpredictable.
# Also note this simple example assumes all input fields had unique names,
# though the &getcgivars() routine correctly handles similarly named
# fields-- it delimits the multiple values with the \0 character, within
# $cgivars{$_}.
foreach (keys %cgivars) {
print MAIL "[$_] = [$cgivars{$_}]\n" ;
}

#print MAIL "Remote_Addr: $ENV{'REMOTE_ADDR'}\n" ;

print MAIL "\n\n" ;

foreach $key (sort keys(%ENV)) {
print MAIL "$key = $ENV{$key}\n" ;
}

print MAIL "-" x 75 . "\n\n";


close (MAIL);
} #end if
}
 
try

if ($ENV{'REMOTE_ADDR'} ne '212.17.104.124')


ne is for strings
!= is for numbers

what is the error and show the lines around it
 
Thanks for the hint.
But it didn't help.

I just recieve following error messge, which doesn't tell much. And that's the problem why I find it tricky to locate the error.

"Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request. "

Also tried:
if ( ($ENV{'REMOTE_ADDR'} eq '212.17.104.157') {
#do nothing!!
} else {

# Open The Mail Program
.....
}
But without success.

I believe the error is not with that ip-checking.
cause i don't get an email and the same error message, when i leave that away.
 
Originally posted by akersche
if ( ($ENV{'REMOTE_ADDR'} eq '212.17.104.157') {
#do nothing!!
} else {

# Open The Mail Program
.....
}
But without success.

see something missing? ;)
 
just posted the dots instead of the code, which can be seen above.

and i am not sure, if it works that the if-part is empty and just the else part is used... But it was just another try.
 
Running the script thru the perl compiler (win2k) give me the following error:

C:\>perl test.pl
syntax error at test.pl line 103, near "print"
Execution of test.pl aborted due to compilation errors.

when I commented out that line (it wasn't a script altering change), I got these errors too:

C:\>perl test.pl
syntax error at test.pl line 112, near ") {"
syntax error at test.pl line 128, near "}"
Execution of test.pl aborted due to compilation errors.

Might I suggest you install perl on your computer and do debugging locally first? I always do, since you generally don't get access to the raw errors online...

Of course, if your host gives you access to the raw error log, you can check that for debugging purposes too.
 
Last edited:
thanks a lot.
103: i forget ";" (added the line later on)

guess i can fix the rest now, too, cause it's located :)

and yes, i really need to get a debugger. got apache and perl on my windows pc (but without sendmail)
gonna get a debugger.
what are you using?
 
Just using ActivePerl...to debug a script, just go to the command prompt and do this:

C:\perl testscript.pl

That will show you either the output of the script or the line number it failed on.

You'll have to add the perl compiler to your path (if you don't know how, I can show you, or, I think the instructions are in the ActivePerl docs, which I assume you are using).

My actual server (not this computer, the one sitting next to me :D) is running IIS 5.0 on Windows 2000, with ActivePerl, PHP and ASP enabled....
 
Back
Top