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

Preview POP3 messages and download a complete email

See more Email Object Examples

Read a short preview for each of up to five messages into an EmailBundle, print their subjects, and download the complete first entry into a separate Email. Configure a POP3 account that supports UIDL and TOP. All messages stay on the server.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Email;
use Chilkat2\EmailBundle;
use Chilkat2\MailMan;

// Assumes Chilkat is unlocked. See Global Unlock Sample.

$mail = new MailMan();
$mail->MailHost = 'pop.example.com';
$mail->MailPort = 995;
$mail->PopSsl = true;
$mail->RequireSslCertVerify = true;
$mail->PopUsername = 'MAIL_USER';
$mail->PopPassword = 'MAIL_PASSWORD';
$previews = new EmailBundle();
// Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
try {
    $mail->FetchRange(true, true, 0, 0, 4, $previews);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

if ($previews->MessageCount == 0) {
    echo "There are no messages to preview.\n";
    try {
        $mail->Pop3EndSession();
    } catch (\Chilkat2\Exception $e) {
        echo $e->getMessage(), "\n";
        exit;
    }

    exit;
}

$header = new Email();
$i = 0;
while ($i < $previews->MessageCount) {
    try {
        $previews->EmailAt($i, $header);
    } catch (\Chilkat2\Exception $e) {
        echo $e->getMessage(), "\n";
        exit;
    }

    echo $header->Uidl, '  ', $header->Subject, "\n";
    $i = $i + 1;
}

// A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
try {
    $previews->EmailAt(0, $header);

    $email = new Email();
    $mail->FetchFull($header, $email);

    $mail->Pop3EndSession();
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo 'Full message: ', $email->Subject, '  Files: ', $email->NumAttachments, "\n";

try {
    $email->SaveEml('pop-received.eml');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo "Saved pop-received.eml.\n";