Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send Mail Perl Script

Status
Not open for further replies.

evr72

MIS
Dec 8, 2009
265
US
Hello,

I have a user that has a web form, the form calls
Code:
<form ACTION="cs01.cgi" METHOD="POST">

this was working well until the user changed the email address in the cs01.cgi
Now when the form gets filled out and then submited it gets the following error
Code:
Not Found
The requested document was not found on this server.

The file structure has not changed, the file is there what I have tried to do is change the path for the "cs01.cgi"
to
Code:
<form ACTION="cgi-bin/cs01.cgi" METHOD="POST">
with this I get the following error
Code:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.

One thing that looks a bit odd to me, is the way the email is written in the "cs01.cgi"

Code:
print MAIL "From: test.testing\@mycompany.com\n";

even if I enter the old email address it still gives me the same errors not very familiar with cgi nor perl.

Any help is much appreciated!!!



 
Hi,

Firstly its a little difficult to provide a definitive solution without being able to see your full code.

You managed to get as far as recieving an Internal Server Error. It just doesn't explicitly explain what the problem is. You can check your server error log, use CGI::Carp to display the error(s) in the browser or use a basic syntax checker.

Chris
 
so would I put the
use CGI::Carp
at the beginning of the code?
Unfortunately I do not have a backup I will ask if the webhost site has one.

Here is the code
Code:
#!/usr/bin/perl

print "Content-type:text/html\n\n";

#$passwd = random_password();

#sub random_password {
# Generate a random password. Alternates vowels and
# consonants, for maximum pronounceability.  Uses its own
# list of consonants which exclude F and C and K to prevent
# generating obscene-sounding passwords. Capital I and
# lowercase L are excluded on the basis of looking like
#each other.
#	($length) = @_;
#	if ($length eq "" or $length < 3) {
#		$length = 6;		# make it at least 6 chars long.
#	}
#	$vowels = "aeiouyAEUY";
#	$consonants = "bdghjmnpqrstvwxzBDGHJLMNPQRSTVWXZ12345678";
#	srand(time() ^ ($$ + ($$ << 15)) );
#	$alt = int(rand(2)) - 1;
#	$s = "";
#	$newchar = "";
#	foreach $i (0..$length-1) {
#		if ($alt == 1) {
#			$newchar =
#substr($vowels,rand(length($vowels)),1);
#		} else {
#			$newchar = substr($consonants,
#				rand(length($consonants)),1);
#		}
#		$s .= $newchar;
#		$alt = !$alt;
#	}
#	return $s;
#}


read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
}
$mailprog = '/usr/lib/sendmail';

$recipient = $FORM{'emailaddress'};


open (MAIL, "|$mailprog -t") or &dienice("Can't access $mailprog!\n");


print MAIL "To: $recipient\n";

print MAIL "From: useremail\@mycompany.com\n";

print MAIL "Reply-to:  useremail\@mycompany.com\\n";

print MAIL "Subject: Spare Parts Form Results\n\n";

print MAIL  <<End1;


Thank you for your inquiry.  A sales representative will review the information submitted and will contact you.

This is the information you submitted.

MACHINE DETAILS:
=================
W.O. Number:   $FORM{'wonumber'}
Machine Type:          $FORM{'machinetype'}
Product Type:    $FORM{'producttype'}
Machine is in Warranty:   $FORM{'machineis'}
Line is Currently:   $FORM{'currentline'}


CUSTOMER DETAILS:
====================
Company Name:  $FORM{'companyname'}
Contact Name:  $FORM{'contactname'}
Phone Number:  $FORM{'phonenumber'}
Fax Number:  $FORM{'faxnumber'}
E-mail Address:  $FORM{'emailaddress'}
Mailing Address:  $FORM{'mailingaddress'}


SPARE PARTS:
============
Warranty:   $FORM{'warranty'}
Quote:  $FORM{'quote'}
Order Number:  $FORM{'ordernumber'}
Per Our Quote:  $FORM{'perquote'}
P.O.:   $FORM{'po'}
Quantity:   $FORM{'qty'}
Ceeco Part Number or Drawing Number:  $FORM{'ceecopart'}
Description:   $FORM{'description'}
OEM Part Number:   $FORM{'oempart'}
Documents:    $FORM{'documents'}
Comments:   $FORM{'comments'}

