PHP code example of cracksalad / phpmailer-pgp

1. Go to this page and download the library: Download cracksalad/phpmailer-pgp 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/ */

    

cracksalad / phpmailer-pgp example snippets



PHPMailer\PHPMailer\PHPMailerPGP;

$mailer = new PHPMailerPGP();

//$mailer->SMTPDebug = 3;                               // Enable verbose debug output

$mailer->isSMTP();                                      // Set mailer to use SMTP
$mailer->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mailer->SMTPAuth = true;                               // Enable SMTP authentication
$mailer->Username = '[email protected]';                 // SMTP username
$mailer->Password = 'secret';                           // SMTP password
$mailer->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mailer->Port = 587;                                    // TCP port to connect to

$mailer->setFrom('[email protected]', 'Mailer');
$mailer->addAddress('[email protected]', 'Joe User');     // Add a recipient
$mailer->addAddress('[email protected]');               // Name is optional
$mailer->addReplyTo('[email protected]', 'Information');
$mailer->addCC('[email protected]');
$mailer->addBCC('[email protected]');

$mailer->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mailer->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mailer->isHTML(true);                                  // Set email format to HTML

$mailer->Subject = 'Here is the subject';
$mailer->Body    = 'This is the HTML message body <b>in bold!</b>';
$mailer->AltBody = 'This is the body in plain text for non-HTML mail clients';

// Optionally specify a file that contains the keys you want to use.
// Not necessary if the key was already imported into gnupg previously (or manually).
$mailer->importKeyFile('/path/to/my-gpg-keyring.asc');

// Optionally check if there is an encryption key for the given recipient(s).
// People not knowing about OpenPGP might be confused by OpenPGP signed mails, 
// so putting `pgpSign()` in an if-statement might be a good idea.
if (count($mailer->getKeys('[email protected]', 'encrypt')) === 1) {

    // Turn on encryption for your email
    $mailer->encrypt(true);
    
    // Turn on signing for your email
    $mailer->pgpSign(true);
}

// Turn on protected headers for your email (not supported by all OpenPGP supporting clients)
$mailer->protectHeaders(true);

// Send!
if (!$mailer->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mailer->ErrorInfo;
} else {
    echo 'Message has been sent';
}

$mailer = new PHPMailerPGP();
$errCode = 0;
$key = $mailer->lookupKeyServer('[email protected]', 'keys.openpgp.org', $errCode);
if ($errCode === PHPMailerPGP::LOOKUP_ERR_OK) {
    $mailer->importKey($key);
} // else: not found or error occurred

// now you can send encrypted e-mails to [email protected]