zbateson/mmp-crypt-gpg
February 13, 2026 ยท View on GitHub
PGP/MIME encryption, decryption, signing, and verification for zbateson/mail-mime-parser using PEAR's Crypt_GPG.
composer require zbateson/mmp-crypt-gpg
Sponsors
A huge thank you to all my sponsors. <3
If this project's helped you, please consider sponsoring me.
Requirements
PHP 8.1 or newer with the gpg binary available on the system, and zbateson/mail-mime-parser 4.x. A configured GnuPG keyring with the appropriate keys imported is also required โ see the GnuPG documentation for details on setting up your keyring.
Description
This package provides a PGP/MIME implementation of zbateson/mmp-crypt using PEAR's Crypt_GPG. It supports:
- Encrypting and decrypting
multipart/encrypted; protocol="application/pgp-encrypted"messages - Signing and verifying detached
application/pgp-signaturesignatures - Composing PGP/MIME messages via the
PgpMimeMessagestatic API (encrypt, sign, sign-then-encrypt)
All operations use file-based I/O via temp files, so large messages are handled without loading them entirely into memory.
Usage
Composing an encrypted message
use ZBateson\MailMimeParser\Message;
use ZBateson\MmpCryptGpg\GpgPear;
use ZBateson\MmpCryptGpg\PgpMimeMessage;
$message = Message::from($rawEmail, false);
$cryptGpg = new \Crypt_GPG();
$cryptGpg->addEncryptKey('recipient@example.com');
$crypt = new GpgPear($cryptGpg);
// Encrypt -- returns a new IMimePart with multipart/encrypted structure
$encrypted = PgpMimeMessage::encrypt($message, $crypt);
// The outer Subject defaults to "..." to hide the real subject
echo (string) $encrypted;
Composing a signed message
$cryptGpg = new \Crypt_GPG();
$cryptGpg->addSignKey('sender@example.com');
$crypt = new GpgPear($cryptGpg);
$signed = PgpMimeMessage::sign($message, $crypt);
Sign-then-encrypt
$cryptGpg = new \Crypt_GPG();
$cryptGpg->addSignKey('sender@example.com');
$cryptGpg->addEncryptKey('recipient@example.com');
$crypt = new GpgPear($cryptGpg);
$result = PgpMimeMessage::signAndEncrypt($message, $crypt);
Decrypting a parsed message
use ZBateson\MmpCryptGpg\GpgPear;
$cryptGpg = new \Crypt_GPG();
// Keys must be in the GnuPG keyring for decryption
$crypt = new GpgPear($cryptGpg);
$decrypted = $crypt->decrypt($encryptedPart->getContentStream());
License
BSD licensed - please see license agreement.
