General discussion

Locked

CGI Script

By wolfbanes ·
I have created a simple feedback form for a website with the basic MAILTO: action to send the responses via e-mail. The problem is that the answers arrive as an attachment that isn't associated with any application and when opened in Wordpad looks like jibberish. I can translate it, but it's not what I want. I know that people use CGI scripts to get these responses sent as a readable e-mail. Can anyone lead me through the process of creating one? I know it needs to be put in the cgi-bin folder,but beyond that I'm pretty confused. I'm certainly no programmer. I'd really appreciate your help.

This conversation is currently closed to new comments.

15 total posts (Page 1 of 2)   01 | 02   Next
| Thread display: Collapse - | Expand +

All Comments

Collapse -

CGI Script

by Joseph Moore In reply to CGI Script

Try this article, "Running a form-to-email gateway"

http://www.lies.com/begperl/form_to_email.html

It has all the sample HTML and CGI script you could want to do this.

hope this helps

Collapse -

CGI Script

by wolfbanes In reply to CGI Script

Helpful site. Thanks!

Collapse -

CGI Script

by nikki96 In reply to CGI Script

This is a CGI Perl mail handler template I use. I authored it. It's customizable and works (after you customize it with email addy and mail server and so on). Also, a sample html page for calling it. Script must be modified with proper path to Perl,sendmail, and email addresses. Get these from your admin. Watch for line breaks in the pasted code below. Hope it helps.

Collapse -

CGI Script

by nikki96 In reply to CGI Script

Sample HTML:
******************************************

<html>
<head>
<title>Test for feedback form</title>
</head>

<body>
<p> <br></p>

<form name="feedbackForm" action="cgi-bin/formhandler.cgi" method="get">
Text1: <input type="text" name="myTxt1" id="myTxt1" value=""><br>
<input type="hidden" name="myTxt1_required" id="myTxt1_required" value="Text 1 is required!">
Text2: <input type="text" name="myTxt2" id="myTxt2" value=""><br>
Select1: <select name="mySelect" id="mySelect">
<option value="One" selected>One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
</select><br>
<pre>
RadioSet1: <input type="radio" name="myRadio1" id="myRadio1" value="one">1<br>
<input type="radio" name="myRadio1" id="myRadio1" value="two">2<br>
<input type="radio" name="myRadio1" id="myRadio1" value="three">3<br>
</pre>
<input type="hidden" name="myRadio1_required" id="myRadio1_required" value="MyRadio1 is required!">
<input type="checkbox" name="myCheck1" id="myCheck1" value="one">1<br>
<input type="checkbox" name="myCheck2" id="myCheck2" value="two">2<br>
<input type="hidden" name="myCheck2_required" id="myCheck2_required" value="MyCheck2 is required!">
<input type="submit" value="submit">
</form>
</body>
</html>

Collapse -

CGI Script

by nikki96 In reply to CGI Script

*****************************************
Perl script
*****************************************

#!/eesd/ofc/bin/perl -I/eesd/ofc/lib/perl/lib -I /home/ledtools/ofc/perl/lib

#########################################################################
#
# formhandler.cgi
#
# script author: M. Nikole Hunn
#
#########################################################################

use strict;
use CGI qw(:all);

# In will have all form variables. Do not edit.
my %In = ();
my $key;

#########################################################################
# customizable variables

# Set this to the name of the output file (if any), full path from machine root.
# If no file is to be used, use the empty string ("").
# Example: my $outFileName = "/home/myDir/myOtherDir/myFilename";
my $outFileName = "";

# Set this to the name of the outgoing mail server, if using mail option.
# Leave blank if no mail.
# Example: my $mailServer = "myMailServer.com";
my $mailServer = "";

# Set this to the list of primary addresses to mail the form data to.
# Leave blank if no mail ("").
# Some systems require this to backslash-escape the @ sign.
# Example: my $mailList = "me\@myISP.com";
my $mailList = "";

## NOTE: to use bcc and cc, a primary mailList must be specified as well.

# Set this to the list of cc addresses to mail the form data to.
# Leave blank if no cc ("").
# Some systems require this to backslash-escape the @ sign.
# Example: my $ccList = "them\@theirISP.com";
my $ccList = "";

# Set this to the list of bcc addresses to mail the form data to.
# Leave blank if no bcc ("").
# Some systems require this to backslash-escape the @ sign.
# Example: my $bccList = "them\@theirISP.com";
my $bccList = "";

Collapse -

CGI Script

by nikki96 In reply to CGI Script

###Perl script continued...

