Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

POP3 Verify Signed (S/MIME) Email

Demonstrates how to download and verify digitally signed S/MIME email.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\Cert;
use Chilkat2\Email;
use Chilkat2\MailMan;
use Chilkat2\StringTable;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$mailman = new MailMan();

// Set the POP3 server's hostname
$mailman->MailHost = 'pop.example.com';

// Set the POP3 login/password.
$mailman->PopUsername = 'myLogin';
$mailman->PopPassword = 'myPassword';

$stUidls = new StringTable();
try {
    $mailman->FetchUidls($stUidls);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$email = new Email();
$cert = new Cert();

$count = $stUidls->Count;
$i = 0;
while ($i < $count) {
    // Download the full email.
    try {
        $mailman->FetchByUidl($stUidls->StringAt($i), false, 0, $email);
    } catch (\Chilkat2\Exception $e) {
        echo $e->getMessage(), "\n";
        exit;
    }

    echo $i, "\n";
    echo 'From: ', $email->From, "\n";
    echo 'Subject: ', $email->Subject, "\n";

    // The security layers of signed and/or encrypted emails
    // are automatically "unwrapped" when loaded into
    // a Chilkat email object.
    // An application only needs to check to see if an email
    // was received signed or encrypted, and then examine
    // the success/failure.  For example:
    if ($email->ReceivedSigned) {
        echo "This email was signed.\n";

        // Check to see if the signatures were verified.
        if ($email->SignaturesValid) {
            echo "Digital signature(s) verified.\n";
            echo 'Signer: ', $email->SignedBy, "\n";

            try {
                $email->LastSignerCert(0, $cert);
            } catch (\Chilkat2\Exception $e) {
                echo $e->getMessage(), "\n";
                exit;
            }

            echo 'Signing cert: ', $cert->SubjectCN, "\n";
        }
    } else {
        echo "Digital signature verification failed.\n";
    }

    $i = $i + 1;
}

$mailman->Pop3EndSession();