note 36598 added to function.openssl-pkcs7-encrypt
| From: | richardaburton at hotmail dot com | Date: | Wed, 15 Oct 2003 21:38:04 +0000 |
| Subject: | note 36598 added to function.openssl-pkcs7-encrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-58666@lists.php.net to get a copy of this message | ||
Using sendmail isn't very portable, and seems daft since PHP has the mail function which will
do the job. Problem is how do you use the mail function to send this email, since it's already
complete with headers?
If you pull in the contents of the file produced by openssl_pkcs7_encrypt and pass this as the
message data to the mail command, you end up with an email with two sets of headers (one set from
the encrypt function, another added by the mail command). The result is that the second set of
headers (which tell the mail client the email is encrypted) get ignored and the (base64 encoded)
encrypted mail is shown as-is, rather than being decrypted.
The solution is quite simple, but it took me a little while to think of it, so I'm sharing it
here. Once you load the contents of the file, split the headers off the body. Then pass the headers
as the additional_headers parameter to the mail function, and just the body of the email as the
message parameter of the mail function.
You will need to specify the to & subject parameters, but these will be overriden in the final
email (as delievered to the recipiant) by the ones from the real encrypted email.
<?php
$pubkey = file_get_contents("cert.pem");
openssl_pkcs7_encrypt("msg.txt", "enc.txt", $pubkey,
array("To" => "nighthawk@example.com",
"From" => "HQ <hq@example.com>",
"Subject" => "Eyes only"), 0)
$data = file_get_contents("enc.txt");
// separate header and body, to use with mail function
// unfortunate but required, else we have two sets of headers
// and the email client doesn't decode the attachment
$parts = explode("\n\n", $data, 2);
// send mail (headers in the Headers parameter will override those
// generated for the To & Subject parameters)
mail($mail, $subject, $parts[1], $parts[0]);
?>
Richard.
----
Manual Page -- http://www.php.net/manual/en/function.openssl-pkcs7-encrypt.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+36598
Delete -- http://master.php.net/manage/user-notes.php?action=delete+36598&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+36598&report=yes
Search -- http://master.php.net/manage/user-notes.php