# Set this to the page (full http path) to be used as a success page.
# Leave blank if no redirect desired. ("")
# Example my $successPage = "http://www.myServer.com/myPage.asp";
my $successPage = "";

# Set this to the page (full http path) to be used as a failure page.
# Leave blank if no redirect desired. ("")
# Error messages are passed to the page via the URL to be parsed out if desired
# Example my $failurePage = "http://www.myServer.com/myPage.asp";
my $failurePage = "";

#################################################################################
# begin form processing

# Get the data that was supposed to be passed in from the form
parse();

# Check for _required vars - all form elements that are required have a hidden form element passed in with the name
# varName_required. I.E. if one has a field called "address" and wants it to be non-blank, one also has a hidden
# form field called "address_required". The value ofthat hidden form field is the error message to be output.
my $loc;
my $vr;
my $key2;
my $found=0;

foreach $key (sort keys(%In))
{
$loc=index($key,"_required");
if ($loc > 0)
{
$found=0;
$vr=substr($key,0,$loc);
foreach $key2 (sort keys(%In))
{
if ($key2 eq $vr)
{
$found=1;
last;
}
}
if ($found == 1)
{
$_ = $In{$vr};
if (/^\s*$/) {
printError ($In{vr}, $failurePage);
}
}
else
{
# Radio buttons and checkboxes send nothing if left blank.
printError ($In{$vr."_required"}, $failurePage); }
}
}

Collapse -

CGI Script

by nikki96 In reply to CGI Script

### Perl script cont...



# Process form elements as needed.
if ($outFileName ne "")
{
open(MYFILE, ">$outFileName") || printError ("Can't open $outFileName", $failurePage);
flock(MYFILE, 2);
foreach $key (sort keys(%In)) {
print MYFILE "$key = $In{$key}\n";
}
close(MYFILE);
}

if ($mailList)
{
open (MESSAGE,"| /usr/lib/sendmail -t");
print MESSAGE "To: $mailList\n";
if ($ccList)
{
print MESSAGE "Cc: $ccList\n";
}
if ($bccList)
{
print MESSAGE "Bcc: $bccList\n";
}
print MESSAGE "From: $In{eMail}\n";
print MESSAGE "Reply-to: $In{eMail}\n";
print MESSAGE "Subject: Feedback form info\n\n";
foreach $key (sort keys(%In))
{
print MESSAGE "$key = $In{$key}\n";
}
close (MESSAGE);
}

printSuccess($successPage);

## end main script ##
##################################################################################

sub parse
{
# parse the input from the form
# full stream in $buffer of all form elements
my $buffer;
if ($ENV{REQUEST_METHOD} eq 'GET')
{
$buffer = $ENV{QUERY_STRING};
}
else
{
read(STDIN,$buffer,$ENV{CONTENT_LENGTH});
}
# the ampersand is a special char that splits form elements
# valArray will have the variable / value pairs
my @valArray = split(/&/,$buffer);
# trans special characters and url vars

Collapse -

CGI Script

by nikki96 In reply to CGI Script

## perl script cont...
foreach(@valArray)
{
$_ =~ tr/+/ /;
my ($var,$val) = split(/=/,$_,2);
$var =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$val =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$val =~ s/(\<.*?)(embed|object|script|applet)(.*?\>)/$1$3/gis;
# put variable and value into associative array (tab separated if key var already exists)
if($In{$var})
{
$In{$var} .= "\t$val";
#print "$var = $In{$var}\n";
}
else
{
$In{$var} = $val;
#print "$var = $In{$var}\n";
}
}
}

sub printTop
{
print header;
print start_html("form submission");
print br();
}

sub printError
{
my ($errMsg, $errPage) = @_;
if ($errPage)
{
print ("Location: $errPage?Err=$errMsg\n\n");
exit;
}
else
{
printTop();
print ("Error!<br>");
print (@_);
print "\n";
print end_html;
exit;
}
}

sub printSuccess
{
my ($successPage) = @_;
if ($successPage)
{
print ("Location: $successPage\n\n");
exit;
}
else
{
printTop();
print ("Success!");
print end_html;
exit;
}
}

sub printDebug
{
print (@_);
print "\n";
}

## end of script

Collapse -

CGI Script

by nikki96 In reply to CGI Script

Boy, that didn't hold the formatting well, did it?
If you want it as a text file with proper indentation and line breaks, you can e-mail me at hunn@lucent.com and I'll send it to you.

Collapse -

CGI Script

by wolfbanes In reply to CGI Script

You went way WAY over my head but you put in enough effort to earn points. Thanks!

Back to Web Development Forum
15 total posts (Page 1 of 2)   01 | 02   Next

Related Discussions

Related Forums