Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

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

local imap = chilkat.Imap.new()

-- Connect using TLS.
imap.ssl = true
imap.port = 993
local success = imap:connect("imap.example.com")
if not success then
    print(imap.last_error_text)
    return
end

-- Authenticate
success = imap:login("email_account_login", "email_account_password")
if not success then
    print(imap.last_error_text)
    return
end

-- Select an IMAP mailbox
success = imap:select_mailbox("Inbox/postacert")
if not success then
    print(imap.last_error_text)
    return
end

-- Search for messages having "POSTA CERTIFICATA: Invio File" in the subject.
local fetchUids = true
local messageSet = chilkat.MessageSet.new()
success = imap:query_mbx("SUBJECT \"POSTA CERTIFICATA: Invio File\"", fetchUids, messageSet)
if not success then
    print(imap.last_error_text)
    return
end

if messageSet.count < 1 then
    print("No messages found.")
    return
end

-- For our example, we'll process only the 1st email in the messageSet.
-- Download the MIME source of the email into a StringBuilder object.
local email = chilkat.Email.new()
success = imap:fetch_email(false, messageSet:get_id(0), messageSet.has_uids, email)
if not success then
    print(imap.last_error_text)
    return
end

-- The email should contain an attached email.
-- The Invoice XML is contained within the attached email.
local attachedEmail = chilkat.Email.new()
success = email:get_attached_email(0, attachedEmail)
if not success then
    print(email.last_error_text)
    return
end

-- Get the MIME of the attached email..
local sbMime = chilkat.StringBuilder.new()
success = attachedEmail:get_mime_sb(sbMime)

-- Load it into a Chilkat MIME object.
local mime = chilkat.Mime.new()
success = mime:load_mime_sb(sbMime)

-- Examine the structure of the MIME.
print(mime:get_structure("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
local bdP7m = chilkat.BinData.new()
-- Get the 2nd sub-part, at index 1 because index 0 is the 1st sub-part.

local mimeP7m = chilkat.Mime.new()
success = mime:part_at(1, mimeP7m)
if not success then
    print(mime.last_error_text)
    return
end

if not mime.last_method_success then
    print("Failed to get 2nd sub-part. Perhaps the MIME does not have a 2nd sub-part?")
    return
end

success = mimeP7m:get_body_bd(bdP7m)

-- Verify the signature, which also extracts the content contained within the opaque signature.
local crypt = chilkat.Crypt2.new()
success = crypt:opaque_verify_bd(bdP7m)
if not success then
    print(crypt.last_error_text)
    return
end

-- The bdP7m now contains the Invoice XML.
-- Load it into an XML object.
local xml = chilkat.Xml.new()
success = xml:load_bd(bdP7m, true)

print(xml:get_xml())

-- 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

print("success.")