note 98173 deleted from function.openssl-pkcs7-encrypt by visualmind
| From: | visualmind@php.net | Date: | Mon, 31 May 2010 12:08:10 +0000 |
| Subject: | note 98173 deleted from function.openssl-pkcs7-encrypt by visualmind | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169737@lists.php.net to get a copy of this message | ||
Note Submitter: Marcus Carey
----
<?php
SendEncryptedMail();
?>
<?php
function SendEncryptedMail()
{
$ret = 0;
$cert = "yourpubliccert.crt";
$infile = "filetoencrypt.txt";
$message = ""; // No message needed. It will be appended to the end of the headers.
$to = "recipient@domain.com";
$subject = "Encrypted Message";
$domain = "mailhost.com";
$maildate = MailDate();
$messageid = MessageID($domain,$maildate);
$headers = 'To: <recipient@domain.com>' . "\r\n";
$headers .= 'From: <you@.mailhost.com>' . "\r\n";
$headers .= $maildate;
$headers .= $messageid;
$contenttype = "Content-Type: text/plain\r\n\r\n"; //The Content-Type: will be
encrypted.
$pkcs7 = PKCS7_encrypt($cert,$infile,$contenttype);
$headers .= $pkcs7; // Append the encrypted data to the end of the headers.
$ret = mail($to, $subject, $message , $headers);
if($ret)
echo 'Mail sent.';
else
echo 'Mail send failed.';
}
?>
<?php
function PKCS7_encrypt($Cert,$Infile,$ContentType){
$clearfile = tempnam("temp","email") . ".txt";
$outfile = $clearfile . ".enc";
$clearfile .= ".txt";
$headers = array(); // No headers needed.
$pubkey = file_get_contents($Cert);
$txt = file_get_contents($Infile);
$data = "";
$message = $ContentType . $txt; // Create the message. The ContentType: header must be the first
line of the encrypted data.
$len = strlen($message);
$fh = fopen($clearfile,"w");
fwrite($fh,$message,$len);
fclose($fh);
$ret = openssl_pkcs7_encrypt($clearfile, $outfile, $pubkey, $headers);
if($ret){
$data = file_get_contents($outfile);
}else{
while ($msg = openssl_error_string())
echo $msg . "<br />\n";
}
return $data;
}
?>
<?php
function MailDate()
{
// Set your timezone
date_default_timezone_set('America/Los_Angeles');
$today = 'Date: ' . date("D, j M Y G:i:s O") . "\r\n";
return $today;
}
?>
<?php
function MessageID($domain, $maildate)
{
$id = md5($maildate);
$messageid = 'Message-ID: ' . '<' . $id . '@' . $domain .
'>' . "\r\n";
return $messageid;
}
?>