Link to home
Start Free TrialLog in
Avatar of rngrfan
rngrfan

asked on

Need CGI EMAIL help for a newbie trying to create a simple web form

I am attempting to create a simple form on a web site for my brother that will email some customer information.

Heres what I have so far

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>

<FORM METHOD="POST"
ACTION="http://ankerpools/public_html/cgi-bin/simpleform.txt">

Your e-mail address: <INPUT NAME="email"><p>
Your name: <INPUT NAME="yourname"><p>
<INPUT TYPE="submit" value="Send e-mail">
</FORM>
     
 
</BODY>
</HTML>

I copied the action line from some MIT manual that I found and tried to convert to my brothers info.  Not real sure I did that right.

I also created the simpleform.txt file which I uploaded to the public_html/cgi-bin folder on the web server.  I was just guessing this was the right spot but not real sure.

The simpleform.txt looks like this

To:test@ankerpools.com                          HEADER LINES
Subject: Two Questions
                                                 
What is your email?              [email]          BODY
What is your name?             [yourname]          


Can someone please help me and tell me what I am doing wrong.   Don't know I if is extremely difficult for you but it is for me so 500pts.

Thanks in advance for your help and patience with me on this.
Avatar of ahoffmann
ahoffmann
Flag of Germany image

the ACTION= attribute need to point to a CGI script (or any other active page on your web server) and not to a simple text file.
This script then needs to parse your form data and send the mail.
Your postes simpleform.txt looks more like an example how the generated mail might look like.
Avatar of divt
divt

rngrfan,

What you need is a CGI and a web form working together.
Please find below links that contain step-by-step instructions:

A Simple CGI Email Handler
http://www.boutell.com/email/

If you are not comfortable setting up your own CGI, check out this one:

Free Feedback form
http://freedback.com/

Hope this helps.
Avatar of rngrfan

ASKER

I need some help determining what the correct action needs to be first and then what to change the 2 line in the cgi email script.

When I log onto WS FTP to upload web pages I see that there is a cgiemail (binary file) in the /public_html/cgi-bin.  Is this the spot where my action needs to point?  If so should it read

<FORM METHOD="POST"
ACTION="http://ankerpools.com/cgi-bin/cgiemail">


Avatar of rngrfan

ASKER

OK I think I have the action part correct at http://ankerpools.com/cgi-bin/email.cgi"

Now for the changes that need to be made to the cgi script I am really confused.

Divt pointed me to the above script which I downloaded.


Configuring email.cgi
Just after the copyright notice in email.c, there are two very important definitions you just configure. The first defines the location of the sendmail utility on your system. The default setting works for many systems; however if email.cgi does not send email for you and displays no error message, it is highly likely that sendmail is located in a different place on your system, perhaps /sbin/sendmail or /usr/bin/sendmail. If you are not using a Unix-based server, you do not have sendmail and probably cannot use this script.




The second setting defines the location of your email configuration file, which you will want to set to the following:

my $emailConfPath = "/home/www/conf/email.conf"

Where you must replace "/home/www/conf" with the directory where you have decided to keep your email configuration file. Your email configuration file contains a list of allowed email recipients and the pages to which users should be redirected after email is sent to those recipients, on alternating lines. You can create this file with a text editor such as notepad or pico.
The second (new in version 2.1) defines the location of the email binary:

#define EMAIL_BINARY "/bin/mail"

If this is not the location of your email binary, change this #define appropriately. (Typing 'which mail' at your shell prompt may help you discover the correct location for your system.)
Avatar of rngrfan

ASKER

Please ignore the above comment.  Hit submit by accident.

OK I think I have the action part correct at http://ankerpools.com/cgi-bin/email.cgi"

+++++Now for the changes that need to be made to the cgi script I am really confused.

+++++Divt pointed me to the above script which I downloaded.


Configuring email.cgi
Just after the copyright notice in email.c, there are two very important definitions you just configure. The first defines the location of the sendmail utility on your system. The default setting works for many systems; however if email.cgi does not send email for you and displays no error message, it is highly likely that sendmail is located in a different place on your system, perhaps /sbin/sendmail or /usr/bin/sendmail. If you are not using a Unix-based server, you do not have sendmail and probably cannot use this script.

+++++MY SENDMAIL IS /USR/SBIN?SENDMAIL which looks to me to be the default


The second setting defines the location of your email configuration file, which you will want to set to the following:

my $emailConfPath = "/home/www/conf/email.conf"

+++++I have created an email.conf file and place it in  /public_html/conf and changed my email.cgi file to read    

+++++my $emailConfPath = "/public_html/conf/email.conf


The second (new in version 2.1) defines the location of the email binary:

#define EMAIL_BINARY "/bin/mail"

If this is not the location of your email binary, change this #define appropriately. (Typing 'which mail' at your shell prompt may help you discover the correct location for your system.)

