Sample code for 30+ languages & platforms
PHP

Get the Number of IMAP Attachments

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailNumAttach method, which returns the number of ordinary attachments on a message. The only argument is the Email. This example fetches only the headers and reports the attachment count.

Background: A header-only fetch downloads no attachment bodies, so the Email object's own downloaded-attachment count can be 0. GetMailNumAttach instead reads the ckx-imap-numAttach metadata, giving the true count — which is what you want when building a message list that shows whether each message has attachments.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\Email;
use Chilkat2\Imap;

// Demonstrates the Imap.GetMailNumAttach method, which returns the number of ordinary
// attachments on a message.  The only argument is the Email.

$imap = new Imap();

$imap->Ssl = true;
$imap->Port = 993;

try {
    $imap->Connect('imap.example.com');

    $imap->Login('user@example.com', 'myPassword');

    $imap->SelectMailbox('Inbox');

    // Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
    // metadata describing the attachments is present, so the attachment info methods still work.
    $headersOnly = true;
    $useUid = false;
    $seqNum = 1;
    $email = new Email();
    $imap->FetchEmail($headersOnly, $seqNum, $useUid, $email);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$numAttach = $imap->GetMailNumAttach($email);
echo 'This message has ', $numAttach, " attachment(s).\n";

try {
    $imap->Disconnect();
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}