commit d9ce5eb20b3e7ddf0718a9f7bba3a6bf7ba89c3a
parent 65f471fefc8926b21020a633aa2fb22fb5bdc224
Author: Samir Parikh <noreply@samirparikh.com>
Date: Wed, 2 Mar 2022 13:48:18 +0000
add feedback_sendmail.cgi
Diffstat:
A | feedback_sendmail.cgi | | | 104 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 104 insertions(+), 0 deletions(-)
diff --git a/feedback_sendmail.cgi b/feedback_sendmail.cgi
@@ -0,0 +1,104 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use CGI;
+
+sub validate_email_address {
+ my $addr_to_check = shift;
+ $addr_to_check =~ s/("(?:[^"\\]|\\.)*"|[^\t "]*)[ \t]*/$1/g;
+
+ my $esc = '\\\\';
+ my $space = '\040';
+ my $ctrl = '\000-\037';
+ my $dot = '\.';
+ my $nonASCII = '\x80-\xff';
+ my $CRlist = '\012\015';
+ my $letter = 'a-zA-Z';
+ my $digit = '\d';
+
+ my $atom_char = qq{ [^$space<>\@,;:".\\[\\]$esc$ctrl$nonASCII] };
+ my $atom = qq{ $atom_char+ };
+ my $byte = qq{ (?: 1?$digit?$digit |
+ 2[0-4]$digit |
+ 25[0-5] ) };
+
+ my $qtext = qq{ [^$esc$nonASCII$CRlist"] };
+ my $quoted_pair = qq{ $esc [^$nonASCII] };
+ my $quoted_str = qq{ " (?: $qtext | $quoted_pair )* " };
+
+ my $word = qq{ (?: $atom | $quoted_str ) };
+ my $ip_address = qq{ \\[ $byte (?: $dot $byte ){3} \\] };
+ my $sub_domain = qq{ [$letter$digit]
+ [$letter$digit-]{0,61} [$letter$digit]};
+ my $top_level = qq{ (?: $atom_char ){2,4} };
+ my $domain_name = qq{ (?: $sub_domain $dot )+ $top_level };
+ my $domain = qq{ (?: $domain_name | $ip_address ) };
+ my $local_part = qq{ $word (?: $dot $word )* };
+ my $address = qq{ $local_part \@ $domain };
+
+ return $addr_to_check =~ /^$address$/ox ? $addr_to_check : "";
+}
+
+sub send_feedback {
+ my( $email, $message ) = @_;
+
+ open MAIL, "| /usr/lib/sendmail -t -i"
+ or die "Could not open sendmail: $!";
+
+ print MAIL <<END_OF_MESSAGE;
+To: bottlenix\@gmail.com
+From: custservrobot\@perlcgi.tk
+Reply-To: $email
+Subject: Web Site Feedback
+
+Feedback from a user:
+
+$message
+END_OF_MESSAGE
+ close MAIL or die "Error closing sendmail: $!";
+}
+
+sub send_receipt {
+ my $email = shift;
+ my $from_name = 'Customer Support';
+ my $from_email = 'noreply@perlcgi.tk';
+
+ open MAIL, "| /usr/lib/sendmail -t"
+ or die "Could not open sendmail: $!";
+ print MAIL <<END_OF_MESSAGE;
+To: $email
+From: $from_email
+Subject: Your feedback
+
+Your message has been sent and someone should be responding to you
+shortly. Thanks for taking the time to provide us with your feedback!
+END_OF_MESSAGE
+ close MAIL or die "Error closing sendmail: $!";
+}
+
+# Clean up environment for taint mode before calling sendmail
+BEGIN {
+ $ENV{PATH} = "/bin:/usr/bin";
+ delete @ENV{ qw( IFS CDPATH ENV BASH_ENV ) };
+}
+
+my $q = new CGI;
+my $email = validate_email_address( $q->param( "email" ) );
+my $message = $q->param( "message" );
+
+unless ( $email ) {
+ print $q->header( "text/html" ),
+ $q->start_html( "Invalid Email Address" ),
+ $q->h1( "Invalid Email Address" ),
+ $q->p( "The email address you entered is invalid. " .
+ "Please use your browser's Back button to " .
+ "return to the form and try again." );
+ $q->end_html;
+ exit;
+}
+
+send_feedback( $email, $message );
+send_receipt( $email );
+
+print $q->redirect( "/thanks.html" );