cgi-dev

Repository that serves as my CGI "scratchpad" to try things out.
git clone git://git.samirparikh.com/cgi-dev
Log | Files | Refs | README

feedback_sendmail.cgi (3090B) - raw


      1 #!/usr/bin/perl
      2 
      3 use strict;
      4 use warnings;
      5 use CGI;
      6 
      7 sub validate_email_address {
      8     my $addr_to_check = shift;
      9     $addr_to_check =~ s/("(?:[^"\\]|\\.)*"|[^\t "]*)[ \t]*/$1/g;
     10 
     11     my $esc         = '\\\\';
     12     my $space       = '\040';
     13     my $ctrl        = '\000-\037';
     14     my $dot         = '\.';
     15     my $nonASCII    = '\x80-\xff';
     16     my $CRlist      = '\012\015';
     17     my $letter      = 'a-zA-Z';
     18     my $digit       = '\d';
     19 
     20     my $atom_char   = qq{ [^$space<>\@,;:".\\[\\]$esc$ctrl$nonASCII] };
     21     my $atom        = qq{ $atom_char+ };
     22     my $byte        = qq{ (?: 1?$digit?$digit |
     23                               2[0-4]$digit    |
     24                               25[0-5]         ) };
     25 
     26     my $qtext       = qq{ [^$esc$nonASCII$CRlist"] };
     27     my $quoted_pair = qq{ $esc [^$nonASCII] };
     28     my $quoted_str  = qq{ " (?: $qtext | $quoted_pair )* " };
     29 
     30     my $word        = qq{ (?: $atom | $quoted_str ) };
     31     my $ip_address  = qq{ \\[ $byte (?: $dot $byte ){3} \\] };
     32     my $sub_domain  = qq{ [$letter$digit]
     33                           [$letter$digit-]{0,61} [$letter$digit]};
     34     my $top_level   = qq{ (?: $atom_char ){2,4} };
     35     my $domain_name = qq{ (?: $sub_domain $dot )+ $top_level };
     36     my $domain      = qq{ (?: $domain_name | $ip_address ) };
     37     my $local_part  = qq{ $word (?: $dot $word )* };
     38     my $address     = qq{ $local_part \@ $domain };
     39 
     40     return $addr_to_check =~ /^$address$/ox ? $addr_to_check : "";
     41 }
     42 
     43 sub send_feedback {
     44     my( $email, $message ) = @_;
     45     
     46     open MAIL, "| /usr/lib/sendmail -t -i"
     47         or die "Could not open sendmail: $!";
     48     
     49     print MAIL <<END_OF_MESSAGE;
     50 To: bottlenix\@gmail.com
     51 From: custservrobot\@perlcgi.tk
     52 Reply-To: $email
     53 Subject: Web Site Feedback
     54 
     55 Feedback from a user:
     56 
     57 $message
     58 END_OF_MESSAGE
     59     close MAIL or die "Error closing sendmail: $!";
     60 }
     61 
     62 sub send_receipt {
     63     my $email = shift;
     64     my $from_name = 'Customer Support';
     65     my $from_email = 'noreply@perlcgi.tk';
     66     
     67     open MAIL, "| /usr/lib/sendmail -t"
     68         or die "Could not open sendmail: $!";
     69     print MAIL <<END_OF_MESSAGE;
     70 To: $email
     71 From: $from_email
     72 Subject: Your feedback
     73 
     74 Your message has been sent and someone should be responding to you 
     75 shortly. Thanks for taking the time to provide us with your feedback!
     76 END_OF_MESSAGE
     77     close MAIL or die "Error closing sendmail: $!";
     78 }
     79 
     80 # Clean up environment for taint mode before calling sendmail
     81 BEGIN {
     82     $ENV{PATH} = "/bin:/usr/bin";
     83     delete @ENV{ qw( IFS CDPATH ENV BASH_ENV ) };
     84 }
     85 
     86 my $q       = new CGI;
     87 my $email   = validate_email_address( $q->param( "email" ) );
     88 my $message = $q->param( "message" );
     89 
     90 unless ( $email ) {
     91     print $q->header( "text/html" ),
     92           $q->start_html( "Invalid Email Address" ),
     93           $q->h1( "Invalid Email Address" ),
     94           $q->p( "The email address you entered is invalid. " .
     95                  "Please use your browser's Back button to " .
     96                  "return to the form and try again." );
     97           $q->end_html;
     98     exit;
     99 }
    100 
    101 send_feedback( $email, $message );
    102 send_receipt( $email );
    103 
    104 print $q->redirect( "/thanks.html" );