While the solution given works, it's a bit of a hack. Below is a simple form to email script with the destination address embedded as a variable - it hasn't got all the bells and whistles of the standard formmail script, and should therefore be much easier to configure and 'drop in' to your site.
#!/usr/bin/perl
### Configuration
$destination="me\@mydomain.com"; # destination email address
$from_address="form\@mydomain.com"; # address the email will appear to come from
$subject="Form submittal"; # subject line for the email
$sendmail="/usr/lib/sendmail"; # location of mailer program
### End configuration
print "content-type: text/html\n\n";
# set up the CGI.pm stuff
use CGI;
$query=CGI::new();
# get all form field names
@params=$query->param();
# open a pipe to mailer program
open (FP, "¦ $sendmail -t -oi -oeq") or die;
# print out email headers
print FP "To: $destination\n";
print FP "From: $from_address\n";
print FP "Subject: $subject\n\n";
# iterate through the @fields array, printing each
# field name and it's value
foreach $i (@params) {
$value=$query->param($i);
print FP "$i : $value\n";
}
# print a '.' on its own line, to tell sendmail we've
# finished constructing the email
print FP "\n.\n";
# close the pipe to mailer program
close (FP);
# print out a 'thank you' message
print qq^
<!-- insert your own 'thank you' html here -->
<html><head></head><body>
<h2>Thank you, your message has been sent</h2>
</body></html>
^;
exit;
Any questions / suggestions / improvements are most welcome!
See "getmail.htm [searchengineworld.com]" over there under "contacts" for the html form I use. The following script handles processing of that email sent.
#!/usr/bin/perl
$username = 'you@yourdomain.com';# where the email is sent too
$mailprog = "/usr/lib/sendmail -t -n";#system mailer binary
#$footerfile = ""; #delete leading # and define path to html file to enable this feature - a footer on the followup thank you page
$refer = " www.yoursite.com";
#next one is for storing the email sent as a backup feature incase of email send failure.
$mailfile = "/fullpath to a backup file/inbox.dbm";
$follow_up = <<"WEB_PAGE";
This text is generated after a user successfully sends you email.
This is usually a thank you note.
WEB_PAGE
#================================================= end of configuration
$mail_error =1;
$file_error =0;
&get_time;# calc time
&find_domain;# scarf out user domain
&parse_form;# get query string
unless ($FORM{'reply_to'} =~ /^[\w.+-]+\@[\w.+-]+$/) {
print 'Address not in form foo@nowhere.com';
exit;
}
if (open(MAIL, "¦$mailprog $username")) {
print MAIL "To: $username\n";
print MAIL "Reply-to: $FORM{'reply_to'}\n";
print MAIL "From: $FORM{'reply_to'}\n";
print MAIL "Date: $tdate\n";
print MAIL "Subject: $FORM{'subj'}\n\n";
print MAIL "Mailed from: $refer\n";
print MAIL "Host: $hostname\n";
print MAIL "Date $tdate\n";
print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n\n";
print MAIL "Site Reference: $refer\n\n";
print MAIL "$FORM{'text'}\n";
close(MAIL);
$mail_error =0;
}
if ($mailfile) {
&open_file("FILE1",">>",$mailfile);
if ($mail_error) {
print FILE1 "there was a mail error.\n\n";
}
print FILE1 "To: $username\n";
print FILE1 "From: $FORM{'reply_to'}\n";
print FILE1 "Date: $tdate\n";
print FILE1 "Subject: $FORM{'subj'}\n\n";
print FILE1 "Mailed from: $refer\n";
print FILE1 "Remote host: $ENV{'REMOTE_HOST'}\n\n";
print FILE1 "Host: $hostname\n";
print MAIL "Site Reference: Search Engine World\n\n";
print FILE1 "$FORM{'text'}\n";
print FILE1 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n";
if ($file_error) {
print FILE1 "ACK! File Error!\n";
print FILE1 "Bummer, the server or script encountered an error while trying to ";
print FILE1 "access $file_error - Either the directory or ";
print FILE1 "file doesn't exist, or its permissions are set ";
print FILE1 "incorrectly.";
}
close(FILE1);
}
print "Content-type: text/html\n\n";
print <<"WEB_PAGE";
<html><head>
<title>Thank You</title>
</head>
<body>
<blockquote>
The following email has been sent:<p>
<blockquote>
<b>From</b>: $FORM{'reply_to'} (via $refer)<br>
<b>Date</b>: $tdate<br>
<b>Subject</b>: $FORM{'subj'}<br>
<b>Remote host</b>: $hostname<br>
<b>Site Reference</b>: $refer
<p>
$FORM{'text'}
<p>
</blockquote>
WEB_PAGE
print "$follow_up";
if ($footerfile) {
&open_file("FOOTER","",$footerfile);
@footer = <FOOTER>;
close (FOOTER);
foreach $line (@footer) {
print "$line";
}
print "<!-- (C) Copy & Copyright 1996-2001 PHD Software Systems/Brett J. Tabke. [searchengineworld.com...] -->\n\n";
print "<!-- Page Generated: $tdate CT -->\n";
print " <!-- $t2 -->\n";
}
print "</body></html>";
exit;
# R E A D & W R I T E F I L E S #
sub open_file {
local ($filevar, $filemode, $filename) = @_;
open ($filevar,$filemode . $filename) ¦¦ &error_file($filename);
}
sub read_file {
local ($filevar) = @_;
<$filevar>;
}
sub write_file {
local ($filevar, $line) = @_;
print $filevar ($line);
}
sub error_file {
$file_error = $_;
print "<P ALIGN=CENTER><BIG><BIG><STRONG>";
print "File Error!</STRONG></BIG></BIG>\n";
print "<P>Bummer, the server or script encountered an error while trying to ";
print "access <STRONG>$_[0]</STRONG>! Either the directory or ";
print "file doesn't exist, or its permissions are set ";
print "incorrectly. ACK! </P></center><br>";
}
sub get_time {
@txt_months = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
@txt_days = (Sun,Mon,Tue,Wen,Thr,Fri,Sat);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year =$year+1900;
if ($mday < 10) { $mday = "0$mday"; }
if ($hour < 10) { $hour = "0$hour"; }
if ($min < 10) { $min = "0$min"; }
if ($sec < 10) { $sec = "0$sec"; }
$word_day = $txt_days[$wday];
$word_month = $txt_months[$mon];
$tdate = "$word_day\, $mday $word_month $year $hour\:$min";
}
sub find_domain {
$number = $ENV{REMOTE_ADDR};
($a,$b,$c,$d)=split(/\./,$number);
$ipadr=pack("C4",$a,$b,$c,$d);
($name,$aliases,$addrtype,$length,@addrs)=(gethostbyaddr("$ipadr", 2));
$hostname = $name;
}
sub parse_form {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
}
Dim cdonts
Set cdonts = Server.CreateObject("CDONTS.NewMail")
Call cdonts.Send("from@domain.com","to@domain.com", "this is a subject", "this is a body", 1)
Set cdonts = Nothing
It is very easy to create a form around this code. Since the "to@domain.com" would only appear server-side, it is protected from viewing by the surfer.
<?
mail("to@domain.com", "Subject","message body", "extra headers");
?>
where 'extra headers' is a list of headers seperated by \r\n eg "From: from@domain.com\r\nCc: foo@bar.com"
What I'm having trouble with, though, is finding an equivalent of the Perl @fields=$query->param(); - ie getting a list of all form fields submitted rather than having to know the field names in advance...