1  #! /usr/local/bin/perl -w
  2  
  3  #############################################################################
  4  #  
  5  #   File:    mailform.pl
  6  #
  7  #   Input:   data from an HTML form called mailform.html
  8  #
  9  #   Output:  an HTML document, either confirming the message was sent
 10  #            or reporting an error
 11  #
 12  #   From the HTML mail form, this perl script receives an URL-encoded 
 13  #   message containing three pieces of data provided by the user: name, 
 14  #   e-mail, and a message.  The script processes this data and sends an 
 15  #   e-mail message to the recipient, after which the user is notified 
 16  #   whether or not the mailing was successful.
 17  #
 18  #############################################################################
 19  #  
 20  #   Initialization
 21  #
 22  #############################################################################
 23  
 24  $EOL = "\015\012";
 25  $MAILPROG = '/usr/lib/sendmail -t -n';
 26  $HOSTPATH = 'http://www.npac.syr.edu/projects/tutorials/CGI/examples/basic';
 27  
 28  #############################################################################
 29  #  
 30  #   Library Routines
 31  #
 32  #############################################################################
 33  
 34  require "ReadParse.pl" || die "error loading ReadParse.pl\n";
 35  
 36  #############################################################################
 37  #  
 38  #   Main Routines
 39  #
 40  #############################################################################
 41  
 42  ReadData() &&
 43  SendMessage();
 44  
 45  #############################################################################
 46  #  
 47  #   Subroutines
 48  #
 49  #############################################################################
 50  
 51  sub ReadData {
 52    my (%in);
 53    # ReadParse returns an associative array of key-value pairs:
 54    %in = ReadParse();
 55    $NAME = $in{'name'};
 56    $ADDRESS = $in{'address'};
 57    $MESSAGE = $in{'message'};
 58    return 1;
 59  }
 60  
 61  sub SendMessage {
 62    if ( open(MAIL, "|$MAILPROG") ) {
 63      print MAIL "To: \"$NAME\" \<$ADDRESS\>\n";
 64      print MAIL "From: \"$NAME\" \<$ADDRESS\>\n\n";
 65      print MAIL "$MESSAGE\n";
 66      close(MAIL);
 67      SendConfirm();
 68    } else {
 69      SendError();
 70    }
 71    return 1;
 72  }
 73  
 74  #############################################################################
 75  #  
 76  #   Print HTML pages
 77  #
 78  #############################################################################
 79  
 80  sub SendConfirm {
 81    print qq{Content-type: text/html$EOL$EOL
 82  <HTML>
 83  <HEAD>
 84  <TITLE>Mail Form Confirmation Notice</TITLE>
 85  </HEAD>
 86  <BODY BGCOLOR="#FFFFFF">
 87  
 88  Hi $NAME, <P>
 89  
 90  Thanks for sending an e-mail message to: <P>
 91  
 92  $NAME <BR>
 93  $ADDRESS
 94  
 95  <P><HR><P>
 96  
 97  Return to
 98  <A HREF="$HOSTPATH/mailform0.html">HTML mailform</A>
 99  
100  </BODY>
101  </HTML>
102  };
103  
104  }  # end SendConfirm
105  
106  sub SendError {
107    print qq{Content-type: text/html$EOL$EOL
108  <HTML>
109  <HEAD>
110  <TITLE>Mail Form Error Notice</TITLE>
111  </HEAD>
112  <BODY BGCOLOR="#FFFFFF">
113  
114  Hi $NAME, <P>
115  
116  Sorry, your message to <P>
117  
118  $NAME <BR>
119  $ADDRESS <P>
120  
121  did not go through!
122  
123  <P><HR><P>
124  
125  Return to
126  <A HREF="$HOSTPATH/mailform0.html">HTML mailform</A>
127  
128  </BODY>
129  </HTML>
130  };
131  
132  }  # end SendError
133