Need help with perl script

Hey guys, I am having problems. Long time HTML coder, which these days is like knowing COBOL, and am trying to get my form emailed back to me. I’ve tried lots, but never get the email. Can anyone help?

#!/usr/bin/perl –w
use CGI::Carp qw(fatalsToBrowser);

The following accepts the data from the form and splits it into its component parts


	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;

open(MAIL, "| $usr/sbin/sendmail -t"); 

	print MAIL "To: mail\\\
"; # Don't forget to escape this @ symbol!
	print MAIL "From: $FORM{Customer}\
	print MAIL "Reply To: $FORM{E-Mail}\
	print MAIL "Phone: $FORM{Phone}\
	print MAIL "Subject: Web Submitted Order from $FORM{Contact} \
	print MAIL "Printing Information: $FORM{OrderInfo}\
	print MAIL "Print Location: $FORM{printloc}\
	print MAIL "Follow Up Info: $FORM{Followup}\

close (MAIL);

Any help would be greatly appreciated.

Nothing jumps out at me as non-working and you haven’t said in what way it’s failing to work other than that you don’t receive the mail, so I don’t know what will get it working, but there’s a good chance that it may be on the mail service side - have you checked the sending machine’s mail queue for undelivered messages, checked the sendmail log for errors, etc.?

I would, however, strongly recommend that you not attempt to interface with CGI and email by hand. Those are easy things to get subtly wrong, so it’s much better to use well-tested modules to handle those interactions for you.

For the web side, I would normally suggest that you look at a web framework such as Dancer, [url=]Mojolicious, or [url=]Catalyst, but, in this case, I [i]guess[/i] you could get away with using plain old [url=], although that’s very nasty and old and really not the best way to go if you ever find yourself wanting to do something more involved than receiving a single form and emailing it.

On the email side, there are many, many options, but I’ll recommend looking into MIME::Lite, mainly because it’s what I like and use myself.

Here’s a quick (and untested!) example which uses and MIME::Lite to do what your posted code appears to intend:

#!/usr/bin/env perl    

use strict;
use warnings;

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use MIME::Lite;

my $q = CGI->new;

my $body = "Printing Information: " . $q->param('OrderInfo') . "\
$body .= "Print Location: " . $q->param('printloc') . "\
$body .= "Follow Up Info: " . $q->param('Followup') . "\
$body .= "Phone: " . $q->param('Phone') . "\

my $msg = MIME::Lite->new(
  From => $q->param('Customer'),
  To    => '',   
  Reply-To => $q->param('E-Mail'),
  Subject => 'Web Submitted Order from ' . $q->param('Contact'),
  Data  => $body,


As I said, this code is untested, but it’s something I’ve done enough times before that it should, at worst, be very close to correct.

Note that I moved “Phone” into the message body, as it is not a standard SMTP header. Depending on server configurations, it’s entirely possible that your problem may be that a server is rejecting the message due to the invalid “Phone” header.

I would also definitely recommend that you standardize how the field names on your form are capitalized. It may not be a problem yet, but you will one day drive yourself crazy with errors caused by typing “PrintLoc” instead of “printloc”.

Thank you so much for taking the time to do this. I appreciate it. I will try each of these things, but most importantly the code you wrote. THANKS!

You rock, Dave!

I wonder if we should link to that lightning talk, Must Die in this Perl section.

Here’s a video from the Israeli Perl Workshop (really starts at 1min.30) since I don’t think the YAPC onces are online yet.