paragonie / gpg-mailer
Encrypt outbound emails with Crypt_GPG
Installs: 18 279
Dependents: 2
Suggesters: 0
Security: 0
Stars: 96
Watchers: 12
Forks: 18
Open Issues: 3
Requires
- php: ^7.2|^8
- laminas/laminas-mail: ^2.8
- pear/crypt_gpg: ^1.6
Requires (Dev)
- phpunit/phpunit: ^8
- vimeo/psalm: ^3
README
Send GPG-encrypted emails (using zend-mail and Crypt_GPG).
License: MIT
The GPG-Mailer API in a Nutshell
/** * Import a public key, return the fingerprint * * @param string $gpgKey An ASCII armored public key * @return string The GPG fingerprint for this key */ public function import(string $gpgKey): string; /** * Get the public key corresponding to a fingerprint. * * @param string $fingerprint * @return string */ public function export(string $fingerprint): string; /** * Encrypt then email a message * * @param Message $message The message data * @param string $fingerprint Which public key fingerprint to use */ public function send(Message $message, string $fingerprint); /** * Email a message without encrypting it. * * @param Message $message The message data * @param bool $force Send even if we don't have a private key? */ public function sendUnencrypted(Message $message, bool $force = false);
Example: Encrypt Outbound Emails with Your GnuPG Public Key
<?php use \ParagonIE\GPGMailer\GPGMailer; use \Zend\Mail\Message; use \Zend\Mail\Transport\Sendmail; // First, create a Zend\Mail message as usual: $message = new Message; $message->addTo('test@example.com', 'Test Email'); $message->setBody('Cleartext for now. Do not worry; this gets encrypted.'); // Instantiate GPGMailer: $gpgMailer = new GPGMailer( new Sendmail(), ['homedir' => '/homedir/containing/keyring'] ); // GPG public key for <security@paragonie.com> (fingerprint): $fingerprint = '7F52D5C61D1255C731362E826B97A1C2826404DA'; // Finally: $gpgMailer->send($message, $fingerprint);
If you're encrypting with a user provided public key (and they didn't tell you their fingerprint), do this instead:
<?php /** * Output from: gpg --armor --export user@example.com * * This is our security team's GPG public key. You probably don't * want to use ours, as only we can decrypt the messages. * * @var string */ $ASCIIArmoredPublicKey = "-----BEGIN PGP PUBLIC KEY BLOCK----- Version: SKS 1.1.5 mQENBFUgwRUBCADcIpqNwyYc5UmY/tpx1sF/rQ3knR1YNXYZThzFV+Gmqhp1fDH5q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Wp0saaESkGXIXqurj6gZoujJvSvzl0n9F9 VwqMEizDUfrXgtD1siQGhP0sVC6qha+F/SAEJ0jEquM4TfKWWU2S5V5vgPPpIQSYRnhQW4b1 =Z4m0 -----END PGP PUBLIC KEY BLOCK-----"; // Then to import. $fingerprint = $gpgMailer->import($ASCIIArmoredPublicKey);
Sign Emails with the Server's Private Key
Signed and Encrypted
To add signing, we pass the signing key to the third argument of the GPGMailer constructor.
<?php use \ParagonIE\GPGMailer\GPGMailer; use \Zend\Mail\Message; use \Zend\Mail\Transport\Sendmail; // First, create a Zend\Mail message as usual: $message = new Message; $message->addTo('test@example.com', 'Test Email'); $message->setBody('Cleartext for now. Do not worry; this gets encrypted.'); $signingKey = file_get_contents('tests/private.key'); // Instantiate GPGMailer: $gpgMailer = new GPGMailer( new Sendmail(), ['homedir' => '/homedir/containing/keyring'], $signingKey ); // GPG public key for <security@paragonie.com> (fingerprint): $fingerprint = '7F52D5C61D1255C731362E826B97A1C2826404DA'; // Finally: $gpgMailer->send($message, $fingerprint);
Alternatively, we could define our constructor as above but then use
setPrivateKey()
like so:
$gpgMailer = new GPGMailer( new Sendmail(), ['homedir' => '/homedir/containing/keyring'] ); $signingKey = file_get_contents('tests/private.key'); $gpgMailer->setPrivateKey($signingKey);
Signed, But Not Encrypted
Same as above, except we don't need to load the recipient's fingerprint
and we use the sendUnencrypted()
method instead.
<?php use \ParagonIE\GPGMailer\GPGMailer; use \Zend\Mail\Message; use \Zend\Mail\Transport\Sendmail; // First, create a Zend\Mail message as usual: $message = new Message; $message->addTo('test@example.com', 'Test Email'); $message->setBody('Cleartext for now. Do not worry; this gets encrypted.'); $signingKey = file_get_contents('tests/private.key'); $gpgMailer = new GPGMailer( new Sendmail(), ['homedir' => '/homedir/containing/keyring'], $signingKey ); $gpgMailer->sendUnencrypted($message);
Support Contracts
If your company uses this library in their products or services, you may be interested in purchasing a support contract from Paragon Initiative Enterprises.