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