#!/usr/bin/perl -w
#--------#---------#---------#---------#---------#---------#---------#---------#
# TITLE
#    submit an HTML form, post an e-mail, display the result
# AUTHOR
#    larg 23/03/00
# DESCRIPTION
#    A cgi script which e-mails a text page stored in a template file.
#    Parameters are passed to this script via HTML form submission.
#    They are checked and are stored in a text file.
#    The message body is read in and parsed. It can contain variables like this
#       [form_field]
#    Variables are replaced with field values passed in from the HTML form.
#    A message body may look like this, with headers, blank line and a body:
#       To: [recipient]
#       Subject: Feedback from the intranet
#
#       Message from this person: [name]
#       ...
#
#    The message is sent using sendmail.
#    If not successful, the error is displayed in an HTML page.
#    if successful, a success page is displayed.
#    These HTML pages are parsed in the same way as the e-mail body,
#    so feel free to add variables.
#
#    The "from" addresses on the envelope and the message are forced to
#    "mailer@isp01.co.uk".
#
# preparation
#---------#--------
# Edit the values between the "### CUSTOMISE HERE ###" lines.
# ~:-) Create the directory and file named by $ha{file_log}
# ~:-) change the permissions: chmod 755 dir  and chmod 622 file_log
# ~:-) if you are not returning control to a flash movie,
#    create "success" and "info_warning" pages and note their names AND paths
#    in $success_file and $info_warning_file
# ~:-) Make sure the path names are relative to this script.
#
# optional parameters
#---------#--------
# debug=1
#     display a test page
# page=flash
#     print a status message rather than
#     a success or fail page, for a Macromedia Flash movie
# page=redirect
#     redirect to the URL of the success and fail pages
#     (no debugging information is available with this option)
# page=html
#     print a success or fail page (this is the default)
#
# testing
#---------#--------
# turn on debugging.
# add a hidden field on your test page with the debug parameter
# send mail to your acount on the webserver eg.
#    larg@localhost
# start a terminal session on the webserver
# check the mailbox using the command
#    mail
# If you are logged in as root, check another mailbox with the
#    mail -f /var/mail/larg
# comand.
#
# files
#---------#--------
# 1 e-mail-form-submit.html
# 2 e-mail-form.pl
# 3 e-mail-form-message.txt
# 4 e-mail-form-lib.pl
# 5 e-mail-form-success.html
# 6 e-mail-form-fail.html
#
#--------#---------#---------#---------#---------#---------#---------#---------#
    require 'e-mail-form-lib.pl';    # subroutines live in this file
    my %ha                  = &init_email_hash;

### CUSTOMISE HERE ### CUSTOMISE HERE ### CUSTOMISE HERE ### CUSTOMISE HERE ###
#
# edit these values to fit your web site.
   #
   # Files
   # these paths are relative to this executable, in the cgi bin.
   $ha{file_log}           = '../private/email-log.txt';
   $ha{file_fail}          = '../doc/e-mail/e-mail-form-fail.htm';
   $ha{file_debug}         = '../private/debug-log.txt';
   #
   # Headers
   # Headers such as "subject" and "to" message address are set in the
   # message file. They can also be forced here, if you want every message to
   # have the same headers.
   # force "from" address of the message, the one that gets displayed by the
   # mail client.
   # This is NOT the envelope "from" address. Tihs is set in the form using
   # the "sender" variable.
   #
   $ha{msg_body}           = "From: mailer\@domain.co.uk\n";
#
### CUSTOMISE HERE ### CUSTOMISE HERE ### CUSTOMISE HERE ### CUSTOMISE HERE ###
   #
   # required fields
   # This script requires several other values in order to work correctly.
   # You must create these fields in your HTML form.
   # These are read by the "read_params" sub and checked by the "check_params"
   # sub.
   # recipient
   # The "to" address used for the envelope and message.
   # sender
   # The "from" address used for the envelope. The message address is set above.
   # success
   # The HTML success page is different for every form, so we set this value
   # in the HTML form rather than here. See the "success" field.
   # You can force this by adding this line above and removing "success" below.
   # $ha{file_success}       = '../doc/e-mail/e-mail-form-success.htm';
   # template
   # The text file used as the message body.
   # You can force this by adding this line above and removing "success" below.
   # $ha{file_msg_body}      = '../doc/e-mail/e-mail-form.txt';
   #
   $ha{params_required}    = [ 'recipient', 'sender', 'success', 'template' ];
   #


# save time with reusable components
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    $ha{cgi_ptr}            = new CGI;
    use strict;
#    use MIME::Lite;

# work it, baby
   # read the parameters that were passed to this script
   # and store them in a hash of hashes
   # check the required params are present
   # store the details
   # send the email
   # parse the body to replace variables

   if (
               &read_params( \%ha )    &&
               &check_params( \%ha )   &&
               &write_params( \%ha )   &&
               &read_msg_file( \%ha )  &&
               &parse_msg_body( \%ha ) &&
               &send_text( \%ha )
   ) {
      $ha{success} = 1;
   } else {
      $ha{success} = 0;
   }

# display a page
   &display_result( \%ha );

# clean up after yourself. Destroy, destroy, destroy.
   undef %ha;

