Hello,
I wrote a post earlier about my problems with my PHP script which I am using to call GPG. Perhaps I should have asked anyone with GPG knowledge to step forward and help with my problem.
I am currently trying to run a script which will send an encrypted e-mail to my e-mail account. However, when I run the PHP script to call my GPG command, something is not working right.
The sender's e-mail, title of the e-mail, and body of the e-mail are captured on a page using html forms that links to the following PHP code. I am getting result of 2 in my GPG command and I need a result of 0. :rolleyes: As you can see I am echoing the command, outfile, infile, and result to the screen. I cannot see anything wrong with my command and my outfile and infile are named correctly and in the right directory. GPG can also be run globally on my web server I am using so I do not need a directory to GPG. Any help would be wonderful.
Here is my code:
<?
$to_email = "myemail@mydotcom.com";
//tell gpg where to find the key ring
putenv("GNUPGHOME=/mydir/.gnupg/");
//create a unique file name
$infile = tempnam("", "pgp");
$outfile = $infile.".asc";
//write the user's text to the file
$fp = fopen($infile, "w");
fwrite($fp, $body);
fclose($fp);
//set up the command
$command = "gpg -a --recipient 'myname' --encrypt -o $outfile $infile";
//error finding code
echo "<p>this is the outfile: ".$outfile."</p>";
echo "<p>this is the infile: ".$infile."</p>";
echo "<p>this is the command: ".$command."</p>";
//execute the gpg command
system($command, $result);
echo "<p>this is the result: ".$result."</p>";
//delete the unencrypted temp file
unlink($infile);
if ($result==0)
{
$fp = fopen($outfile, "r");
if (!$fp||filesize ($outfile)==0)
{
$result = -1;
}
else
{
//read the encrypted file
$contents = fread ($fp, filesize ($outfile));
//delete the encrypted temp file
unlink($outfile);
mail ($to_email, $title, $contents, "From: $from\n");
echo "<h1>Message Sent</h1>
<p>Your message was encrypted and sent.
<p>Thank you.";
}
}
if ($result!=0)
{
Echo "<h1>Error:</h1>
<p>Your message could not be encrypted, so has not been sent.
<p>Sorry.";
}
?>