PDA

View Full Version : Email Script Problems


MattJakel
07-07-2004, 08:48 PM
Well, it's been a month or so since I've really done anything with CGI, so I might just be making a stupid mistake here. I've got a simple script that takes form input and emails it. For the most part, it just gets the key/value pairs and sends them as "$key : $value \n". However, I would like the "formname" field to determine the subject of the email, so I have this part of the script:


$size = $ENV{'CONTENT_LENGTH'};
read (STDIN, $form_info, $size);
my $bodytext;
my $formname;
foreach $pair (split (/&/, $form_info)) {
($key, $value) = split (/=/, $pair);
$key =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
#if ($key eq "formname") {
#$formname = $value;
#} else {
$bodytext = "${bodytext}$key \: $value \n";
#}
}
#if ($formname eq "newsletter") {
#my $subject = "Mailing List Signup";
#my $toaddr = "email\@domain.com";
#} elsif ($formname eq "article") {
#my $subject = "Article Rating";
#my $toaddr = "email\@domain.com";
#} else {
my $subject = "Test";
my $toaddr = "email\@domain.com";
#}


The script works when I comment out the lines commented out in the code above, but when I uncomment them, a blank email is sent. Am I just making a stupid mistake here?

Thanks,
Matt

mlseim
07-07-2004, 09:04 PM
Here's the basic, no security, run-of-the-mill emailer.

The HTML Form would use method="POST" to send the variables as listed.

You can see what you are doing wrong with your code ...
you are missing the part where you convert the 'parsed' variable
to a regular string variable: $username = $FORM{'username'};

-------------------------------------------------------------------------
#!/usr/bin/perl

$mailprogram = '/usr/sbin/sendmail';

$site_url = 'my company';

$myemail = 'bob@aol.com'; #put YOUR email address here

# Get the form variables

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$buffer = $ENV{'QUERY_STRING'};
}
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}

# Break em up into a format the script can read

@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;
}

# Assign shorter variable names

if ($FORM{'username'}) { $username = $FORM{'username'}; }
if ($FORM{'sender'}) { $sender = $FORM{'sender'}; }
if ($FORM{'subject'}) { $subject = $FORM{'subject'}; }
if ($FORM{'message'}) { $message = $FORM{'message'}; }
if ($FORM{'phone'}) { $userphone = $FORM{'phone'}; }

# Send e-mail to you

open (MAIL, "|$mailprogram -t") or die "Can't fork sendmail.\n";
print MAIL "To: $myemail\n";
print MAIL "From: $sender\n";
print MAIL "Subject: $subject\n";
print MAIL "\n";
print MAIL "Name: $username\n";
print MAIL "Phone: $userphone\n";
print MAIL "=====================================================\n";
print MAIL "$message\n\n";
print MAIL "\n";
close(MAIL);

# Print the success message

print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Thank-you - E-mail Sent</TITLE></HEAD>\n";
print "<BODY><center>\n";
print "<H2>Thank-You</H2>\n";
print "Your email has been sent.<P>\n";
print "<P><B>Click here to <A HREF=$site_url>return</A>.</B></P>\n";
print "</center></BODY></HTML>\n";

-------------------------------------------------------------------------

MattJakel
07-07-2004, 11:18 PM
What I'm trying to do is have the script send all of the values from the key/value pairs without knowing the names of those keys... And I still don't see why my script wouldn't work... I don't need the k/v pairs to be saved to the hash, and I only need it parsed if the key is formname...

Thanks again,
Matt