End1

print <<ENDHTML;
<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<title>Syncro Spare Parts &amp; Technical Documentation</title>
		<meta name="generator" content="Microsoft FrontPage 5.0">
		<script><!--
CSStopExecution = false;
CSInit = new Array;
function CSScriptInit() {
idxArray = new Array;
for(var i=0;i<CSInit.length;i++)
	idxArray[i] = i;
CSAction2(CSInit, idxArray);
}
CSAg = window.navigator.userAgent; CSBVers = parseInt(CSAg.charAt(CSAg.indexOf("/")+1),10);
function IsIE() { return CSAg.indexOf("MSIE") > 0;}
function CSIEStyl(s) { return document.all.tags("div")[s].style; }
function CSNSStyl(s) { return CSFindElement(s,0); }
function CSFindElement(n,ly) { if (CSBVers < 4) return document[n];
	var curDoc = ly ? ly.document : document; var elem = curDoc[n];
	if (!elem) { for (var i=0;i<curDoc.layers.length;i++) {
		elem = CSFindElement(n,curDoc.layers[i]); if (elem) return elem; }}
	return elem;
}
function CSButtonReturn () {
	var bAgent = window.navigator.userAgent;
	var bAppName = window.navigator.appName;
	if ((bAppName.indexOf("Explorer") >= 0) && (bAgent.indexOf("Mozilla/3") >= 0) && (bAgent.indexOf("Mac") >= 0))
		return false; // follow link
	else return true; // follow link
}
CSIm = new Object();
function CSIShow(n,i) {
	if (document.images) {
		if (CSIm[n]) {
			var img = (!IsIE()) ? CSFindElement(n,0) : document[n];
			if (img && typeof(CSIm[n][i].src) != "undefined") {img.src = CSIm[n][i].src;}
			if(i != 0)
				self.status = CSIm[n][3];
			else
				self.status = " ";
			return true;
		}
	}
	return false;
}
function CSILoad(action) {
	im = action[1];
	if (document.images) {
		CSIm[im] = new Object();
		for (var i=2;i<5;i++) {
			if (action[i] != '') { CSIm[im][i-2] = new Image(); CSIm[im][i-2].src = action[i]; }
			else CSIm[im][i-2] = 0;
		}
		CSIm[im][3] = action[5];
	}
}
CSStopExecution = false;

function CSAction(array) {
	return CSAction2(CSAct, array);
}
function CSAction2(fct, array) {
	var result;
	for (var i=0;i<array.length;i++) {
		if(CSStopExecution) return false;
		var actArray = fct[array[i]];
		var tempArray = new Array;
		for(var j=1;j<actArray.length;j++) {
			if((actArray[j] != null) && (typeof(actArray[j]) == "object") && (actArray[j].length == 2)) {
				if(actArray[j][0] == "VAR") {
					tempArray[j] = CSStateArray[actArray[j][1]];
				}
				else {
					if(actArray[j][0] == "ACT") {
						tempArray[j] = CSAction(new Array(new String(actArray[j][1])));
					}
				else
					tempArray[j] = actArray[j];
				}
			}
			else
				tempArray[j] = actArray[j];
		}
		result = actArray[0](tempArray);
	}
	return result;
}
CSAct = new Object;

// --></script>
		<csactiondict>
		<script><!--
CSInit[CSInit.length] = new Array(CSILoad,/*CMP*/'Energy',/*URL*/'en01.gif',/*URL*/'en02.gif',/*URL*/'en03.gif','');
CSInit[CSInit.length] = new Array(CSILoad,/*CMP*/'Telecom',/*URL*/'te01.gif',/*URL*/'te02.gif',/*URL*/'te03.gif','');
CSInit[CSInit.length] = new Array(CSILoad,/*CMP*/'Flexible Pipe',/*URL*/'fp01.gif',/*URL*/'fp02.gif',/*URL*/'fp03.gif','');
CSInit[CSInit.length] = new Array(CSILoad,/*CMP*/'Fiber Optic',/*URL*/'fo01.gif',/*URL*/'fo02.gif',/*URL*/'fo03.gif','');
CSInit[CSInit.length] = new Array(CSILoad,/*CMP*/'Ancillary',/*URL*/'an01.gif',/*URL*/'an02.gif',/*URL*/'an03.gif','');
CSInit[CSInit.length] = new Array(CSILoad,/*CMP*/'button4',/*URL*/'navc01.gif',/*URL*/'navc02.gif',/*URL*/'','');
CSInit[CSInit.length] = new Array(CSILoad,/*CMP*/'button5',/*URL*/'navc01.gif',/*URL*/'navc02.gif',/*URL*/'','');

