shephesa2000 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w # The first line of the file is unnecessary for the Novonyx servers, b +ut is necessary for other # web servers, leave it in to keep the code portable. ################################ # Necessary Fields in HTML Form: # SendersDepartment = specifies the remote users department # sendername = specifies the remote users real identity # subject = the subject line that will appear in your email # link1 = the url for the link, relative to your web entry point # (ie link1="/index.html") # link2 = the text that will be printed for the link back on the retur +n html page # (ie link2="Back to Wild Things Photography Home") # The first thing that must be printed out to a web page is the follow +ing line. It doesn't # actually print, but it tells the web server what format you will b +e sending it from this # program print "Content-type: text/html\n\n"; # Sendmail is saved as a separate program, so in order to use it, it m +ust be "required" require("sendmail.pl"); # If the requestMethod is "POST", the data will be read from STDIN, # if "GET" it will be read from the URL line $requestMethod = $ENV{"REQUEST_METHOD"}; $input = ""; if($requestMethod eq "POST") { read(STDIN, $input, $ENV{"CONTENT_LENGTH"}); } else { $input = $ENV{'QUERY_STRING'}; } # Strip off white space and any html tags that come in with the form d +ata, and # create an array of the name/value pairs %in = UnencodeFormData(); sendmail(%in); # Print Return HTML # Any reference to graphics of other URLs need the path relative to yo +ur web entry point # If your entry point is sys:/novonyx/suitespot/docs and your graphi +cs are in # ~docs/web/graphics # the url to use is /web/codebreak/graphics/image.jpg print "<html><head><title>-----LINKS eMail-----</title></head>\n"; print "<body><center><table><tr><td colspan=2>\n"; print "<center><img src=\"../images/LINKSemail.gif\">\n"; print "<p><hr size=2><br><font face='arial'>Thank you for your email\. +<br>\n"; print "To continue from where you left off simply click the button bel +ow.</font></center><br>\n"; print "<hr size=2>\n"; print "</td></tr><tr align=center>\n"; print "<td colspan=2><input type=\"button\" value=\"Continue\" onClick +=\"history.go(-2)\"></td></tr>\n"; print "<tr align=center><td colspan=2><br><br><font face=arial size=2> +<a href=$ITEM{link1}>$ITEM{link2}</a></td></tr>\n"; print "</table>\n"; print "</center></body></html>"; sub UnencodeFormData { # separate fields @fields = split(/&/, $input); # separate field names from field values foreach $field (@fields) { ($name, $value) = split(/=/, $field); # Convert + signs to spaces, $value =~ tr/+/ /; # convert %HEX values to ASCII characters $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # remove any embedded comments in case of server-side includes $value =~ s/<!--(.|\n)*-->//g; # remove any HTML tags $value =~ s/<([^>]|\n)*>//g; $ITEM{$name} = $value; } # Replace spaces with underscores in the category name # $ITEM{category} = FixCategoryName($UNDERSCORE, $ITEM{category}); return(%ITEM); } <p> The following code is from the sendmail.pl file <p> <code> #!/usr/bin/perl -w use Socket; use strict; sub sendmail() { my (%body) = @_; my $line = ""; # Substitute your mail server here. my($mailServer) = 'mailhub.health.nb.ca'; #These next fields come in from the form, your form should have at l +east the following # fields: To, SendersDepartment, Sender, subject my($mailTo) = "$body{To}\n\n\n\n"; my($mailFrom) = "$body{SendersDepartment}"; my($realName) = "$body{Sender}"; my($subject) = "$body{subject}"; # These constants shouldn't need changing. my($packFormat) = 'S n a4 x8'; #Internet address format (AF_INET:por +tNumber:serverAddress:eightNulls) my($proto) = getprotobyname("tcp") || 6; my($port) = getservbyname("SMTP", "tcp") || 25; my($name,$aliases,$addrtype,$len,@addrs) = gethostbyname($mailServer +); my($serverAddr) = $addrs[0]; # If this script dies at this line, it is because you don't have an +entry in the SYS:\etc\hosts # file for your mail server. Manually add an entry for the mail se +rver if not already there. if (! defined($len)) { die('gethostbyname failed.'); } socket(SMTP, AF_INET(), SOCK_STREAM(), $proto) or die("socket: $!"); connect(SMTP, pack($packFormat, AF_INET(), $port, $serverAddr)) or die("connect: $!"); select(SMTP); $| = 1; select(STDOUT); # use unbuffered i/o. { my($inpBuf) = ''; recv(SMTP, $inpBuf, 200, 0); } sendSMTP(1, "HELO\n"); sendSMTP(1, "MAIL FROM: <$mailFrom>\n"); sendSMTP(1, "RCPT TO: <$mailTo>\n"); sendSMTP(1, "VRFY\n"); sendSMTP(1, "DATA\n"); sendSMTPnoresponse(1, "From: $realName <$mailFrom>\n"); sendSMTPnoresponse(1, "Subject: $subject\n"); sendSMTPnoresponse(1, "\n"); # Print each of the name/value pairs coming from the form IF they ha +ve been filled in for $line (keys %body) { if($line !~ /link/ && $line !~ /subject/) # We don't need to prin +t out the hidden form fields { sendSMTPnoresponse(1, "$line = $body{$line}\n") if($body{$lin +e}); } } # Since we have put the name/value pairs into a hash which stores ra +ndomly, using the above # for loop will print the name/value pairs randomly in your email +message. # To print them in a specific sequence, you can explicitly print e +ach one with the following # format (INSTEAD OF using the for loop). # sendSMTPnoresponse(1, "Sender\'s Name = $body{Sender}\n") if($bo +dy{Sender}); # sendSMTPnoresponse(1, "Sender\'s Department = $body{SendersDepar +tment}\n") if($body{SendersDelpartment}); # sendSMTPnoresponse(1, "Message = $body{Message}\n") if($body{Mes +sage}); sendSMTPnoresponse(1, "\n"); sendSMTP(1, ".\n"); sendSMTP(1, "QUIT\n"); close(SMTP); } sub closeSocket { # close smtp socket on error close(SMTP); die("SMTP socket closed due to SIGINT\n"); } sub send_to_smtp { my($debug) = shift; my($response) = shift; my($buffer) = @_; # Uncomment the following line for debugging only # print STDERR ("> $buffer") if $debug; send(SMTP, $buffer, 0); if ($response) { recv(SMTP, $buffer, 200, 0); # Uncomment the following line for debugging only # print STDERR ("< $buffer") if $debug; return( (split(/ /, $buffer))[0] ); } } sub sendSMTP { my ($debug) = shift; send_to_smtp($debug, 1, @_); } sub sendSMTPnoresponse { my ($debug) = shift; send_to_smtp($debug, 0, @_); } 1; #return true
Edit kudra, 2002-06-06 Added readmore
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl and Email Form problem
by cjf (Parson) on Jun 05, 2002 at 14:50 UTC |