+++++Is this the file in the /public_html/cgi-bin named cgiemail that is a binary file?

+++++I rent space through ACENET and some general serverinformation
 Server name xxxxxxxxxxxxxxxx
  WWW directory xxxxxxxxxxxxxxxxxxxxx
  Root directory xxxxxxxxxxxxxxxxxxxxxxx
  Path to PERL /usr/bin/perl  
  Perl modules Click to view  
  Path to sendmail /usr/sbin/sendmail  
  Apache version 1.3.33 (Unix)  
  MySQL version 4.0.23-standard  
  PHP version 4.3.10  
  PHP info Click to view  
  PERL version 5.8.4

+++++Shell prompt?  Is there something similar on windows that I can use?


Hope this makes some sense to someone because its a little foggy to me.
> #define EMAIL_BINARY "/bin/mail"
what's the filename wher you find this?

> Is this the file in the /public_html/cgi-bin named cgiemail that is a binary file?
do you mean that you have a file    /public_html/cgi-bin/cgiemail  ?
then please post the result of:
  file  /public_html/cgi-bin/cgiemail

> +++++Shell prompt?
what does this mean?

 rngrfan, if you're not used to all the things you have to do, please post verbatim what you've done, and what the result is you get. Posting a textual description of what you think it could/should be does not help much ('cause experts might think in another way;-)
Avatar of rngrfan

ASKER

ahoffmann sorry if this was unclear to you.  I was following instructions posted by divt at http://www.boutell.com/email/

my comment have +++++++ infront of them

the other comments I am having problem with come from the site above.  

If you need any info please let me know and I will post asap.


Avatar of rngrfan

ASKER

Im still lost and havent receive and answer sorry.
You just have to copy and paste the code to two files.
One named "my_sendmail.html" (which you can put into your DocumentRoot folder on your server),
and the other "my_sendmail.cgi" (which you have to put it into the cgi-bin folder into your server).

There is a chance to have to change something in your 'my_sendmail.html' file.
At the line where the  <form action="/cgi-bin/my_sendmail.cgi" method="POST"> is ,
you might need to change the path like this
<form action="/public_html/cgi-bin/my_sendmail.cgi" method="POST">
But first try it as it is.

First of all lets make your form look a little better

my_sendmail.html
---------------------------------
<html>
   <head>
      <title>A form to send mail</titlte>
   </head>
   <body>
   <p align="center"><b><h1>Send Email</h1></b>
      <br>
      Please fill all the fields
      <br><br>    
   </p>
   <table align=center width="80%" border="0" cellspacing="0">
      <form action="/cgi-bin/my_sendmail.cgi" method="POST">
            <tr>
             <td align=left>Your name: </td>
             <td align=left><input type="text" name="name"></td>
          </tr>
          
          <tr>
             <td align=left>Your e-mail address: </td>
             <td align=left><input type="text" name="sender"></td>
          </tr>
          
          <tr>
             <td align=left>Recipient's email: </td>
             <td align=left><input type="text" name="recipient"></td>
          </tr>

          <tr>
             <td align=left>Subject: </td>
             <td align=left><input type="text" name="subject"></td>
          </tr>
                   
          <tr>
             <td align=left>Message: </td>
             <td align=left><textarea name="message" cols=40 rows=20></textarea></td>
          </tr>
   </table>
   <p align="center"><input type="submit" name="send" value="Send"></p>
     </FORM>
   </body>
</html>



Now lets make your cgi file which will do a little 'kung fu' to do what you want


my_sendmail.pl
-------------------------
#!/usr/bin/perl -w
use strict;
use CGI;

my $query = new CGI;

my $name = $query->param('name');
my $sender = $query->param('sender');
my $recipient = $query->param('recipient');
my $subject = $query->param('subject');
my $message = $query->param('message');

if (($name eq "") || ($sender eq "") || ($message eq "") || ($recipient eq ""))
{
      &error("Email Rejected",
            "Please fill out all fields provided. Back up to the " .
            "previous page to try again.");
}

open (MAIL, "| /usr/sbin/sendmail -t");
print MAIL <<MyMail;
To: $recipient
From: $name <$sender>
Subject: $subject

$message;
.
MyMail
close MAIL;


sub error {
    # Output a valid HTML page as an error message
    my($title, $content) = @_;
    print $query->header;
#don't try to change the spaces from this point and after cause it might not find the end of EOM.
#leave it like this.
print <<EOM;
<html>
<head>
<title>$title</title>
</head>
<body>
<h1 align="center">$title</h1>
<p align=center>
$content
</p>
</body>
</html>

.
EOM

exit 0;
}

It could be more complicated (as to check for mispelled addresses, to look for dangerous code in the place of the texts, and others) but i think your brother doesn't care that much.
If though you like it more secure just tell me about it.

I hope this is what you want. Free to ask any questions if you like.

The error subroutine is taken from the email.cgi that is outthere as divt mentioned
sorry, don't save the file like this

my_sendmail.pl
-------------------------
#!/usr/bin/perl -w
...........................
..................


save it like this

my_sendmail.cgi
-------------------------
#!/usr/bin/perl -w
...............
........................

sorry about that, it was a typo. Instead of .cgi i wrote .pl
Avatar of rngrfan

ASKER

Thanks for responding since I havent heard from anyone in a while.

Well I tried your suggestions and dont think I have ever gotten this far so I know I am on the right track.

This is the error message I received:

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

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


Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

--------------------------------------------------------------------------------
Apache/1.3.33 Server at www.ankerpools.com Port 80""


I tried the suggestion the other way (<form action="/public_html/cgi-bin/my_sendmail.cgi" method="POST">) but I got page not found error.

There was one thing I am not sure I did right.  When you said

"save it like this

my_sendmail.cgi
-------------------------
#!/usr/bin/perl -w"

I copied your text to a notepad from

#!/usr/bin/perl -w
use strict;
use CGI;

all the way down to

</body>
</html>

.
EOM

exit 0;
}


