Solved

Email Form contents - cgi/perl - giving errors

Posted on 2003-11-12
8
490 Views
Last Modified: 2013-12-25
Hello experts....

I am a total newbie to cgi/perl programming. A friend wanted to email the contents of a feedback form to his email address. The server is Apache cgi server.

I found help with the code I needed on EE, and I customised to my needs. I wrote the code in perl and saved the file as .cgi

The code for email.cgi is:
---------------
#!/usr/bin/perl
# mailform.cgi - email form contents to webmaster

# define a variable that accepts a value from the form
$in;

# assign values to the variable depending on form METHOD
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
$in = $ENV{'QUERY_STRING'};
}
else {
$in = <STDIN>;
}

# fix URL-encoded strings
$in =~ s/\+/ /g;

# all variables are passed to the script as name-value pairs separated by &
# split the input string on the basis of &
@detail = split (/&/, $in);

# open mail pipe

open (MAIL,"|/usr/lib/sendmail -t") || &ErrorMessage;
print MAIL "To: myname@domain.com\n";
print MAIL "From: Feedback Form Mailer\n";
print MAIL "Subject: Feedback on your site\n\n";
print MAIL "Here is the result of your feedback form.\n\n";

foreach $details(@detail)
{
    %details = split (/=/, $details);
    while (($name, $value) = each %details)
    {
print MAIL "$name: $value\n";
    }
}

close MAIL;

print "Content-Type: text/html\n\n";
print "<center><font face=Arial size=+1>Thank you for your
feedback!</font></center>";

sub ErrorMessage{
      print "There is a problem with ther server "
}

---------------

Just to make sure, I wanted to confirm that:
1. We found the cgi-bin in /apache folder. That's where i store my email.cgi right?
2. When we hit submit on the form, we got an internal server error. The error log file showed the following:
[Sun Nov  9 10:19:08 2003] [error] (7)Arg list too long: exec of
/usr/local/apache/cgi-bin/emailform.cgi failed
[Sun Nov  9 10:19:08 2003] [error] [client 141.155.142.47] Premature
end of script headers: /usr/local/apache/cgi-bin/emailform.cgi

I then tried running a simple hello world program as:
#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<B>All is ok</B>\n";

But again i got an error as:
141.155.142.47 - - [09/Nov/2003:11:12:34 -0500] "GET /cgi-bin/test.cgi
HTTP/1.1" 500 617

I would appreciate if someone could help me with this.... a hello world program not running is frustrating!

Thanx...
0
Comment
Question by:poshlivin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 5

Expert Comment

by:djplaistow
ID: 9732772
1. Yes, Apache appears to have found the script just fine, so you have it in the right place.
2. The "Premature end of script headers" error in these situations is normally due to something being printed to STDOUT before the Content-type line or a missing blank line after the Content-type line. However, in both your examples it appears to be correct. Then I would guess that there is an error message being printed to STDOUT before the Content-type line. Run the scripts from the command line and see if there are any error messages. Post your findings.
0
 

Author Comment

by:poshlivin
ID: 9733518
I also have a cgi-bin folder in /usr/local/bin - do I need to store my files there?

Also, would adding
$|++;
use CGI::Carp qw( fatalsToBrowser );
open( STDERR, ">&STDOUT" );
 
after #!/usr/bin/perl line help?

I don't have access to ther server, but I am going to ask the admin to run the script from the command prompt and post the findings here.
0
 
LVL 5

Accepted Solution

by:
djplaistow earned 125 total points
ID: 9733855
Where you put the CGI scripts depends on how Apache is configured. The configuration file is most likely called httpd.conf but could be something different. You should see a line in there that looks about like

     ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"

The second argument to the ScripAlias directive is where you put your CGI scripts. However, the error message that you posted does not say that it couldn't find the script, it says that it had a problem executing it. Thus, I am sure it found it. Rename or move the script so Apache can't find it and try the URL again. You should notice the error message changed.

Yes CGI::Crap might help you. You may also try the following after the shebang line:

$SIG{__DIE__} = \&Error_Msg;

sub Error_Msg
{
  $msg = "@_";
  print "Content-type: text/html\n\n";
  print "The following error occurred : $msg\n";
  exit;
}

Also, try making the Content-type line the very first line after the shebang line.

It may not matter that you don't have access to the server, try and execute them on your local box, and see if there is some obvious error. If you don't get an error than let the admin run them for you.

Also, get rid of the code that is attempting to get the form variables and replace it with:

    use CGI qw(:cgi-lib);
    ReadParse();

After these 2 lines of code have finished, you will have all the form variables stored in the hash name %in. They can be accessed like:

    my $varValue = $in{"variableName"};
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:poshlivin
ID: 9748365
Thanx djplaistow.

I put the error message line after the shebang line and the error did get solved (it had to do with line breaks - nothing that we anticipated). Apparently, the admin had to put special line breaks since he was using Unix on a  Mac.

Anyways, when we tried executing the script, we did get a "thank you for your feedback" message which implied there were no errors. Also, the log file didn't show any error logs. But it's been almost 24 hours and we haven't received the mail as yet.

Right now I set it as:
print MAIL "To: myname\@domain.com; colleague1\@domain.com\n";

1 Do you think the order of the To, From, etc matters?
2 Also, should the From line necessarily contain an email address in it?
3 Sendmail should normally work right? Since I didn't change the $in = <STDIN> line, would that have mattered?

Thanx again.
0
 

Author Comment

by:poshlivin
ID: 9775799
Sendmail is still not giving any errors and not sending any mails either.
Any ideas?
0
 

Author Comment

by:poshlivin
ID: 9780160
I substitued the <STDIN> with the following code
use CGI;

$form = new CGI;

my $issue          = $form->param('Issue')     || die "no Issue supplied:$! ";
(continue for all fields in form)

Now I get
nobody.....Recipient names must be specified >html>
and then
Thank you for your feedback

But no mail is sent out.
0
 

Author Comment

by:poshlivin
ID: 9780209
djplaistow
You told me to replace the line that attempts to read the form variables with
 use CGI qw(:cgi-lib);
    ReadParse();

Since I am totally new to perl,which lines should I exactly change and where do i put the code?
 use CGI qw(:cgi-lib);
    ReadParse();

Do I have to assign %in = ReadParse()
0
 

Assisted Solution

by:million1977
million1977 earned 75 total points
ID: 9908392
I have a similar problem-
see http://www.experts-exchange.com/Web/Web_Languages/CGI/Q_20821196.html

Maybe answers given on my page will help you.
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question