#!/usr/bin/perl
use CGI qw(:standard);
$search = new CGI;

## DO NOTE CHANGE ABOVE THIS LINE EXCEPT TO MAKE THE
## PERL PATH CORRECT.
##  -- do this to determine if the perl path is correct:
##    which perl


## This must be the correct path to sendmail
$SENDMAIL = "/usr/lib/sendmail";

## Feel free to edit these to suit your needs.
$MAIL_FROM      = "Automatic Emailer<automatic_email\@js-x.com>"; # note the @ sign is preceeded by a \
$MAIL_SUBJ      = "Form Submitted! Automatically E-Mail.";
$MAIL_TO_TITLE  = "You got a form submitted to you";
$AFTER_URL      = "/";
$ERROR_EMAIL    = "error@js-x.com"; # where to send emails if parameters are not defined


## There should be one line for every parameter you want to get from
##   the posted data.
##  - These are ccase sensitive - they need to identically match that defined
##     in the form:
##   Example: (Where ... denotes form code not shown here)
##      <form .... ><input type=text name="emailA">...</form>

$p_emailA  = $search->param('emailA');
$p_emailB  = $search->param('emailB');
$p_address = $search->param('address');
$p_phone   = $search->param('phone');

# this just fixes - so sendmail will not crash
$p_emailA=$ERROR_EMAIL if ! $p_emailA;
$p_emailB=$ERROR_EMAIL if ! $p_emailB;

### START OF PROGRAM ###

&print_header;        # first line of output to make an HTML mime type
&send_email;          # Send the email
&write_thanks;



##############################################################
sub write_thanks() {
print <<"EOT";
<HTML><HEAD><TITLE>Thanks!</TITLE>
<!--
  -- wait 3 seconds then goto $AFTER_URL
  -->
<META HTTP-EQUIV="Refresh" CONTENT="3; URL=$AFTER_URL">
</HEAD>
<BODY>
<center>
<H3>Thanks For your information!</H3>
<BR><a href="$AFTER_URL">Click here to continue</a>
</center>
</BODY>
</HTML>
EOT
}

sub print_header() {
  print "Content-type: text/html\n\n";  ## First line of any HTML created web page.
}

sub send_email() {
  # -oi don't terminate if a "." appears on a line by itself
  # -t read to see the From: To: Cc: Bcc:
  # -odq put it in the queue... don't do it right away...
  #open(SENDMAIL, "| $SENDMAIL -oi -t -odq") or die "Can't fork for sendmail: $!\n";
  open(SENDMAIL, "| $SENDMAIL -oi -t") or die "Can't fork for sendmail: $!\n";
  print SENDMAIL <<"MYEOM";
From: $MAIL_FROM
To: $p_emailA
Cc: $p_emailB
Subject: $MAIL_SUBJ

- Address: $p_address
- Phone: $p_phone
MYEOM
  close(SENDMAIL) or warn "sendmail didn't close nicely.";
}