note 47311 deleted from function.mail by aidan
| From: | aidan@php.net | Date: | Mon, 14 Feb 2005 02:22:26 +0000 |
| Subject: | note 47311 deleted from function.mail by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-84971@lists.php.net to get a copy of this message | ||
Note Submitter: mcgrof at ruslug dot rutgers dot edu
----
I have written a PHP PGP/MIME support routine. For now it only supports ASCII-text but it isn't
difficult to add support for other content-types. I maintain this on my personal page at:
http://mcgrof.com/php/pgp_mail/
Here is pgp_mail():
<?
/* PGP/MIME mail support */
function pgp_mail($to, $subject, $message, $headers, $from) {
/* All of these values can be empty, we leave it up to the user and
* SMTP server to deal with them as they please just as mail() does */
/* Per RFC can be up to 70 chars */
srand(date("s"));
for($i=0; $i<16; $i++) {
$boundary .= chr((rand()%26)+97);
}
$pgp_headers = "Mime-Version: 1.0\r\n";
$pgp_headers .= "Content-Type: multipart/encrypted; ";
$pgp_headers .= "protocol=\"application/pgp-encrypted\";\n\t";
$pgp_headers .= "boundary=\"$boundary\"\r\n";
$pgp_headers .= "Content-Disposition: inline\r\n";
$pgp_content .= "--$boundary\r\n";
$pgp_content .= "Content-Type: application/pgp-encrypted\r\n";
$pgp_content .= "Content-Disposition: attachment\r\n";
$pgp_content .= "\n";
$pgp_content .= "Version: 1\r\n";
$pgp_content .= "\n";
$pgp_content .= "--$boundary\r\n";
$pgp_content .= "Content-Type: application/octet-stream\r\n";
$pgp_content .= "Content-Disposition: inline; filename=\"msg.asc\"\r\n";
$pgp_content_tail = "\r\n--$boundary--\r\n";
/* Make sure you:
* gpg --search-keys "myemail@example.com"
* on your web-servers $HOME on your $to address to
* retrieve the public key. */
putenv("GNUPGHOME=/var/www/.gnupg");
$infile = tempnam("/tmp", "pgp_mail-");
$outfile = $infile.".asc";
$fp = fopen($infile, "w");
$command = "/usr/bin/gpg -a --trust-model always --recipient '$to' --encrypt -o
$outfile $infile";
$data = "Content-Type: TEXT/PLAIN; charset=US-ASCII\r\n";
$data .= "Content-Disposition: inline\r\n\n";
$data .= $message;
fwrite($fp, $data);
fclose($fp);
system($command, $result);
unlink($infile);
if ($result!=0)
return false;
$fp = fopen($outfile, "r");
if(!$fp||filesize ($outfile)==0)
return false;
$encrypted_data = fread ($fp, filesize ($outfile));
unlink($outfile);
$headers .= $pgp_headers;
$encrypted_data = $pgp_content . $encrypted_data . $pgp_content_tail;
return mail($to, $subject, $encrypted_data, $headers, "-f" . $from);
}
?>