Hi Curtis,

this script is far from nice, but it works.  A lot of it you could 
probably remove.  The recipientlist enabled me to hide the email 
addresses inside the cgi.

To improve it try looking at CPAN for mail handling routines.

Gary

__BEGIN__
#!/usr/bin/perl
#
# My varient of formmail.
# takes in form fields, validates them and if
# validated, forwards them to a designated email
# address.  If the form is invalid, the user is asked
# to retry.
#
 
# default values
$mailer="";                             # program to send mail
$recipient='[EMAIL PROTECTED]';     # default recipient
$sender='[EMAIL PROTECTED]';        # default from/sender
$send_name='WWW server';         # nice from/sender name
$subject="completed form details";      # default subject line
$required="";                           # list of mandatory fields
$nexturl="";                            # url to go to on completion
$failurl="";                            # url to go to on exception
$missing="";                            # mand. fields missing or empty
$referer="";                            # url of page containing form
%recipientlist=("feedback"    => '[EMAIL PROTECTED]',
                "webmaster"   => '[EMAIL PROTECTED]');
 
use CGI;
$query=new CGI;
 
if (! $query->param) {
  rep_error('noparam');
  exit;
}
 
if ( -x '/usr/bin/sendmail') {
  $mailer='/usr/bin/sendmail';
} elsif ( -x '/usr/sbin/sendmail') {
  $mailer='/usr/sbin/sendmail';
} elsif ( -x '/usr/lib/sendmail') {
  $mailer='/usr/lib/sendmail';
} elsif ( -x '/usr/local/bin/sendmail') {
  $mailer='/usr/local/bin/sendmail';
} elsif ( -x '/sbin/sendmail') {
  $mailer='/sbin/sendmail';
} elsif ( -x '/bin/sendmail') {
  $mailer='/bin/sendmail';
} else {
  rep_error('mailer');
  exit;
}
 
$required=$query->param('required');
$nexturl=$query->param('nexturl');
$failurl=$query->param('failurl');
$referer=$ENV{'HTTP_REFERER'};
#$recipient=$query->param('mailto') if ($query->param('mailto'));
if ($query->param('mailto')) {   
  $tmprecip=$query->param('mailto');
  $recipient=$recipientlist{$tmprecip} if ($recipientlist{$tmprecip});
}
$subject=$query->param('subject') if ($query->param('subject'));
if ($query->param('required')) {
  &checkfields;
  if ($missing) {
    rep_error('missing',$missing);
    exit;
  }
}
 
 
 
open(MAIL,"|$mailer -t") || rep_error('fatal',$!);
print MAIL <<EOF;
>From $sender
To: $recipient
From: $sender ($send_name)
Reply-To: webmaster\@revcom.org.uk (Webmaster)
Subject: $subject
 
This is an automatically generated email created because someone has
submitted a form on page $referer.
 
Please do not 'Reply-To' this form as it will not be seen by a human.
 
EOF
 
if ($query->param) {
  print MAIL "The contents of the completed form are:\n\n";
  @keywords=$query->param;
  foreach $keyword (@keywords) {
    $VALUE=$query->param($keyword);
    $VALUE='&lt;empty&gt;' if (!$VALUE);
    print MAIL "$keyword=$VALUE\n";
  }
} else {
  print MAIL "There were no fields submitted, a pretty useless form 
really\n";
}
 
print MAIL "\n\n.\n"; # nicely finish the mail message before closing 
pipe
 
close(MAIL);
 
# now give the user back a new page.
 
if ($nexturl) {
  print "Location: $nexturl\n\n";
} else {
  &thankyou_page;
}
 
#not really needed, but.. 
exit;
 
sub rep_error() {
  print STDERR "$_\n";
}
 
sub checkfields() {
  my $missing='';
  foreach (split ",",$required) {
    $missing.=$_ unless $query->param($_);
  }
}
 
sub thankyou_page() {
  print "Location: /subscribe.html\n\n";
}                                                                       

__END__
On Monday 30 April 2001  5:20 pm, Curtis Michelson wrote:
> Hi,
>
> I'm a Perl newbie for sure.  I recently subscribed to the list and
> this is my first post.
>
> My programming background is limited C and VisualBasic stuff.  My
> regular focus is database development and I work a lot with Filemaker
> Pro.  I also do some web devpt. as well.
>
> I'd like to try using Perl for a current client project.  Situation
> is this: Web users need to hit an HTML form page, fill out the form,
> press "submit" and then the data needs to be formatted into either a
> comma or tab delimited text file and then emailed to a specific fixed
> email address.   The backend Perl cgi code is what I need help with.
>
> Would anyone have suggestions on the best way for me to go about
> doing this with Perl?  Is there some publicly available base code I
> could start from and then modify or tweak to fit my needs?  Since I'm
> on a short timeline for the project, I'm open to  hiring a Perl
> consultant to help me wrap this up quickly.
>
> Any thoughts?
>
> Curtis Michelson
> Small Company
> Orlando, FL
> www.smallco.net

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 
    

Reply via email to