PHP code example of zbateson / mmp-crypt-smime

1. Go to this page and download the library: Download zbateson/mmp-crypt-smime library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zbateson / mmp-crypt-smime example snippets


use ZBateson\MailMimeParser\Message;
use ZBateson\MmpCryptSmime\SMimeMessage;

$message = Message::from($rawEmail, false);
$cert = file_get_contents('/path/to/recipient-cert.pem');

// Encrypt -- returns a new IMimePart with application/pkcs7-mime content
$encrypted = SMimeMessage::encrypt($message, $cert);

// The outer Subject defaults to "..." to hide the real subject
echo (string) $encrypted;

$cert = file_get_contents('/path/to/signer-cert.pem');
$key = file_get_contents('/path/to/signer-key.pem');

$signed = SMimeMessage::sign($message, $cert, $key);

$result = SMimeMessage::signAndEncrypt(
    $message,
    $signerCert,
    $signerKey,
    $recipientCert,
);

use ZBateson\MmpCryptSmime\SMimeOpenSsl;

// Configure the decryption credentials
$crypt = new SMimeOpenSsl(
    decryptionCredentials: [
        ['cert' => $cert, 'key' => $key],
    ],
);

// Decrypt the content stream from a parsed encrypted part
$decrypted = $crypt->decrypt($encryptedPart->getContentStream());