PHP code example of vpsbg / laravel-pgp-mailer

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

    

vpsbg / laravel-pgp-mailer example snippets


use Vpsbg\PgpMailer\Models\PgpKey;

PgpKey::store('[email protected]', file_get_contents('alice.pub.asc'));

Mail::to('[email protected]')->send(new InvoiceMail($invoice));
// arrives as multipart/encrypted; only Alice's PGP client can read it

use Vpsbg\PgpMailer\Models\PgpKey;

PgpKey::store('[email protected]', $armoredPublicKey);  // upsert + lifecycle event
PgpKey::forEmail('[email protected]')->first();         // lookup
PgpKey::purgeEmail('[email protected]');                // delete + PgpKeyRemoved
PgpKey::transferEmail('[email protected]', '[email protected]');               // migrate
PgpKey::transferEmail('[email protected]', '[email protected]', flagMismatch: true);

use Vpsbg\PgpMailer\Rules\ValidPgpKey;

$request->validate([
    'pgp_key' => ['

$request->validate([
    'email'   => [' (new ValidPgpKey)->forEmail($request->input('email'))],
]);

use Vpsbg\PgpMailer\Models\PgpKey;

class User extends Authenticatable
{
    public function pgpKey()        { return $this->hasOne(PgpKey::class, 'email', 'email'); }
    public function invoicePgpKey() { return $this->hasOne(PgpKey::class, 'email', 'invoice_email'); }
}

User::updating(function (User $u) {
    if ($u->isDirty('email')) {
        PgpKey::transferEmail($u->getOriginal('email'), $u->email, flagMismatch: true);
    }
});

User::deleting(fn (User $u) => PgpKey::purgeEmail($u->email));

namespace App\Models;

class TenantPgpKey extends \Vpsbg\PgpMailer\Models\PgpKey
{
    public function tenant() { return $this->belongsTo(Tenant::class); }
}

// config/pgp-mailer.php
'model' => App\Models\TenantPgpKey::class,

use Illuminate\Mail\Mailables\Headers;

public function headers(): Headers
{
    return new Headers(text: ['X-Pgp-Mailer-Disable' => '1']);
    // or: ['X-Pgp-Mailer-No-Encrypt' => '1']
}

use Vpsbg\PgpMailer\PgpMailer;

Mail::to($user)->send(PgpMailer::skip(new ThirdPartyNotification($data)));
Mail::to($user)->send(PgpMailer::unencrypted(new MonthlyDigestMail($data)));

// config/pgp-mailer.php
'signing' => [
    'enabled' => env('PGP_MAIL_SIGN', true),
    'key' => ($b64 = env('PGP_MAIL_SIGNING_KEY_B64'))
        ? base64_decode($b64, true)
        : env('PGP_MAIL_SIGNING_KEY'),
    'passphrase' => env('PGP_MAIL_SIGNING_KEY_PASSPHRASE'),
    // ...
],

'signing' => [
    'enabled' => env('PGP_MAIL_SIGN', true),

    // Default key. Used when no per-sender entry below matches. Leave
    // these unset if every sender you actually use has its own entry.
    'key_path' => env('PGP_MAIL_SIGNING_KEY_PATH'),
    'key' => env('PGP_MAIL_SIGNING_KEY'),
    'passphrase' => env('PGP_MAIL_SIGNING_KEY_PASSPHRASE'),

    // Map: From address (case-insensitive) to signing key.
    'senders' => [
        '[email protected]' => [
            'key_path' => env('PGP_MAIL_SIGNING_KEY_SUPPORT_PATH'),
            'passphrase' => env('PGP_MAIL_SIGNING_KEY_SUPPORT_PASSPHRASE'),
        ],
        '[email protected]' => [
            'key_path' => env('PGP_MAIL_SIGNING_KEY_BILLING_PATH'),
            'passphrase' => env('PGP_MAIL_SIGNING_KEY_BILLING_PASSPHRASE'),
        ],
    ],

    'unmatched_sender_policy' => env('PGP_MAIL_UNMATCHED_SENDER_POLICY', 'use_default'),
],

use Vpsbg\PgpMailer\PgpMailer;

Mail::to($user)->send(PgpMailer::withVisibleSubject(new PasswordResetMail($token)));
bash
php artisan vendor:publish --provider="Vpsbg\PgpMailer\PgpMailerServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Vpsbg\PgpMailer\PgpMailerServiceProvider" --tag="migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="Vpsbg\PgpMailer\PgpMailerServiceProvider" --tag="translations"