PDA

View Full Version : Perl code runs from command line, but not CGI


jblock
12-12-2003, 10:55 PM
Help! This block of code encrypts some text and sends it to me by email. It runs fine from the command line when I type:

perl myscript.pl

But when I embed it in a cgi script and call it from a web form it does not run. I don't get a syntax error, I just do not get the mail. I don't think it's an environmental problem. My script sets all the required environment variables and paths, so the cgi knows where my encryption key is, where pgp is, where sendmail is, etc.

Any ideas?

use IPC::Open2;

sub encrypt_order {

# Set up the pgp command
$pgpcmd = $pgpprog . " -efa +force \'$pgpuserid\'";

# Open the PGP program for bidirectional I/O
open2(\*READPGP, \*WRITEPGP, $pgpcmd) || die "Can't open PGP: $!\n";

# Send text to be encrypted to PGP
print WRITEPGP $unencrypted;

# Encrypt the data
close(WRITEPGP);

# Get the encrypted data from PGP
@encrypted = <READPGP>;
$encrypted = join ('', @encrypted);
close(READPGP);

}


sub send_order {

# Open The Mail Program
open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog: $!\n";

# Standard headers
print MAIL "From: Administrator \<admin@mysite.com\>\n";
print MAIL "To: $recipient\n";
print MAIL "Subject: In-Line Encryption Test\n\n";

print MAIL $encrypted;

close (MAIL);

}

Jeff Mott
12-13-2003, 02:42 AM
print MAIL "From: Administrator \<admin@mysite.com\>\n";This line will treat @mysite as an array and attempt to interpolate it. You'll need to either use single quotes or \ escape the @ symbol. The code you posted doesn't show how $recipient is set but you may want to check it for the same kind of problem.

jblock
12-14-2003, 07:17 AM
I caught the error in the email address. That's not it. Also, my full script sets $recipient. I discovered that pgp/encryption has nothing to do with it (that's a relief). The same this happened when I tried a simpler version with 'cat' -- it ran from the command line, not when called from a cgi form. My environment is Linux/Apache, btw. Any known incompatibilities with open2?

Here's the cat version, with same problem. Assume $cleartext1 and $recipient are set.

sub cat_order {

# Set up the cat command
$catcmd = "cat -u";

# Open the cat program for bidirectional I/O
$pid = open2(\*READER, \*WRITER, $catcmd) || die "Can't open cat: $!\n";

# Send text to cat
print WRITER $cleartext1;

# Send the data
close(WRITER);

# Get the data from cat
@cattext = <READER>;
close(READER);

}


sub send_order {

# Open The Mail Program
open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog: $!\n";

# Standard headers
print MAIL "From: Admininstrator \<admin\@oliviasecret.com\>\n";
print MAIL "To: $recipient\n";
print MAIL "Subject: In-Line ClearText Test\n\n";

print MAIL @cattext;

close (MAIL);

}

jblock
12-14-2003, 07:23 AM
If anyone has a linux/apache or linux environment, I would really appreciate is someone could run the cat example from a form, just to see if it runs on a completely different server environment (change email addresses of course). At least that would point to the server, not the code; then all I have to do is work with my ISP and figure out why the code bombs on HIS server. Thanks.