note 47311 added to function.mail
| From: | mcgrof at studorgs dot rutgers dot edu | Date: | Wed, 10 Nov 2004 23:57:04 +0000 |
| Subject: | note 47311 added to function.mail | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80380@lists.php.net to get a copy of this message | ||
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);
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.mail.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47311
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47311&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47311&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47311&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47311&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47311&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47311&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47311&report=yes
Search -- http://master.php.net/manage/user-notes.php