PHP Classes
elePHPant
Icontem

PHP GPG Email Encrypt: Encrypt, decrypt and sign email messages with GPG

Recommend this page to a friend!
  Info   View files Documentation   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-01-10 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 88 This week: 1All time: 8,631 This week: 797Up
Version Licenses PHP version Categories
gnupg-mailer 1.0.2GNU General Publi...7Email, Cryptography, Security, PHP 7
Description Author

This class can encrypt, decrypt and sign email messages with GPG.

It uses the Zend framework Mail package to encrypt and sign a given message already composed with the Message class and sends with a given transport class object.

The message is encrypted and signed with a given key.

The class can also decrypt and verify signed messages.

  Performance   Level  
Name: Scott Arciszewski <contact>
Classes: 5 packages by
Country: United States United States
Age: ???
All time rank: 2389295 in United States United States
Week rank: 24 Up2 in United States United States Up
Innovation award
Innovation award
Nominee: 3x

Details

GPG-Mailer

Build Status

Send GPG-encrypted emails (using zend-mail and Crypt_GPG).

License: Any or all of the following:

* MIT * GPLv3

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+Gmqhp1fDH5qBs9foh1
xwI6O7knWmQngnf/nBumI3x6xj7PuOdEZUh2FwCG/VWnglW8rKmoHzHAivjiu9SLnPIPAgHS
Heh2XD7q3Ndm3nenbjAiRFNl2iXcwA2cTQp9Mmfw9vVcw0G0z1o0G3s8cC8ZS6flFySIervv
fSRWj7A1acI5eE3+AH/qXJRdEJ+9J8OB65p1JMfk6+fWgOB1XZxMpz70S0rW6IX38WDSRhEK
2fXyZJAJjyt+YGuzjZySNSoQR/V6vNYnsyrNPCJ2i5CgZQxAkyBBcr7koV9RIhPRzct/ABEB
AAG0IVNlY3VyaXR5IDxzZWN1cml0eUBwYXJhZ29uaWUuY29tPokBOQQTAQIAIwUCVSDBFQIb
AwcLCQgHAwIBBhUIAgkKCwQWAgMBAh4BAheAAAoJEGuXocKCZATat2YIAIoejNFEQ2c1iaOE
tSuB7Pn/WLbsDsHNLDKOV+UnfaCjv/vL7D+5NMChFCi2frde/NQb2TsjqmIH+V+XbnJtlrXD
Vj7yvMVal+Jqjwj7v4eOEWcKVcFZk+9cfUgh7t92T2BMX58RpgZF0IQZ6Z1R3FfC9Ub4X6yk
W+te1q0/4CoRycniwmlQi6iGSr99LQ5pfJq2Qlmz/luTZ0UX0h575T7dcp2T1sX/zFRk/f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Xul/UE0jct/5f7KLM
dWMJ1VgniQmmhjvkHLPSICteqCI04RfcmMseW9gueHQXeUu1SNIvsWa2MhxjeBej3pDnrZWs
zKwygF45GO9/v4tkIXNMy5J1AtOyRgQ3IUMqp8EAEQEAAYkBHwQYAQIACQUCVSDBFQIbDAAK
CRBrl6HCgmQE2jnIB/4/xFz8InpM7eybnBOAir3uGcYfs3DOmaKn7qWVtGzvrKpQPYnVtlU2
i6Z5UO4c4jDLT/8Xm1UDz3Lxvqt4xCaDwJvBZexU5BMK8l5DvOzH6o6P2L1UDu6BvmPXpVZz
7/qUhOnyf8VQg/dAtYF4/ax19giNUpI5j5o5mX5w80RxqSXV9NdSL4fdjeG1g/xXv2luhoV5
3T1bsycI3wjk/x5tV+M2KVhZBvvuOm/zhJjeoLWp0saaESkGXIXqurj6gZoujJvSvzl0n9F9
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); 
  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (3 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Accessible without login Plain text file GPGMailer.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file EmailTest.php Test Unit test script
  Accessible without login Plain text file private.key Data Auxiliary data
  Accessible without login Plain text file public.key Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:88
This week:1
All time:8,631
This week:797Up