PDA

View Full Version : problem running cgi-form ->please help



akersche
February 17th, 2001, 22:30
hi,

i am a newbie concerning cgi-scripts, but programing and computer literate (graduate student of cs)

I am not sure wheater I am making something wrong, or the problem is with my free site at virtualave.net.

I just want to start with a simple online form and want the input to be emailed to me via cgi and sendmail.

BUT it didn't work at all:-( got an error, but I couldn't find an error, when opening the cgi-error file of my free site.

Would be great if anybody could go through the script or tell me what problems at virualave.net could be:-)
TIA
ARNO

What i did is write the following html-page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD><TITLE>Test FORM</title></head>
<body>
<FORM ACTION="webmail.cgi" METHOD="post">
<INPUT TYPE=hidden NAME="send_to" VALUE="from_form@kersche.net">
<INPUT TYPE=hidden NAME="subject" VALUE="Online Form Submitted">
<INPUT TYPE=hidden NAME="response_url" VALUE="http://www.kersche.net/">
<INPUT type="text" size="30" VALUE="TestFeld">
<input TYPE=submit border=0 Name="Sent" VALUE="Sent">
</FORM>
</body>
</html>

And I am using the following cgi-script, which i modified to the needed settings. (also set permissions and it's in the same directory, which i called cgi-bin as the html page)


!/usr/local/bin/perl

#*********************************************************** ***********
#* Program: WebMail from Radiation *
#* Version: 2.0 *
#* Author: Andrew Cowan (icculus@radiation.com) for Radiation *
#* Date: January 7, 1997 *
#* LastModified: August 8, 1999
#* Product Info: http://www.radiation.com/gizmos/webmail.html *
#* *
#* This program is the copyrighted property of Radiation, a venture *
#* of GlobalMedia Design. Any use of this program is subject to the *
#* the terms of the license agreement (LICENSE.TXT) included as part *
#* of this distribution archive. Any other uses are stictly *
#* prohibited without the written permission of GlobalMedia Design *
#* and all other rights are reserved. *
#* *
#* Support for this software is available only to registered users. *
#* For registration information, consult the REGISTER.TXT document *
#* included as part of this distribution archive, or visit the Gizmos *
#* section of the Radiation website at: *
#* *
#* http://www.radiation.com/gizmos/ *
#* *
#*********************************************************** ***********

# ************************************************************ ************
# WebMail Installation and Usage Instructions
# ************************************************************ ************
#
# This script requires perl 4.036 or any newer version. Put the webmail.cgi
# script in a CGI executable directory. For Unix systems, be sure to make the
# file executable itself if it is not already. The command to do this is:
#
# chmod 755 webmail.cgi
#
# If you are using a Dos-based or Macintosh system you will need to make
# sure that .cgi files are running the perl interpreter. If this is a problem
# you can always rename the file to run the interpreter correctly.
#
# All that remains is to create the form you want to integrate with the
# script (we assume you know how to write forms to call existing programs).
# The hidden form fields you will use are as follows:
#
# * send_to - Set the value of this field to be the email address you
# want the mail sent to.
#
# * subject - Set this field to what you would like the subject of your
# mail to be.
#
# * response_url - This field is optional, if not set a default response
# will be printed. If set, the user's web browser will be redirected
# to the url you specify. Be sure to use absolute urls for your responses.
#
# If you experience problems with the mail (i.e. It doesn't get sent) you
# will probably need to adjust the $MAIL setting in the script. Ask your
# system administrator the location to use for the system's mail program.
#
# ************************************************************ ************
# Configuration Section
# ************************************************************ ************
#
# ************************************************************ ************

# Set MODE to '1' - normal mail '2' - sendmail
$MODE = '2';

# If MODE set to '2' this SENDMAIL program is used
$SENDMAIL = "/var/qmail/bin/qmail-inject"; # Adjust as needed

# FROM_EMAIL is only used in mode '2' (sendmail mode)
$FROM_EMAIL = 'form\@kersche.net';

# REPLY_EMAIL is only used in mode '2' (sendmail mode)
$REPLY_EMAIL = 'arno\@kersche.net';

# Set SORT_MODE to '1' - alphabetic sorting or '2' for sorting as found
$SORT_MODE = '1';

# If you are using mod_perl with auto-content-type display set, uncomment
# the next line, to avoid the content type from being duplicated
#$MOD_PERL = 1;

# ************************************************************ ************
# Main Routine Section
# ************************************************************ ************


&cgiparse;

# Prevent malicious use and check for valid email address
$elements{'send_to'} =~ s/\;//g;
$elements{'send_to'} =~ s/^\s+//g;
$elements{'send_to'} =~ s/\s+$//g;
if ($elements{'send_to'} =~ /^\S+\@\S+$/){
&send_mail;
}

if (defined($elements{'response_url'})){
print "Location: $elements{'response_url'} \n\n";
}else{
unless ($MOD_PERL){
print "Content-type: text/html \n\n";
}

&print_default;
}

exit(0);

# ************************************************************ ************
# Subroutine Declarations
# ************************************************************ ************

# ************************************************************ ************
# Send_Mail - Send mail to the specified location
# ************************************************************ ************

sub send_mail{

if ($MODE eq '1'){
# Normal mail front-end program mode
open(MAIL, "|mail -s \"$elements{'subject'}\" $elements{'send_to'}");
}else{
# sendmail program mode
open(MAIL, "|$SENDMAIL -f $FROM_EMAIL -t $elements{'send_to'}");
print MAIL "Subject: $elements{'subject'}\n" .
"Reply-to: $REPLY_EMAIL \n" .
"From: $FROM_EMAIL \n\n";
}

print MAIL <<"EOM1";

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

* $elements{'subject'} *

EOM1

if ($SORT_MODE eq '1'){
foreach $key (sort by_alph keys(%elements)){
next if $key =~ /^(send_to|subject|response_url)$/;
print MAIL "[$key]: \n$elements{$key} \n\n";
}
}else{
for ($i = 0; $i <= $#elements2 ; $i+=2){
local($key) = $elements2[$i];
local($val) = $elements2[$i+1];
unless ($key =~ /^(send_to|subject|response_url)$/){
print MAIL "[$key]: \n$val \n\n";
}
}
}

print MAIL <<"EOM2";

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


EOM2

close(MAIL);

}

# ************************************************************ ************
# By_Alph - Sort by alphabetical order
# ************************************************************ ************

sub by_alph{

local $newa = uc($a);
local $newb = uc($b);

$newa cmp $newb;

}

# ************************************************************ ************
# Print_Default - Print a default response if no response page defined
# ************************************************************ ************

sub print_default{

print <<"EOF";

<html><head><title>WebMail Response</title></head>
<body bgcolor=#FFFFFF>

<center><h1>WebMail Response</h1></center>

<center>
Thank you for your submission. The information you provided is being
emailed to <a href="mailto:$elements{'send_to'}">$elements{'send_to'}</a>.
</center>

<p><hr><p>

<center>
<font size=-1>
WebMail is copyright &copy 1996 - 1999
<a href="http://www.radiation.com/">Radiation</a><br>
a service mark of
<a href="http://www.radzone.org/gmd/">GlobalMedia Design Inc.</a>
</font>
</center>

</body></html>

EOF

}

# ************************************************************ ************
# Urldecode Routines
# ************************************************************ ************
# Copyright & Disclaimer. (James Tappin)
# This set of routines may be freely distributed, modified and
# used, provided this copyright & disclaimer remains intact.
# This package is used at your own risk, if it does what you
# want, good; if it doesn't, modify it or use something else--but
# don't blame me. Support level = negligable (i.e. mail bugs but
# not requests for extensions)
# ************************************************************ ************

sub cgiparse {

if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $data, $ENV{'CONTENT_LENGTH'});
}
elsif ($ENV{'REQUEST_METHOD'} eq "GET") {
$data = $ENV{'QUERY_STRING'};
}
else {
#
# Bad request method report and exit.
#
&method_error;
}

