perl cgi
Perl is a scripting language that enables you to integrate database processing, search facilites, real-time page generation or just simple email processing into your website.
Nearly every company that offers Unix/Linux hosting incudes perl as part of the plan.
Below you can see a simple perl script that processes email from a HTML <form>. For simplicity the example doesn’t contain any validation, parameter or error checking on the information passed to it.
Simple Perl Script:
#!/usr/bin/perl
use strict;
use CGI;
my $cgiobject = new CGI;
my $fname=$cgiobject->param("fname"); my $email_add=$cgiobject->param("email_add"); my $email_msg=$cgiobject->param("email_msg");
print "Content-type: text/html\n\n"; open (MAIL, "|/usr/lib/sendmail your.name\@yourdomain.com") || &errormess; print MAIL "From: $email_add\n"; print MAIL "Reply-to: $email_add\n"; print MAIL "Subject: Email From Your Website\n"; print MAIL "Name $fname\n"; print MAIL "email $email_add\n"; print MAIL "Msg $email_msg\n"; close(MAIL);
print <<"html"; <HTML> <HEAD> <TITLE> Contact Us </TITLE> </HEAD> <BODY > <H4>Thank you - your message has been sent. You will be contacted shortly. </BODY> </HTML> html
exit;
sub errormess { print "<BR>Problem sending mail Aborting\n"; exit; }
Brief Overview:
The email form takes in 3 parameters, Name, Email and Message:
These values are picked up by the Perl script via a cgi object and assigned to 3 variables:
my $fname=$cgiobject->param("fname"); my $email_add=$cgiobject->param("email_add"); my $email_msg=$cgiobject->param("email_msg");
Notice that the name in bold must be the same in both the html form and Perl script. You can see the HTML code that accompanies this script by clicking here.
The script then send the mesasge out: open (MAIL, "|/usr/lib/sendmail your.name\@yourdomain.com") || &errormess;
/usr/lib/sendmail is the path to your sendmail program (your web host can give you this information) and the email address used (your.name\@yourdomain.com in this example) MUST have a backslash before the @ sign as this is a special character in Perl.
Finally the script displays a mesasge back to the browser:
print <<"html_msg"; <HTML> <HEAD> <TITLE> Contact Us </TITLE> </HEAD> <BODY > <H4>Thank you - your message has been sent. You will be contacted shortly. </BODY> </HTML> html_msg
Please note the 'html_msg' tag on the last line must start in column one or the script will fail.
|