Saturday, February 23, 2008

Simple CGI AutoResponder Form

Hello again,

Today, we're going to hit up a fairly common CGI form technique used by pretty much every site on the internet today. At least, every e-commerce or interactive site. It's become expected, nowadays, that when you place an order for this or that, or you sign up for a message board, etc, that you'll be receiving an email to confirm this fact in a matter of minutes, if not seconds.

Our form below is a simple Perl CGI script that accepts input and reacts to it, by sending out an email to the person (or robot ;) requesting the information, and alerting us that we have a new mailing list member. It's somewhat similar, although completely different in function, to our previous post on processing and collecting emails from a CGI form. That script was functionally sound, but lacked the "reaction" that this script incorporates.

Like I said, this is a fairly bare-bones script (No limitations on Unix or Linux flavors it'll run on, as long as they can run Perl), so we're not getting deep into error checking or backend database operations, shopping carts, etc. Just a simple give and take (or take and give, depending on how you look at it ;). The user is going to input some requested data, and we're going to send them confirmation that we received that data (as well as a notification to ourselves). This CGI form assumes a frontend that passes it the form variables listed as $form{'VARIABLE_NAME'} throughout.

Here it is. Enjoy it and, hopefully, find some use for it within your larger applications :)


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/usr/bin/perl

#
# JoinMailList.pl
# Simple Autoresponder Example
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

print "Content-type: text/html\n\n";
if ($ENV{'REQUEST_METHOD'} ne 'POST')
{
print <<"HTML";
<html><head><title>Whoops!</title></head>
<body><h1>Please Only Use the POST Method with this script!</h1>
</body></html>
HTML
exit;
}
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;
}
chomp($date = `date`);
open(MAIL, "|/usr/lib/sendmail -t") || die "Can't open mailer!";
print MAIL "To: $form{'email'}\n";
print MAIL "From: OURCOMPANY\@OURSITE.COM\n";
print MAIL "Subject: Thanks For Joining Our Mailing List\n\n";
print MAIL <<"EOA";
Hey There,
Thanks a million for joining our mailing list. We will send you your first
email as soon as possible!
EOA
close(MAIL);
open(MAILER, "|/usr/lib/sendmail -t") || die "Can't open mailer!";
print MAILER "To: OURCOMPANY\@OURDOMAIN.COM\n";
print MAILER "From: newusers\@OURDOMAIN.COM\n";
print MAILER "Subject: Someone Just Joined Our Mailing List!\n\n";
print MAILER <<"EOB";
On $date, $form{'fname'} $form{'lname'} decided to join our little mailing list.
Yeehah
EOB
print MAILER <<"EOC";
$form{'fname'}\'s personal info:
$form{'fname'} $form{'lname'}
$form{'email'}
$form{'street'}
$form{'city'}, $form{'state'} $form{'zip'}
EOC
print MAILER "Please add $form{'fname'} to our list ASAP!\n";
print MAILER "Maybe we can finally sell something!\n";
close(MAILER);
print <<"HTML";
<html><head><title>Thanks for signing up!</title></head>
<body><h1> Thank you for joining our mailing list! </h1>
<b>$form{'fname'}</b>!,
Welcome to our mailing list. You should be receiving a confirmation email shortly!
The Mgmt.
</body></html>
HTML
exit;


, Mike