If you want to decrypt a received email, keep in mind that you need the full encrypted message including the mime header.
<?php
$encrypted = imap_fetchmime($stream, $msg_number, "1", FT_UID);
$encrypted .= imap_fetchbody($stream, $msg_number, "1", FT_UID);
$infile = tempnam("", "enc");
file_put_contents($infile, $encrypted);
$outfile = tempnam("", "dec");
$public = file_get_contents("/path/to/your/cert.pem");
$private = array(file_get_contents("/path/to/your/cert.pem"), "password");
if(openssl_pkcs7_decrypt($infile, $outfile, $public, $private))
{
echo file_get_contents($outfile);
}
else
{
echo "Oh oh! Decryption failed!";
}
@unlink($infile);
@unlink($outfile);
?>