%elements = &url_decode(split(/[&=]/,$data));
@elements2 = &url_decode(split(/[&=]/,$data));
%elements;
}

sub method_error {

print "\nCGI Script Requires use of method POST or GET.\n";
exit;
}


sub url_decode {

foreach (@_) {
tr/+/ /;
s/%(..)/pack("c",hex($1))/ge;
}
@_;
}

# ************************************************************ ************
# End of Generic Form Mailer
# ************************************************************ ************

razor
February 17th, 2001, 22:58
did u make sure to:
-upload it ascii
-upload it to the cgi-bin
-chmod it 755

akersche
February 18th, 2001, 18:51
thanks razor.
what i did wrong was:
not to upload it ascii

now the script runs, but it doesn't work that i recieve an email as desired. guess there is another mistake or something with "/var/qmail/bin/qmail-inject"

any further suggestions?

razor
February 19th, 2001, 09:56
try replacing "/var/qmail/bin/qmail-inject" with "/usr/sbin/sendmail"(or the path to sendmail)

Koolguy
February 19th, 2001, 11:46
As I recall /var/qmail/bin/qmail-inject is the correct path to sendmail on virtualave.

razor
February 19th, 2001, 13:14
oh. . . i didnt know cuz i never used virtual ave. sorry about that

akersche
February 19th, 2001, 14:38
thanks for trying to help me. that's really great.

I tried now
/var/qmail/bin/qmail-inject
and also
/usr/sbin/sendmail

BUT both didn't work out. The script runs ok, i don't get an error and i am rederected to the desired page after submitting the form. BUT I don't get an email.

seems like it is a specific problem with viruallave. guess i should better switch to a paid host...


Here the info from virtuallave:
How do I use the mail program?
The mail program is located at /var/qmail/bin/qmail-inject
. You would use it the same way you use "SendMail", by piping things into it. Note, you can also continue using the sendmail program at /usr/sbin/sendmail.

jobesoft
March 2nd, 2001, 12:09
Did you set an correct recipient???

Iam using virtualave and are happy with them. Fast, CGI-Bin & SSL (not SSI) .
My Formmail worked correctly. Try the formmail script which you can download at virtualaves Member Menue.

akersche
March 2nd, 2001, 12:23
hi,
i still don't know why this script, i first used is not working.
but i tried a diffrent one. as you said i tried the one from virtuallave and now it's working:))

and i even managed to write a diffrent script on my own:-)

Is working great now (except that one script i tried first, but it's ok).

Greetings and thanks for your help.
Arno