RSS feed logo RSS Feed | About Pantz.org
Send an PGP encrypted e-mail with PHP
Posted on 11-02-2002 21:13:00 EST | Updated on 11-02-2002 21:13:00 EST
Section: /software/php/ | Permanent Link

This is some PHP code that uses GNUPG to send an encrypted email to someone. Like having a user type a message in a message box on a webpage and have have that message sent to a person over email. The person recieving the e-mail would be able to decrypt the email because the form on the server used that persons public PGP key. Make sure the browser session connection is SSL encrypted (https) for the most security.

<? 

$testemail = "me@here.com";
$emailsubject = "Encrypted Information";
$emailfrom = "From: stranger@here.com;
$body = "-== Information to be Encrypted ==-\n\n";

//Tell gnupg where the key ring is. Home dir of user web server is running as.
putenv("GNUPGHOME=/var/www/.gnupg");

//create a unique file name
$infile = tempnam("/tmp", "PGP.asc");
$outfile = $infile.".asc";

//write form variables to email
$fp = fopen($infile, "w");
fwrite($fp, $body);
fclose($fp);

//set up the gnupg command. Note: Remember to put E-mail address on the gpg keyring.
$command = "/usr/bin/gpg -a --recipient 'Mini Me <me@here.com>' \\
--encrypt -o $outfile $infile";

//execute the gnupg command
system($command, $result);

//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 file
	unlink($outfile);


	//send the email
	mail ($testemail, $emailsubject, $contents, $emailfrom);

	print "<html>Thank you for your information. Your encrypted E-Mail has been sent.</html>";

     } 


}

if($result!=0) {
     print "<html>Their was a problem processing the informaion. Please contact <a href=\"mailto:webmaster@here.com\">webmaster@here.com</a>.";
}

?>

Del.icio.us! | Digg Me! | Reddit!

Related stories