Open piernik opened 4 years ago
smime.p7m
needs a separate viewer or a library. This is reportedly possible to do with OpenSSL like so:
openssl pkcs12 -in <key file.p7m> -out <key_file.pem> -nodes
You are saying that I am not able to parse this email with php-mime-mail-parser
? Am I able to parse it with php
at all?
Yes this lib only parse "normal" emails, an we can't parse encrypted emails.
It could be a feature request, but not sure to have time now. The workaround for you is to use another lib to decode theses emails.
Your lib is best so if You can implement it would be great.
Can You give me some hints how to do it myself?
Provided you have a key to decrypt this file, you can use proc_open()
to ask OpenSSL to decrypt this file for you.
But I don't have a key :) This email isn't mine - it's from my CRM's customer. But even without key why I can see email contents in windows' mail app?
Well, if you can share this smime.p7m
, I can have a closer look.
I send it to You via Your email
I was able to see the content of the email with the following command:
openssl smime -verify -noverify -in email-with-smime-p7m.eml
Then you can feed the output back to the library.
Above the -noverify
might need to be omitted if you want to actually verify the email.
thanks - I'll try to implement
I think it should be possible to extract the content using openssl_pcks7_verify()
, as described here:
https://www.php.net/manual/en/function.openssl-pkcs7-verify.php#113835
Edit: Here's my implementation:
<?php
function parse_pkcs7(string $header_and_body): ?string
{
$prefix = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "pkcs7_" . microtime(true);
$msg = "$prefix.eml";
$pem = "$prefix.pem";
$out = "$prefix.out";
try {
file_put_contents($msg, $header_and_body);
if (!openssl_pkcs7_verify($msg, PKCS7_NOVERIFY, $pem)) {
return null;
}
if (!openssl_pkcs7_verify($msg, PKCS7_NOVERIFY, $pem, [], $pem, $out)) {
return null;
}
$body = file_get_contents($out);
if (empty($body)) {
// S/MIME mail with empty content
return null;
}
return $body;
} finally {
foreach ([$msg, $pem, $out] as $file) {
if (file_exists($file)) {
unlink($file);
}
}
}
}
Great @caugner I reopened the issue to integrate it in php-mime-mail-parser
Note: In PHP 8.0 it should be possible to reduce the snippet to a single openssl_pkcs7_verify
call thanks to Named Arguments:
openssl_pkcs7_verify(filename: $msg, flags: PKCS7_NOVERIFY, content: $out)
@eXorus can we expect that a fix will be added. if not please announce it :)
Hi @MrBalum,
This isn't actually a bug fix, but rather a feature request to add support for parsing emails with openssl_pkcs7_verify
. Since this is an open-source project, I do my best to maintain it, but I welcome any PRs that would like to add this functionality to the code.
At the moment, I can't commit to a specific delivery date as I'm currently working on other projects.
Thanks for your understanding!
Here is my email. Parser don't see any contents only one attachment
smime.p7m
.Here is header
Is there a way to parse this email?
In widows' mail I can see email contents correctly.