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

IMAP Read PEC Email of Aruba and Extract the Invoice XML Files

See more Email Object Examples

IMAP Read PEC Email of Aruba and Extract the Invoice XML Files.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const imap = try chilkat.Imap.init();
    defer imap.deinit();

    // Connect using TLS.
    imap.setSsl(true);
    imap.setPort(993);
    imap.connect("imap.example.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Authenticate
    imap.login("email_account_login", "email_account_password") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Select an IMAP mailbox
    imap.selectMailbox("Inbox/postacert") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Search for messages having "POSTA CERTIFICATA: Invio File" in the subject.
    const fetch_uids = true;
    const message_set = try chilkat.MessageSet.init();
    defer message_set.deinit();
    imap.queryMbx("SUBJECT \"POSTA CERTIFICATA: Invio File\"", fetch_uids, message_set) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    if (message_set.getCount() < 1) {
        std.debug.print("No messages found.\n", .{});
        return;
    }

    // For our example, we'll process only the 1st email in the messageSet.
    // Download the MIME source of the email into a StringBuilder object.
    const email = try chilkat.Email.init();
    defer email.deinit();
    imap.fetchEmail(false, message_set.getId(0), message_set.getHasUids(), email) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // The email should contain an attached email.
    // The Invoice XML is contained within the attached email.
    const attached_email = try chilkat.Email.init();
    defer attached_email.deinit();
    email.getAttachedEmail(0, attached_email) catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    // Get the MIME of the attached email..
    const sb_mime = try chilkat.StringBuilder.init();
    defer sb_mime.deinit();
    try attached_email.getMimeSb(sb_mime);

    // Load it into a Chilkat MIME object.
    const mime = try chilkat.Mime.init();
    defer mime.deinit();
    try mime.loadMimeSb(sb_mime);

    // Examine the structure of the MIME.
    std.debug.print("{s}\n", .{try mime.getStructure(alloc, "text")});

    // The MIME has this structure:
    // multipart/mixed
    //     application/octet-stream
    //     application/octet-stream     <-- This is where the XML is contained.   It is within an opaque signature.
    //     text/plain

    // The 2nd application/octet-stream MIME header looks like this:

    // ------=_Part_329673_-1348225228.1579889273592
    // Content-Type: application/octet-stream; name="SM99999_99aaa.xml.p7m"
    // Content-Transfer-Encoding: base64
    // Content-Disposition: attachment; filename="SM99999_99aaa.xml.p7m"

    // Let's get the binary content of the .p7m
    const bd_p7m = try chilkat.BinData.init();
    defer bd_p7m.deinit();
    // Get the 2nd sub-part, at index 1 because index 0 is the 1st sub-part.

    const mime_p7m = try chilkat.Mime.init();
    defer mime_p7m.deinit();
    mime.partAt(1, mime_p7m) catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    if (!mime.getLastMethodSuccess()) {
        std.debug.print("Failed to get 2nd sub-part. Perhaps the MIME does not have a 2nd sub-part?\n", .{});
        return;
    }

    try mime_p7m.getBodyBd(bd_p7m);

    // Verify the signature, which also extracts the content contained within the opaque signature.
    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    crypt.opaqueVerifyBd(bd_p7m) catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    // The bdP7m now contains the Invoice XML.
    // Load it into an XML object.
    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    try xml.loadBd(bd_p7m, true);

    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // We have XML such as the following:
    //
    // <?xml version="1.0" encoding="windows-1252"?>
    // <?xml-stylesheet type="text/xsl" href="fatturapa_v1.2.xsl"?>
    // <p:FatturaElettronica versione="FPR12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
    //     xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2"
    //     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    //     <FatturaElettronicaHeader>
    //     ...
    //     </FatturaElettronicaHeader>
    //     <FatturaElettronicaBody>
    //     ...
    //     </FatturaElettronicaBody>
    // </p:FatturaElettronica>

    // Use this online tool to generate parsing code from sample XML:
    // Generate Parsing Code from XML

    std.debug.print("success.\n", .{});
}