Lua Requires Chilkat v11.0.0+
Lua
Sorting Email
Demonstrates how to sort an email bundle.Chilkat Lua Downloads
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 to the IMAP server.
-- Use 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
-- Login
success = imap:login("myLogin", "myPassword")
if not success then
print(imap.last_error_text)
return
end
-- Select an IMAP mailbox
success = imap:select_mailbox("Inbox")
if not success then
print(imap.last_error_text)
return
end
local fetchUids = true
local messageSet = chilkat.MessageSet.new()
success = imap:query_mbx("ALL", fetchUids, messageSet)
if not success then
print(imap.last_error_text)
return
end
-- Fetch the email headers into a bundle object:
local bundle = chilkat.EmailBundle.new()
local headersOnly = true
success = imap:fetch_msg_set(headersOnly, messageSet, bundle)
if not success then
print(imap.last_error_text)
return
end
-- Sort the email bundle by date, recipient, sender, or subject:
local ascending = true
bundle:sort_by_date(ascending)
-- To sort by recipient, sender, or subject, call
-- SortBySender, SortByRecipient, or SortBySubject.
-- Display the Subject and From of each email.
local email = chilkat.Email.new()
local i = 0
while i < bundle.message_count do
bundle:email_at(i, email)
print(email:get_header_field("Date"))
print(email.subject)
print(email.from)
print("--")
i = i + 1
end
-- Disconnect from the IMAP server.
success = imap:disconnect()