How to use forms for email

      Forms can be used to add interaction to a web site. A very common use of forms is to generate email. In this how-to, we will create a perl script that will take the input from a form, format it nicely, send it to a specified email address, and return a thank-you page.

      First lets create a simple form that will have a field for a person's Name, Address, City, State, Zip, Phone, and email address.

      
      <FORM METHOD=POST ACTION="/cgi-bin/email.pl">
      <INPUT NAME="name">Name<BR>
      <INPUT NAME="address">Address<BR>
      <INPUT NAME="city">City<BR>
      <INPUT NAME="state">State<BR>
      <INPUT NAME="zip">Zip<BR>
      <INPUT NAME="phone">Phone<BR>
      <INPUT NAME="email">Email<BR>
      <BR>
      <INPUT TYPE="SUBMIT" VALUE="Submit Form Now">
      <INPUT TYPE="RESET">
      </FORM>
      
      

      Note several things about this form:

      1. The cgi script that will handle this form will be named email.pl
      2. The names of the variables for our script will be in the quotes from the NAME attribute. e.g. $name, $address, etc.

      Now lets take a look at a simple script to handle this form. We will be using the cgi-lib.pl library to take care of reading all the data from the form. In order to access our form's data, we will use this syntax:

      $in{"variable"}

      In order to send an email, we will open a special file called a pipe. We will use this pipe to print our email message to. Anything printed to the pipe will be sent to the mail program.


      #!/usr/bin/perl
      
      #Use our form library
      require "cgi-lib.pl";
      
      #Get the data from the form
      &ReadParse;
      
      #Now lets open a pipe to the mail program and print our email message
      open(MAIL,"|mail username\@host.com");		#put email address here
      
      #now we put MAIL after print to print to the pipe
      print MAIL <<"EOT";
      Hello,
      
      This is a simple email response from a form.
      
      Here is the information that was sent:
      
      Name:		$in{"name"}
      Address:	$in{"address"}
      City:		$in{"city"}
      State:		$in{"state"}
      Zip:		$in{"zip"}
      Phone:		$in{"phone"}
      Email:		$in{"email"}
      
      Thank you
      EOT
      
      close(MAIL);
      
      print <<"EOT";
      Content-type: text/html
      
      <HTML><BODY>
      <H1>Thank you $in{"name"}.</H1>
      <H2>The form's information was successfully sent via email</H2>
      </BODY></HTML>
      EOT
      


      Note several conventions in this perl script:
      1. Note the \ before the @ in the email address. This is needed because the @ symbol has a special meaning in Perl. The \ is used to take away this meaning, and is often used with other special symbols such as quotes or dollar signs.
      2. We provided an argument to print to tell it to print to the newly created pipe MAIL, instead of to STDOUT.
      3. The <<"EOT"; syntax is a nice way of printing formatted text that spans multiple lines. It says "print everything until you get to EOT. Notice that EOT is on a line by itself with no preceding spaces. Any variables will be evaluated and all spaces and line breaks will be printed verbatim.
      4. The last section prints to STDOUT. Here, the first line tells the web server that this is an HTML page. You don't need the \n\n here because the newlines are printed literally from the formatting.


      Contents