note 98173 deleted from function.openssl-pkcs7-encrypt by visualmind

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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 = "[email protected]";
	$subject = "Encrypted Message";

	$domain = "mailhost.com";
	$maildate = MailDate();
	$messageid = MessageID($domain,$maildate);
	
	$headers = 'To: <[email protected]>' . "\r\n";
	$headers .= 'From: <[email protected]>' . "\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;
}
?>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.