pantz.org banner
Send an PGP encrypted e-mail with PHP
Posted on 11-03-2002 02:13:00 UTC | Updated on 11-03-2002 02:13:00 UTC
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


RSS Feed RSS feed logo
About


3com
3ware
alsa
alsactl
alsamixer
amd
apache
areca
arm
ati
auditd
awk
badblocks
bind
bios
bonnie
cable
carp
cat5
cdrom
centos
chart
cifs
cisco
comcast
commands
comodo
compiz-fusion
corsair
cpufreq
cpufrequtils
cpuspeed
cron
crontab
crossover
cu
cups
cvs
database
dbus
dd
dd_rescue
ddclient
debian
decimal
dhclient
dhcp
diagnostic
diskexplorer
disks
dns
dos
dovecot
dsniff
dvdauthor
e-mail
editor
emerald
ethernet
ext3
fat32
fedora
fetchmail
fiber
filesystems
firefox
firewall
flexlm
floppy
flowtools
fonts
format
ftp
gdm
gnome
greylisting
growisofs
grub
harddrive
hba
hex
hfsc
html
http
idl
ie
intel
ios
ipmi
iptables
irix
javascript
kde
kernel
kmail
kprinter
krecord
kubuntu
ldap
linux
logfile
maradns
matlab
memory
mencoder
mhdd
mkinitrd
mkisofs
moinmoin
motherboard
mouse
movemail
mplayer
multitail
mutt
myodbc
mysql
mythtv
nagios
nameserver
netflow
nic
ntfs
ntp
nvidia
odbc
openbsd
openntpd
openoffice
openssh
openssl
opteron
parted
partimage
patch
perl
pf
pfflowd
pfsync
photorec
php
pop3
pop3s
ports
postfix
power
procmail
proftpd
proxy
putty
pxe
python
qemu
r-studio
raid
recovery
router
rpc
rsync
samba
schedule
scsi
seagate
seatools
sed
sendmail
sgi
shell
siw
smtp
snort
solaris
soundcard
spam
spamd
sql
sqlite
squid
ssh
ssh.com
ssl
subnet
subversion
sun
supermicro
switches
symbols
syslinux
systemrescuecd
t1
tcpip
tcpwrappers
telnet
terminal
testdisk
tftp
thttpd
thunderbird
timezone
tools
tr
trac
tuning
tunnel
vi
wget
wiki
windows
windowsxp
wireless
wpa_supplicant
x
xauth
xfree86
xfs
xinearama
xmms
zdump
zic
zlib