Lua
Lua
Get IMAP Attachment Filenames
See more IMAP Examples
Demonstrates the Chilkat Imap.GetMailAttachFilename method, which returns the filename of an attachment. The first argument is the Email and the second is the zero-based attachment index. This example fetches only the message headers and lists the attachment filenames.
Background: This method reads the filename from
ckx-imap-* metadata, so it works on a header-only email even though the attachment bodies have not been downloaded. That is exactly what a mail client needs to render the paperclip list — the names of the attached files — before the user decides to download any of them.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Demonstrates the Imap.GetMailAttachFilename method, which returns the filename of an
-- attachment. The 1st argument is the Email and the 2nd is the zero-based attachment index.
local imap = chilkat.Imap.new()
imap.ssl = true
imap.port = 993
local success = imap:connect("imap.example.com")
if not success then
print(imap.last_error_text)
return
end
success = imap:login("user@example.com", "myPassword")
if not success then
print(imap.last_error_text)
return
end
success = imap:select_mailbox("Inbox")
if not success then
print(imap.last_error_text)
return
end
-- Fetch only the message headers. Attachment bodies are NOT downloaded, but the ckx-imap-*
-- metadata describing the attachments is present, so the attachment info methods still work.
local headersOnly = true
local useUid = false
local seqNum = 1
local email = chilkat.Email.new()
success = imap:fetch_email(headersOnly, seqNum, useUid, email)
if not success then
print(imap.last_error_text)
return
end
-- Loop over the attachments and print each filename.
local numAttach = imap:get_mail_num_attach(email)
print("Attachments: " .. numAttach)
for i = 0, numAttach - 1 do
-- GetMailAttachFilename returns a string, so assign it to a string variable.
local fname = imap:get_mail_attach_filename(email, i)
if not imap.last_method_success then
print(imap.last_error_text)
return
end
print(fname)
end
success = imap:disconnect()
if not success then
print(imap.last_error_text)
return
end