PGP/MIME encryption, decryption, signing, and verification for zbateson/mail-mime-parser using PEAR's Crypt_GPG.
composer require zbateson/mmp-crypt-gpg
A huge thank you to all my sponsors. <3
If this project's helped you, please consider sponsoring me.
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.
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.
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;$cryptGpg = new \Crypt_GPG();
$cryptGpg->addSignKey('sender@example.com');
$crypt = new GpgPear($cryptGpg);
$signed = PgpMimeMessage::sign($message, $crypt);$cryptGpg = new \Crypt_GPG();
$cryptGpg->addSignKey('sender@example.com');
$cryptGpg->addEncryptKey('recipient@example.com');
$crypt = new GpgPear($cryptGpg);
$result = PgpMimeMessage::signAndEncrypt($message, $crypt);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());BSD licensed - please see license agreement.
