HTML/CGI Talk
Email Script

A Demonstration

Type in your street address:

Type in your phone number:

Which toppings would you like?

  1. Pepperoni.
  2. Sausage.
  3. Anchovies.
How would you like to pay? Choose any one of the following:

  1. Cash.
  2. Check.
  3. Credit card:
    • Mastercard.
    • Visa.
    • American Express.
Would you like the driver to call before leaving the store?

Yes.
No.
Did you Like My Form !!! What is your favorite operating system! To order your pizza, press this button: .

EMAIL PERL Code

#!/usr/local/bin/perl
#==========================================================================
# 
# Created:	28/7/95
# By:		Scott Penrose (scottp@pas.com.au)
# Purpose:	standard EMAIL cgi script
#		(used for html/cgi talk for LUV 1/8.95)
#
# Future:	
#
# Usage:	Install into STANDARD cgi directory
#		Call from HTML Form
#		Input	- to = Your email address
#			- subject = What is the subject
#			- ALL other  = Your other fields to include in email
#				Should include a number for order
#				1 from
#				2 Why is it so
#				3 ...
#
#				Also returns date/time and other info.
#
#==========================================================================

# Set the DATE (silly method, but it works)
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon++;
$SS_DATE = $year*10000 + $mon*100 + $mday;
$SS_TIME = "$hour.$min";


#########################
# GET Command ENVIRONMENT
#########################
# These are the standard variables passed to you with CGI
# -------------------------------------------------------
# AUTH_TYPE - Basic
#     Level ?
# GATEWAY_INTERFACE - CGI/1.1
#     CGI Version
# HTTP_ACCEPT - */*, image/gif, image/x-xbitmap, image/jpeg
#     Accepted types ?
# HTTP_REFERER - http://www.pas.com.au.:8001/cgi-bin
#     ?
# HTTP_USER_AGENT - Mozilla/1.1N (X11; I; Linux 1.1.90 i486)
#     Application calling script, my copy of Netscape on Linux
# QUERY_STRING - 
#     The string USUALLY used in a CGI Script
# REFERER_URL - http://www.pas.com.au.:8001/cgi-bin
#     The document/directory calling this script. ie: you can make sure it
#     is yours.
# REMOTE_ADDR - 203.8.9.18
#     Remote callers IP Number, a simple method of security if necessary
# REMOTE_HOST - scott.meriden.pas.com.au
#     And of course the remote callers address
# REMOTE_IDENT - 
#     Remote ident ?
# REMOTE_USER - scottp
#     Remote USER Name, if it is a protected directory
# REQUEST_METHOD - GET
#     Method called, get/put
# SCRIPT_NAME - /cgi-bin/showenv.cgi
#     Name of this script (and location)
# SERVER_NAME - jethro.meriden.pas.com.au
#     Full name of server running httpd
# SERVER_PORT - 8001
#     Port Number Used
# SERVER_PROTOCOL - HTTP/1.0
#     Server Protocol
# SERVER_SOFTWARE - CERN/3.0
#     Server Software
# ------------------------------------------------------------------------
# A BIG LIST, but very useful !
# 
# Out QUERY_STRING - We shall seperate to our valid variables 
$QUERY_STRING = $ENV{QUERY_STRING};
# Now split into associative array
# Split Query Environment String into a list, seperated by &
# eg: String=Something&Where=Somewhere
@QUERY_LIST = split( /&/, $QUERY_STRING);
# Split each list item into PARAM and VALUE into an array
foreach $item (@QUERY_LIST) {
    ($param, $value) = split( /=/, $item);
    # UNESCAPE
    # CGI makes all weird characters escape sequences. 
    # (I copied this line from the wwwlib for perl
    $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C",hex($1))/ge;
    $value =~ s/\+/ /g;
    $param =~ s/\+/ /g;
    $QUERY_ARRAY{$param} = $value;
}
# Get other entries for LOG File
$REMOTE_HOST = $ENV{REMOTE_HOST};
$REMOTE_USER = $ENV{REMOTE_USER};
$SERVER_NAME = $ENV{SERVER_NAME};
$REMOTE_URL = $ENV{REMOTE_URL};
$HTTP_USER_AGENT = $ENV{HTTP_USER_AGENT};


#######################
# HTML - Display HELLO!
#######################
# Don't forget you need to specify the TYPE of document.
# You do not need this with HTML files because HTTPD uses the extension of
# these files to specifiy the type, eg: HTML, GIF, JPEG etc.
print "Content-type: text/html\n\n";
print "\n\n";
# Standard HTML
# You need a \ in front of @, ", and some other keys
# Don't forget \n to make your source HTML file look clean
print "<HTML>\n";
print "<HEAD>\n<TITLE>EMAIL</TITLE>\n</HEAD>\n";
print "<BODY>\n";
print "<H1>EMAIL</H1>\n";
#
# This of course should really check and SHOULD display the users message
# and who it is to etc. You could extend this with ARE YOU SURE...
#
print "Your document has been succesfully sent!\n";
print "<H2>Contents...</H2>\n";
print "<P><P>\n<PRE>\n";


####################
# Create the message
####################
# This script does not set return address or from correctly
# It is basically and information gatherer. This type of information would
# normally be sent to a log file, not an email address.
open (EMAIL, "|mail $QUERY_ARRAY{to} -s \"$QUERY_ARRAY{subject}\"");
print "To           $QUERY_ARRAY{to}\n";
print "Subject      $QUERY_ARRAY{subject}\n\n";

# Print some standard information
print EMAIL "EMAIL.CGI - Automatic response from HTML Document\n\n";
print "EMAIL.CGI - Automatic response from HTML Document\n\n";
print EMAIL "Date         $SS_DATE\n";
print "Date         $SS_DATE\n";
print EMAIL "Time         $SS_TIME\n";
print "Time         $SS_TIME\n";
print EMAIL "Remote Host  $REMOTE_HOST\n";
print "Remote Host  $REMOTE_HOST\n";
print EMAIL "Remote User  $REMOTE_USER\n";
print "Remote User  $REMOTE_USER\n";
print EMAIL "Server Name  $SERVER_NAME\n";
print "Server Name  $SERVER_NAME\n";
print EMAIL "Remote URL   $REMOTE_URL\n";
print "Remote URL   $REMOTE_URL\n";
print EMAIL "Viewer       $HTTP_USER_AGENT\n\n";
print "Viewer       $HTTP_USER_AGENT\n\n";

# SORT and output user information
foreach $key (sort (keys %QUERY_ARRAY)) {
	# IF it is not to/subject
	if ($key ne "to" && $key ne "subject") {
		print EMAIL "$key:  $QUERY_ARRAY{$key}\n";
		print "$key:  $QUERY_ARRAY{$key}\n";
	}
}

# END of Email document
close (EMAIL);

################
# HTML - Goodbye
################
print "</PRE>\n";
print "<HR>\n<P>\nPlease leave any messages/problems to <A href=\"mailto:sysadmin\@pas.com.au\">sysadmin\@pas.com.au</A>\n";
# DON'T forget to END Body and HTML
print "</BODY>\n";
print "</HTML>\n";

Contents | Previous | Next | Search

Modified: 1/8/95
Created: 28/7/95
By: scottp@pas.com.au