Was that correct?  Also I saved it to the cgi-bin but dont understand the " #!/usr/bin/perl -w " part.



You did good to paste the code from
#!/usr/bin/perl -w
to
exit 0;
}

the first line is the shebang line, which tells where the executable perl is.
in most linux system's it is in /usr/bin folder, others have it in /usr/local/perl/bin, others somewhere else.
Sorry i didn't mention anything about it, i thought you knew some stuff about perl.
So you have to change it to whatever the path to perl executable is on your server.
The '-w' is just for enabling perl warnings (if something is not spelled corectly, or something obvious is mising, etc)

You have to know though that we haven't made any page to tell you "Ok mail sent", or whatever. It will show you a blank page when it is done.
To do that just put this line
print $query->redirect('http://www.ankerpools.com');       #(or inside the link just put the link to your 'Thank you, Ok: mail send' page that you will make!)
after the line
close MAIL;

this will also fix your problem. The problem occured because we didn't place header to the file, only to the error page.
so now with the redirection that we are making you don't need any so this one line will fix it.
Unless there is something in your server that will not let the script work properly.
Tell me if it works.
If it doesn't then go to '/whatever/the/path/to/logs/error_log' file and show me the errors you got.
Avatar of rngrfan

ASKER

I looked at Acenet cPanel and it says Path to PERL is /usr/bin/perl so I take it that your original is right.  I added the line in the my_sendmail.cgi but got the same error message as before.

http://www.ankerpools.com/my_sendmail.html changes pages to http://www.ankerpools.com/cgi-bin/my_sendmail.cgi

Error Logs

[2005-05-24 09:26:05]: error: file has no execute permission: (/home/danker/public_html/cgi-bin/my_sendmail.cgi)
[2005-05-24 09:24:43]: error: file has no execute permission: (/home/danker/public_html/cgi-bin/my_sendmail.cgi)
[Tue May 24 09:26:05 2005] [error] [client 69.40.38.26] File does not exist: /home/danker/public_html/500.shtml
[Tue May 24 09:26:05 2005] [error] [client 69.40.38.26] Premature end of script headers: /home/danker/public_html/cgi-bin/my_sendmail.cgi
[Tue May 24 09:24:43 2005] [error] [client 69.40.38.26] File does not exist: /home/danker/public_html/500.shtml
[Tue May 24 09:24:43 2005] [error] [client 69.40.38.26] Premature end of script headers: /home/danker/public_html/cgi-bin/my_sendmail.cgi
ASKER CERTIFIED SOLUTION
Avatar of perlpengo
perlpengo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> I copied your text to a notepad from
do you mean notpad.exe on M$ systems?
then you most likely produced the wrong format for a UNIX system, or did you convert it it to UNIX fomat on the server?

> [2005-05-24 09:26:05]: error: file has no execute permission: (/home/danker/public_html/cgi-bin/my_sendmail.cgi)
you need to do a
   chmod 550 my_sendmail.cgi

> [Tue May 24 09:26:05 2005] [error] [client 69.40.38.26] File does not exist: /home/danker/public_html/500.shtml
this is a configuration error of your server, not related to this cgi, you can ignore it for now

> [Tue May 24 09:26:05 2005] [error] [client 69.40.38.26] Premature end of script headers: /home/danker/public_html/cgi-bin/my_sendmail.cgi
see my comment above about notepad.exe
Avatar of rngrfan

ASKER

yes I used notepad on xp os.  is there a way to convert it to unix.  Only options i know i have to create are notepad, wordpad, word 2003, dreamweaver, adode.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial