[ back to toc ]

sort in formmail

Date: 2002/02/12 10:27

I have a formmail cgi program that works fine. The only problem is when I
receive the information thru my email it has not specific order I get the
state then the name then some check off option. Is there anyway to make
it return the same way it was layed out in the form? Or is there a way I
can tell it how to send the information back. I provided the cgi program


print "Content-type:text/html\n\n";

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;
$mailprog = '/usr/sbin/sendmail';

# change this to your own email address

$recipient = 'HAMLET59@aol.com';

# this opens an output stream and pipes it directly to the sendmail
# program. If sendmail can't be found, abort nicely by calling the
# dienice subroutine (see below)

open (MAIL, "|$mailprog -t") or &dienice("Can't access $mailprog!\n");

# here we're printing out the header info for the mail message. You must
# specify who it's to, or it won't be delivered:

print MAIL "To: $recipient\n";

# Reply-to can be set to the email address of the sender, assuming you
# have actually defined a field in your form called 'email'.

print MAIL "Reply-to: $FORM{'email'} ($FORM{'name'})\n";

# print out a subject line so you know it's from your form cgi.
# The two \n\n's end the header section of the message. anything
# you print after this point will be part of the body of the mail.

print MAIL "Subject: Form Data\n\n";

# here you're just printing out all the variables and values, just like
# before in the previous script, only the output is to the mail message
# rather than the followup HTML page.

foreach $key (keys(%FORM)) {
print MAIL "$key = $FORM{$key}\n";

# when you finish writing to the mail message, be sure to close the
# input stream so it actually gets mailed.


# now print something to the HTML page, usually thanking the person
# for filling out the form, and giving them a link back to your homepage

print <<EndHTML;
<h2>Thank You</h2>
Thank you for writing. Your mail has been delivered.<p>
Return to our <a href="index.html">home page</a>.

sub dienice {
($errmsg) = @_;
print "<h2>Error</h2>\n";
print "$errmsg<p>\n";
print "</body></html>\n";

The easiest way is to sort the fields into key alphabetic order. To do
that you can alter the code:

foreach $key (keys(%FORM))

print MAIL "$key = $FORM{$key}\n";


foreach $key (sort keys(%FORM)) {
my $pkey = $key;
$pkey =~ s/^...//;#cut off first three chars
print MAIL "$pkey = $FORM{$key}\n";

and name the keys for example:


and so on to ensure that the key names are in the order you want. This is
a little trick.


Please take your time and rate me at AllExperts.com
I ask you to do that the way you believe I deserve.
This is a feedback that I get from allexperts but
it does not affect my life in any financial way. This
is just a mental feedback.

In case you think my answer was really good and if you feel
it appropriate do not hesitate to nominate me to volunteer
of the month.

I do this job volunteer in my free time and this is all I get
as compensation.

[ back to toc ]