PHP ActiveX Requires Chilkat v11.0.0+
PHP ActiveX
Extract signed invoice XML from an Italian PEC message
See more Email Object Examples
Search the PEC folder by subject, fetch one envelope by UID, and inspect the attachments of each nested email. Match .xml.p7m by filename and verify the opaque CMS signature before loading the XML. This checks signature integrity. Certificate trust and invoice schema validation are separate application decisions.
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('IMAP_USERNAME','IMAP_PASSWORD');
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
$success = $imap->SelectMailbox('INBOX/postacert');
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
$messages = new COM("Chilkat.MessageSet");
$success = $imap->QueryMbx('SUBJECT \'POSTA CERTIFICATA: Invio File\'',1,$messages);
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
if ($messages->Count == 0) {
print 'No matching PEC envelope.' . "\n";
$imap->Disconnect();
exit;
}
$envelope = new COM("Chilkat.Email");
$success = $imap->FetchEmail(0,$messages->GetId(0),1,$envelope);
if ($success != 1) {
print $imap->LastErrorText . "\n";
exit;
}
$imap->Disconnect();
$nested = new COM("Chilkat.Email");
$name = new COM("Chilkat.StringBuilder");
$verified = 0;
$i = 0;
while (($i < $envelope->NumAttachedMessages)) {
$success = $envelope->GetAttachedEmail($i,$nested);
if ($success != 1) {
print $envelope->LastErrorText . "\n";
exit;
}
$j = 0;
while (($j < $nested->NumAttachments)) {
$name->SetString($nested->getAttachmentFilename($j));
if ($name->EndsWith('.xml.p7m',0) == 1) {
$data = new COM("Chilkat.BinData");
$success = $nested->GetAttachmentBd($j,$data);
if ($success != 1) {
print $nested->LastErrorText . "\n";
exit;
}
$cms = new COM("Chilkat.Crypt2");
$success = $cms->OpaqueVerifyBd($data);
if ($success != 1) {
print $cms->LastErrorText . "\n";
exit;
}
$invoice = new COM("Chilkat.Xml");
$success = $invoice->LoadBd($data,1);
if ($success != 1) {
print $invoice->LastErrorText . "\n";
exit;
}
print $name->getAsString() . ' XML root: ' . $invoice->Tag . "\n";
$verified = $verified + 1;
}
$j = $j + 1;
}
$i = $i + 1;
}
print 'Verified XML invoices: ' . $verified . "\n";
?>