// --></script>
		</csactiondict>
		<link href="fonts.css" rel="styleSheet" type="text/css">
		<style type="text/css"><!--
			a { color: #039; text-decoration: none }-->
		</style>
	</head>

	<body bgcolor="#e7e7e7" leftmargin="0" rightmargin="0" topmargin="0" marginwidth="0" marginheight="0" link="#003399" alink="#ff9900" vlink="#003399" background="sybkgrnd.gif" onload="CSScriptInit();">
		<table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr bgcolor="#ff9900">
				<td colspan="8" bgcolor="white">
                <a href="index.htm">
                <img border="0" src="images/ceecobartelllogo.jpg" width="309" height="64"></a></td>
			</tr>
			<tr height="14" valign="top">
				<td height="14" bgcolor="#ff9900" width="39"><img height="14" width="30" src="spacer.gif" align="top"></td>
				<td height="14" bgcolor="#ff9900" align="left" width="444"><img height="14" width="117" src="spacer.gif" align="top"></td>
				<td width="79" rowspan="2" bgcolor="#003399"><a href="en_top.html" target="_top" onmouseover="return CSIShow(/*CMP*/'Energy',1)" onmouseout="return CSIShow(/*CMP*/'Energy',0)" onclick="CSIShow(/*CMP*/'Energy',2);return CSButtonReturn()">
                <img src="en01.gif" name="Energy" border="0" width="79" height="16"></a></td>
				<td width="83" rowspan="2" bgcolor="#003399"><a href="te_top.html" target="_top" onmouseover="return CSIShow(/*CMP*/'Telecom',1)" onmouseout="return CSIShow(/*CMP*/'Telecom',0)" onclick="CSIShow(/*CMP*/'Telecom',2);return CSButtonReturn()">
                <img src="te01.gif" name="Telecom" border="0" width="83" height="16"></a></td>
				<td width="128" rowspan="2" bgcolor="#003399"><a href="fp_top.html" target="_top" onmouseover="return CSIShow(/*CMP*/'Flexible Pipe',1)" onmouseout="return CSIShow(/*CMP*/'Flexible Pipe',0)" onclick="CSIShow(/*CMP*/'Flexible Pipe',2);return CSButtonReturn()">
                <img src="fp01.gif" name="Flexible Pipe" border="0" width="128" height="16"></a></td>
				<td width="117" rowspan="2" bgcolor="#003399"><a href="fo_top.html" target="_top" onmouseover="return CSIShow(/*CMP*/'Fiber Optic',1)" onmouseout="return CSIShow(/*CMP*/'Fiber Optic',0)" onclick="CSIShow(/*CMP*/'Fiber Optic',2);return CSButtonReturn()">
                <img src="fo01.gif" name="Fiber Optic" border="0" width="117" height="16"></a></td>
				<td width="104" rowspan="2" bgcolor="#003399" valign="top"><a href="an_top.html" target="_top" onmouseover="return CSIShow(/*CMP*/'Ancillary',1)" onmouseout="return CSIShow(/*CMP*/'Ancillary',0)" onclick="CSIShow(/*CMP*/'Ancillary',2);return CSButtonReturn()">
                <img src="an01.gif" name="Ancillary" border="0" width="104" height="16"></a></td>
				<td bgcolor="#ff9900" height="14" width="30">
					<div align="right">
						<img height="14" width="30" src="spacer.gif" align="top"></div>
				</td>
			</tr>
			<tr height="2">
				<td bgcolor="white" height="2" width="39"><img height="2" width="30" src="spacer.gif" align="top"></td>
				<td height="2" bgcolor="white" width="444"><img height="2" width="117" src="spacer.gif"></td>
				<td bgcolor="white" width="30" height="2"><img height="2" width="30" src="spacer.gif"></td>
			</tr>
			<tr bgcolor="#003399" height="28">
				<td width="39" height="28" bordercolor="#003399">
                <img height="30" width="39" src="spacer.gif" align="top"></td>
				<td height="28" colspan="7" width="985"><img height="30" width="117" src="spacer.gif" align="top"></td>
			</tr>
			<tr bgcolor="#6699ff" height="2">
				<td colspan="8" height="2"><img height="2" width="125" src="spacer.gif"></td>
			</tr>
		</table>
		<table width="743" border="0" cellpadding="0" cellspacing="0" height="259">
			<tr height="1">
				<td width="1" height="1"></td>
				<td width="240" height="1"><spacer type="block" width="240" height="1"></td>
				<td width="500" height="1"><spacer type="block" width="500" height="1"></td>
			</tr>
			<tr height="30">
				<td width="1" height="30"><spacer type="block" width="1" height="30"></td>
				<td width="240" height="258" colspan="1" rowspan="4" valign="top" align="left" xpos="0">
					<table border="0" cellpadding="0" cellspacing="0" width="220">
						<tr>
							<td colspan="2"><img height="30" width="200" src="spacer.gif"></td>
						</tr>
						<tr valign="top">
							<td colspan="2"><a href="cs_main.html" target="_self"><img height="15" width="20" src="spacer.gif" border="0"><span class="nav3">Customer Service</span></a></td>
						</tr>
						<tr valign="top">
							<td colspan="2"><img height="15" width="125" src="spacer.gif"></td>
						</tr>
						<tr valign="top">
							<td width="30"><a href="#" onmouseover="return CSIShow(/*CMP*/'button4',1)" onmouseout="return CSIShow(/*CMP*/'button4',0)" onclick="return CSButtonReturn()">
                            <img src="navc01.gif" name="button4" border="0" width="30" height="15"></a></td>
							<td valign="middle" width="170"><a href="cs_01.html" target="_self"><span class="nav3">Ceeco
                            Bartell Spare Parts</span></a></td>
						</tr>
						<tr valign="middle" height="8">
							<td colspan="2" height="8"><img height="15" width="125" src="spacer.gif"></td>
						</tr>
						<tr valign="top">
							<td width="30"><a href="#" onmouseover="return CSIShow(/*CMP*/'button5',1)" onmouseout="return CSIShow(/*CMP*/'button5',0)" onclick="return CSButtonReturn()">
                            <img src="navc01.gif" name="button5" border="0" width="30" height="15"></a></td>
							<td width="170" valign="middle"><a href="cs_02.html" target="_self"><span class="nav3">Ceeco
                            Bartell Service &amp;<br>
								Troubleshooting</span></a></td>
						</tr>
						<tr height="8">
							<td colspan="2" height="8"><img height="15" width="125" src="spacer.gif"></td>
						</tr>
						<tr height="15">
							<td width="30" height="15" valign="top">
                            <img src="navc02.gif" width="30" height="15"></td>
							<td width="170" height="15" valign="middle"><span class="nav3">Syncro Spare Parts &amp;<br>
								Technical Documentation</span></td>
						</tr>
					</table>
				</td>
				<td width="500" height="30"></td>
			</tr>
			<tr height="50">
				<td width="1" height="50"><spacer type="block" width="1" height="50"></td>
				<td width="500" height="50" colspan="1" rowspan="1" valign="top" align="left" xpos="240" content csheight="50">
					<p class="subhead"><font size="2">Confirmation</font></td>
			</tr>
			<tr height="67">
				<td width="1" height="67"><spacer type="block" width="1" height="67"></td>
				<td width="500" height="67" colspan="1" rowspan="1" valign="top" align="left" xpos="240" content csheight="67">
                <font SIZE="2" face="Verdana">Thank you for your inquiry. A
                sales representative will review the information submitted and
                will contact you.</font><p>&nbsp;</td>
			</tr>
			<tr height="1153">
				<td width="1" height="111"><spacer type="block" width="1" height="1153"></td>
				<td width="500" height="111" colspan="1" rowspan="1" valign="top" align="left" xpos="240">
					<table border="0" cellpadding="0" cellspacing="0" width="500">
						<tr>
							<td><img height="20" width="100" src="spacer.gif"></td>
						</tr>
						</table>
				</td>
			</tr>
		</table>
	</body>

</html>


ENDHTML

$mailprog = '/usr/lib/sendmail';

$recipient = " useremail\@mycompany.com\";


open (MAIL, "|$mailprog -t") or &dienice("Can't access $mailprog!\n");


print MAIL "To: $recipient\n";

print MAIL "From: $FORM{'emailaddress'} ($FORM{'contactname'})\n";

print MAIL "Reply-to: $FORM{'emailaddress'} ($FORM{'contactname'})\n";

print MAIL "Subject: Ceeco Bartell Spare Parts Form Results\n\n";

print MAIL  <<End1;

MACHINE DETAILS:
=================
W.O. Number:   $FORM{'wonumber'}
Machine Type:          $FORM{'machinetype'}
Product Type:    $FORM{'producttype'}
Machine is in Warranty:   $FORM{'machineis'}
Line is Currently:   $FORM{'currentline'}


CUSTOMER DETAILS:
====================
Company Name:  $FORM{'companyname'}
Contact Name:  $FORM{'contactname'}
Phone Number:  $FORM{'phonenumber'}
Fax Number:  $FORM{'faxnumber'}
E-mail Address:  $FORM{'emailaddress'}
Mailing Address:  $FORM{'mailingaddress'}


SPARE PARTS:
============
Warranty:   $FORM{'warranty'}
Quote:  $FORM{'quote'}
Order Number:  $FORM{'ordernumber'}
Per Our Quote:  $FORM{'perquote'}
P.O.:   $FORM{'po'}
Quantity:   $FORM{'qty'}
Ceeco Part Number or Drawing Number:  $FORM{'ceecopart'}
Description:   $FORM{'description'}
OEM Part Number:   $FORM{'oempart'}
Documents:    $FORM{'documents'}
Comments:   $FORM{'comments'}

End1


close(MAIL);

sub dienice {
    ($errmsg) = @_;
    print "<h2>Error</h2>\n";
    print "$errmsg<p>\n";
    print "</body></html>\n";
    exit;
}
 
audiopro,

Thanks for the advice and sorry for the ignorance, but where do I close the emalprog?

Thanks!!
 
Also my question still remains of why would I get a
Code:
Not Found
The requested document was not found on this server.
when on the web form itself shows
Code:
<form ACTION="cs01.cgi" METHOD="POST">
 
The mailprog is called twice, once to send an email to the person completing the form and the second to you.
Each one calls the mailprog with
Code:
open (MAIL, "|$mailprog -t") or &dienice("Can't access $mailprog!\n");

but only the second one issues a close statment.
Code:
close(MAIL);

put another close after the forst instance of
Code:
End1

You would only get a file not found if either the file doesn't exist or the path to it is incorrect.
Where is the .cgi file relative to where the HTML is called?
Is the file name cs01.cgi or csO1.cgi, (is the 0 a zero or capital o)?

Keith
 
the .cgi file lives in the cgi-bin
so all my files are in the root folder then there is a
cgi-bin folder.
the cs01.cgi is a zero one
 
If the cgi file is being called from outside the cgi-bin, I would expect the form action to be

Code:
cgi-bin/cs01.cgi

Does the full path look correct in the address bar of the error screen?

Keith
 
when we do the
Code:
cgi-bin/cs01.cgi
we get this

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

the link looks like this
Code:
[URL unfurl="true"]http://www.mycompany.com/cgi-bin/cs01.cgi[/URL]


when we just do the
Code:
cs01.cgi

we get this error

Not Found
The requested document was not found on this server.

the link looks like this

Code:
[URL unfurl="true"]http://www.mycompany.com/cs01.cgi[/URL]
 
Code:
[URL unfurl="true"]http://www.mycompany.com/cgi-bin/cs01.cgi[/URL]
is the correct version although you still have an error in your script somewhere, did you remove the slash from the email address?.

As advised earlier, using the Carp module would have thrown am syntax error - whether you could fathom out what the styntax error actually was is another matter as the error reports are not very descriptive.

It is also good practice to use the 'strict' pragma to trap those silly errors we all make. Paset this at the top of your script
Code:
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use strict;
print "Content-type:text/html\n\n";
my $buffer;
my $pair;
my @pairs;
my $name;
my $value;
my $mailprog;
my $recipient;
my %FORM;
my $errmsg;
before
Code:
print "Content-type:text/html\n\n";
and that should sort it.

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top