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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to CGI- An easy one 1

Status
Not open for further replies.
Jun 30, 2003
196
GB
Hi all i am very new to CGI and know a little perl.

I have a web page with a form that collects contacts name and address. Just the two fields. I have a button at the bottom submit. Can anyone tell me how to write a cgi script that when this submit button is pressed it sends the information to me via email.

 
I wrote you some simple code to do the job and added some comments to help you understand what is happening. Copy it, change settings for e-mail address, save in file "email.cgi", upload the file to your server (cgi-bin directory) in ASCII mod (not BINARY!) and CHMOD it to 755 (rwx-rx-rx).

Use such HTML code for the form:

<form action=&quot;cgi-bin/email.cgi&quot; method=&quot;POST&quot;>
Your name: <input type=&quot;text&quot; name=&quot;name&quot;><br>
Your e-mail: <input type=&quot;text&quot; name=&quot;email&quot;><br>
<input type=&quot;submit&quot; value=&quot;Submit this form&quot;>
</form>

Once you got it working you can change HTML code between qq~ and ~; so it will fit your site.

Hope this helps!

Perl code:

#!/usr/bin/perl
# Change the path above to the path to Perl on your server
# This one usually works

$your_email = 'you@email.com'; # Your e-mail address
$ subject = 'New contact'; # Subject of the e-mail sent to you
$mailprog = '/usr/sbin/sendmail -t'; # Path to mail software on server
# (ask your server administrator or read server documentation)
# This one usually works

# We use CGI.pm module to easily process the submitted data
use CGI;
$q=new CGI;

# This line of code will help us debugging the script
use CGI::Carp qw(fatalsToBrowser);

# Get the value of the &quot;name&quot; input field and store it in variable $name
$name=$q->param(&quot;name&quot;) or error(&quot;Please enter your name&quot;);

# Get the value of the &quot;email&quot; input field and store it in variable $email
$email=$q->param(&quot;email&quot;) or error(&quot;Please enter your e-mail address&quot;);

# Check if the e-mail is of type &quot;something@something.com&quot;
unless ($email =~ /([\w\-]+\@[\w\-]+\.[\w\-]+)/) {error(&quot;Please enter a valid e-mail address&quot;);}

# Open sendmail and write an e-mail
open(MAIL,&quot;|$mailprog&quot;) or error(&quot;Can't open e-mail program. Check your path!&quot;);
print MAIL &quot;To: $your_email\n&quot;;
print MAIL &quot;From: $name <$email>\n&quot;;
print MAIL &quot;Subject: $subject\n\n&quot;;
print MAIL qq~Hello

Someone has just submitted your form:

Name: $name
E-mail: $email

Bye!
~;
close(MAIL);

# Display the thank you page
print &quot;Content-type: text/html\n\n&quot;;
print qq~
<html>
<head>
<title>Thank you</title>
</head>
<body>
<p align=&quot;center&quot;><b>Thank you</b></p>
<p align=&quot;center&quot;>Your data was submitted!</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
~;
exit;

sub error {
# If something went wrong display an error page
($problem) = @_;
# &quot;$problem&quot; will be replaced with the error message
print &quot;Content-type: text/html\n\n&quot;;
print qq~
<html>
<head>
<title>Error</title>
</head>
<body>
<p align=&quot;center&quot;><b>Error</b></p>
<p align=&quot;center&quot;>$problem</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
~;
exit;
}
 
That is great thanks for that code i really have learnt somthings from that. Thanks for your time.

Just two querys can u explain what you meant by: &quot;Once you got it working you can change HTML code between qq~ and ~; so it will fit your site.&quot;

And Also if u have the time could u explain this section of the code to me:

# Open sendmail and write an e-mail
open(MAIL,&quot;|$mailprog&quot;) or error(&quot;Can't open e-mail program. Check your path!&quot;);
print MAIL &quot;To: $your_email\n&quot;;
print MAIL &quot;From: $name <$email>\n&quot;;
print MAIL &quot;Subject: $subject\n\n&quot;;
print MAIL qq~Hello

Does it open the customers email program and they write a message or is that what will appear in the email when i receive it?


 
You're welcome.

By changing the code between qq~ and ~; I meant this: you can edit the HTML code in this script so the pages displayed (&quot;thank you&quot; page and &quot;error&quot; page) will look same as your website. You can edit all the code between lines &quot;print qq~&quot; and &quot;~;&quot;. Example below:

print qq~
<html>
<head>
<title>Thank you</title>
</head>
<body>
<p align=&quot;center&quot;><b>Thank you</b></p>
<p align=&quot;center&quot;>Your data was submitted!</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
~;

you can for exaplme change it to
print qq~
<html>
<head>
<title>My own title</title>
</head>
<body>
<p align=&quot;center&quot;><b>Thank you</b></p>
<p align=&quot;center&quot;>I have received your e-mail</p>
<p> </p>
<p><a href=&quot; here to continue</a>
<p> </p>
<p><img src=&quot;path/to/some/image.gif&quot;></p>
<p> </p>
</body>
</html>
~;


The code you mentioned does this:
First line opens email program on your server (called &quot;sendmail&quot;), not your visitors' e-mail client
open(MAIL,&quot;|$mailprog&quot;) or error(&quot;Can't open e-mail program. Check your path!&quot;);

These lines print the &quot;To: &quot;, &quot;From: &quot; and &quot;Subject: &quot; headers of the e-mail which will be sent to you. Variables ($your_email, $name ,...) will be replaced by the assigned values.
print MAIL &quot;To: $your_email\n&quot;;
print MAIL &quot;From: $name <$email>\n&quot;;
print MAIL &quot;Subject: $subject\n\n&quot;;

This prints the body of the e-mail (what you will see in your e-mail client):
print MAIL qq~Hello

Someone has just submitted your form:

Name: $name
E-mail: $email

Bye!
~;

and finally script closes the e-mail program:
close(MAIL);

Again, this is done on the server, your visitors will not see this.
 
Thanks that is really helpfull

I can modify and build on this now, just the starting point i needed thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top