home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2004 February / INTERNET114.ISO / pc / projects / contact.cgi next >
Encoding:
Text File  |  2003-12-02  |  4.4 KB  |  155 lines

  1. #!/usr/bin/perl
  2. # Change above so it points to perl. Next line controls output buffering.
  3. $|=1;
  4.  
  5.  
  6. ########
  7. #
  8. # E-mail contact form script, (c) Alex Cruickshank 2003 (alex@itreviews.co.uk)
  9. #
  10. # This code is released under the GNU Public Licence (see www.gnu.org for details) and is supplied
  11. # as is with no warranty either expressed or implied. No liability will be accepted for loss or damage of any kind.
  12. #
  13. # Always back up your data before using new software.
  14. #
  15. # You are free to use this script to manage and serve data on your site.
  16. # Please notify the author of any such use, as well as bug reports, modifications, etc.
  17. #
  18. # chmod this file to 755. This script requires sendmail.
  19. #
  20. $mail_prog = "/usr/sbin/sendmail";
  21. # path to mail program, has to be sendmail or similar
  22. #
  23. $admin_email = "you\@yoursite.co.uk";
  24. # target e-mail address; you must place a '\' before the '@' symbol
  25. #
  26. $maintitle = "Contact us";
  27. # title for every page
  28. #
  29. $cgi_file = "/cgi-bin/contact.cgi";
  30. # relative URL of this script
  31. #
  32. $HeaderFile = "head.htm";
  33. # HTML header file path
  34. #
  35. $FooterFile = "foot.htm";
  36. # HTML footer file path
  37. #
  38. ########
  39.  
  40.  
  41. &parse;
  42. print "Content-type: text/html\n\n";
  43. if ($FORM{'message_in'}) {&send_message;}
  44. else {&main;}
  45. exit;
  46.  
  47.  
  48. sub parse { # handle form data
  49.  
  50.     read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  51.     @pairs = split(/&/, $buffer);
  52.     foreach $pair (@pairs) {
  53.         ($name, $value) = split(/=/, $pair);
  54.         $value =~ tr/+/ /;
  55.         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  56.         if ($FORM{$name}) { $fields{$name} = $fields{$name}.",".$value; }
  57.         else { $FORM{$name} = $value; }
  58.     }
  59. }
  60.  
  61.  
  62. sub header { # load and use a formatted page header
  63.  
  64.     open (HEADER,"$HeaderFile");
  65.     @header = <HEADER>;
  66.     close (HEADER);
  67.     foreach $headerline (@header) {
  68.         print "$headerline";
  69.     }
  70.     print "<H1>$maintitle</H1>\n";
  71. }
  72.  
  73.  
  74. sub footer { # load and use a formatted page footer
  75.  
  76.     open (FOOTER,"$FooterFile");
  77.     @footer = <FOOTER>;
  78.     close (FOOTER);
  79.     foreach $footerline (@footer) {
  80.         print "$footerline";
  81.     }
  82. }
  83.  
  84.  
  85. sub main { # show the form page
  86.     &header;
  87.  
  88.     print<<"OUTPUT";
  89.     <P>
  90.     Please use the form below to contact us.
  91.     </P>
  92.     <form action="$cgi_file" method="POST">
  93.     <input type="hidden" name="message_in" value="message_in">
  94.     <P>Your e-mail address:<BR><input type="text" size="50" name="your_address"></P>
  95.     <P>The message subject (e.g. 'feedback', 'advertising', etc.):<BR><input type="text" size="50" name="your_subject"></P>
  96.     <P>Enter the message:</P>
  97.     <textarea name="new_mail" rows="15" cols="50" WRAP="VIRTUAL" style="font-family:arial, helvetica, sans-serif; color:#000000; font-size:10pt;"></textarea>
  98.     <P><input type="submit" name="B1" value="Send"></P>
  99.     </form>
  100. OUTPUT
  101.  
  102.     &footer;
  103.     exit;
  104. }
  105.  
  106.  
  107. sub send_message { # do the e-mail sending
  108.     &header;
  109.  
  110.     $from_email = lc($FORM{'your_address'});
  111.     $your_subject = $FORM{'your_subject'};
  112.     @message_body = $FORM{'new_mail'};
  113.  
  114.     if ($from_email ne "") {
  115.         if ($your_subject ne "") {
  116.             if ($FORM{'new_mail'} ne "") {
  117.  
  118.                 open(MAIL,"|$mail_prog -t");
  119.                 print MAIL "To: $admin_email\n";
  120.                 print MAIL "From: $from_email\n";
  121.                 print MAIL "Return-Path: $from_email\n";
  122.                 print MAIL "Reply-To: $from_email\n";
  123.                 print MAIL "Subject: $your_subject\n\n";
  124.                 foreach $mline (@message_body) {
  125.                     print MAIL "$mline";
  126.                 }
  127.                 close (MAIL);
  128.  
  129.             }
  130.             else {
  131.                 print "<P>Your form was not complete (no message). Please go back and try again.</P>\n";
  132.                 &footer;
  133.                 exit;
  134.             }
  135.  
  136.         }
  137.         else {
  138.             print "<P>Your form was not complete (no subject). Please go back and try again.</P>\n";
  139.             &footer;
  140.             exit;
  141.         }
  142.  
  143.     }
  144.     else {
  145.         print "<P>Your form was not complete (no e-mail address). Please go back and try again.</P>\n";
  146.         &footer;
  147.         exit;
  148.     }
  149.  
  150.     print "<P>Thank you. Your message has been received, and will be read soon.</P><P>Now please click on one of the links on this page to visit another part of the site.</P>\n";
  151.     &footer;
  152.     exit;
  153. }
  154.  
  155.