note 84484 added to function.openssl-pkcs7-encrypt

From: Date: Wed, 16 Jul 2008 13:15:36 +0000
Subject: note 84484 added to function.openssl-pkcs7-encrypt
Groups: php.notes 
Request: Send a blank email to php-notes+get-141563@lists.php.net to get a copy of this message
In the previous example, the decrypted message couldn't be read by the 'popular' mail clients. Those mail clients needed also headers in the encrypted part. I also noticed that there were some double headers in the previous example ('To:' and 'Subject:' were not overriden by the Headers parameter in mail()). This is also corrected by unsetting 'To:' and 'Subject:' in $headers_msg. body.txt is the file with the mail body. publickey.cer is the file with the public certificate. <?php // Setup mail headers. $headers = array("From" => "from@mail.com", "To" => "to@mail.com", "Subject" => "Encrypted mail readable with most clients", "X-Mailer" => "PHP/".phpversion()); // Get the public key certificate. $pubkey = file_get_contents("publickey.cer"); // Header for encrypted part $eol = "\r\n"; $enc_header .= "From: ".$headers['From'].$eol; $enc_header .= "To: ".$headers['To'].$eol; $enc_header .= "Subject: ".$headers['Subject'].$eol; $enc_header .= "Content-Type: text/plain; format=flowed; charset=\"iso-8859-1\"; reply-type=original".$eol; $enc_header .= "Content-Transfer-Encoding: 7bit".$eol; $enc_header .= "\n"; // Prepend header for encrypted message $body = file_get_contents("body.txt"); $msg = $enc_header.$body; file_put_contents("msg.txt", $msg); // Remove some double headers for mail() $headers_msg = $headers; unset($headers_msg['To'], $headers_msg['Subject']); // Encrypt message openssl_pkcs7_encrypt("msg.txt", "enc.txt",$pubkey,$headers_msg,0,1); // Seperate headers and body for mail() $data = file_get_contents("enc.txt"); $parts = explode("\n\n", $data, 2); // send mail mail($headers['To'], $headers['Subject'], $parts[1], $parts[0]); ?> ---- Server IP: 195.207.10.20 Probable Submitter: 88.197.178.80 ---- Manual Page -- http://www.php.net/manual/en/function.openssl-pkcs7-encrypt.php Edit -- https://master.php.net/note/edit/84484 Del: integrated -- https://master.php.net/note/delete/84484/integrated Del: useless -- https://master.php.net/note/delete/84484/useless Del: bad code -- https://master.php.net/note/delete/84484/bad+code Del: spam -- https://master.php.net/note/delete/84484/spam Del: non-english -- https://master.php.net/note/delete/84484/non-english Del: in docs -- https://master.php.net/note/delete/84484/in+docs Del: other reasons-- https://master.php.net/note/delete/84484 Reject -- https://master.php.net/note/reject/84484 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141563) next »