PHP ActiveX Requires Chilkat v11.0.0+
PHP ActiveX
Find an email by subject and fetch it by IMAP UID
See more Email Object Examples
Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.
Chilkat PHP ActiveX Downloads
<?php
$success = 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$imap = new COM("Chilkat.Imap");
$imap->Ssl = 1;
$imap->Port = 993;
$imap->RequireSslCertVerify = 1;
$imap->PeekMode = 1;
$imap->AutoDownloadAttachments = 1;
$success = $imap->Connect('imap.example.com');
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
$success = $imap->Login('MAIL_USER','MAIL_PASSWORD');
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
$success = $imap->SelectMailbox('INBOX');
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
$matches = new COM("Chilkat.MessageSet");
$success = $imap->QueryMbx('SUBJECT \'Sample message\'',1,$matches);
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
if ($matches->Count == 0) {
print 'No matching message in INBOX.' . "\n";
$success = $imap->Disconnect();
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
exit;
}
// The search returned UIDs. Use the same identifier type for the fetch.
$uid = $matches->GetId(0);
$email = new COM("Chilkat.Email");
$success = $imap->FetchEmail(0,$uid,1,$email);
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
print 'Mailbox INBOX, UIDVALIDITY ' . $imap->UidValidity . ', UID ' . $uid . "\n";
$success = $imap->Disconnect();
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
// The downloaded Email remains usable after disconnecting.
print 'Subject: ' . $email->Subject . ' Files: ' . $email->NumAttachments . "\n";
$success = $email->SaveEml('imap-received.eml');
if ($success != 1) {
print $email->LastErrorText . "\n";
exit;
}
print 'Saved imap-received.eml.' . "\n";
?>