1  #! /usr/npac/bin/perl -w
  2  
  3  #############################################################################
  4  #  
  5  #   File:    mailform.pl
  6  #
  7  #   Input:   data from an HTML form called mailform0.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-cgi';
 27  
 28  #############################################################################
 29  #  
 30  #   Library Routines
 31  #
 32  #############################################################################
 33  
 34  use CGI qw(:standard);
 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    $NAME = param('name');
 53    $ADDRESS = param('address');
 54    $MESSAGE = param('message');
 55    return 1;
 56  }
 57  
 58  sub SendMessage {
 59    if ( open(MAIL, "|$MAILPROG") ) {
 60      print MAIL "To: \"$NAME\" \<$ADDRESS\>\n";
 61      print MAIL "From: \"$NAME\" \<$ADDRESS\>\n\n";
 62      print MAIL "$MESSAGE\n";
 63      close(MAIL);
 64      SendConfirm();
 65    } else {
 66      SendError();
 67    }
 68    return 1;
 69  }
 70  
 71  #############################################################################
 72  #  
 73  #   Print HTML pages
 74  #
 75  #############################################################################
 76  
 77  sub SendConfirm {
 78    print qq{Content-type: text/html$EOL$EOL
 79  <HTML>
 80  <HEAD>
 81  <TITLE>Mail Form Confirmation Notice</TITLE>
 82  </HEAD>
 83  <BODY BGCOLOR="#FFFFFF">
 84  
 85  Hi $NAME, <P>
 86  
 87  Thanks for sending an e-mail message to: <P>
 88  
 89  $NAME <BR>
 90  $ADDRESS
 91  
 92  <P><HR><P>
 93  
 94  Return to
 95  <A HREF="$HOSTPATH/mailform0.html"
 96  >HTML mailform</A>
 97  
 98  </BODY>
 99  </HTML>
100  };
101  
102  }  # end SendConfirm
103  
104  sub SendError {
105    print qq{Content-type: text/html$EOL$EOL
106  <HTML>
107  <HEAD>
108  <TITLE>Mail Form Error Notice</TITLE>
109  </HEAD>
110  <BODY BGCOLOR="#FFFFFF">
111  
112  Hi $NAME, <P>
113  
114  Sorry, your message to <P>
115  
116  $NAME <BR>
117  $ADDRESS <P>
118  
119  did not go through!
120  
121  <P><HR><P>
122  
123  Return to
124  <A HREF="$HOSTPATH/mailform0.html"
125  >HTML mailform</A>
126  
127  </BODY>
128  </HTML>
129  };
130  
131  }  # end SendError
132