Take the 2-minute tour ×

Hey all, I have this code:

 $fp = fopen($unenc_path, "w");
 fwrite($fp, $msg);

 $easy_access_emails = 'person@##.com';
 $headers =   "From: support@##.com <support@##.com>\n" . 
              "Reply-to: support@##.com\n" . 
              "Subject: " . $subject . "\n";

 $key = implode("", file("../newcert.pem"));

 $ArrayMessageProperties = explode("\n", $headers);

 $unenc_path = '..\\tmp\\'. preg_replace('/[^0-9]/','', microtime()) . rand(0,1000) . "msg.txt";

 if (openssl_pkcs7_encrypt($unenc_path, $enc_path, $key, $ArrayMessageProperties))
    $info = file_get_contents($enc_path);

    foreach ($easy_access_emails as $email)
         mail($email, $subject, $info, $headers);
} else {
    die("Failed Encryption");

Which works correctly on my local dev environment (Macbook running LAMP). I moved it to a windows server for testing and now openssl_pkcs7_encrypt fails every time. I assume this is a permissions issue, since the function needs to write to $enc_path; but I've made the directory on the windows server about as open as is possible. (set full control to just about every user / group that might have something to do with it. Anyone know a good method to debug this? It appears the function simply returns false when it fails with no indication as to why.

Upon further investigation it appears the script has write access the directory. It writes to the $unenc_path no problem, just fails on the call to openssl_pkcs7_encrypt.

Another update: I'm using filemon to watch the request some in and i see it opening and writing to the unencrypted file but there's no output for it even trying to write to the encrypted file.

Also added the line that loads the key. I've verified it's loading by echoing it out and it seems to be good.

Another update: Watching filemon again, around the time it should be calling openssq_pkcs7_encrypt I'm seeing an entry that it's looking for the unencrypted message in the windows tmp directory.

w3wp.exe:4172  C:\windows\system32\tmp\04277530010012336..msg.txt    PATH NOT FOUND  Options: Open Access: Read
share|improve this question
can you tell us which error php gives to you using error_reporting(E_ALL) ? –  Manuel Dalla Lana Feb 3 '09 at 19:41
Thanks for the reply. I set E_ALL and it's only spitting out a notice about an unrelated, undefined variable (thanks, saved me a bug report later). I fixed that and it still just fails with no error message. –  Electronic Zebra Feb 3 '09 at 19:45
add comment

2 Answers

up vote 3 down vote accepted

Turned out Windows / Openssl_pkcs7_encrypt didn't like that relative path, giving a full path after messing with forward and backslashes (/. \) for a bit fixed it.

share|improve this answer
add comment

Are you loading the key in $key somewhere in your code? I can't see it on the snippet

share|improve this answer
Thanks for the reply. I've added the line it's being pulled in, and some notes regarding my current progress. –  Electronic Zebra Feb 3 '09 at 20:21
add comment

Your Answer


