Sample code for 30+ languages & platforms
Lua

Get IMAP Attachment Sizes

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailAttachSize method, which returns the size in bytes of an attachment. The first argument is the Email and the second is the zero-based attachment index. This example fetches headers only and prints each attachment's name and size.

Background: Like the attachment filename, the size comes from ckx-imap-* metadata and is available from a header-only fetch without downloading the body. A client can therefore show attachment sizes in a message preview, and decide whether to auto-download or prompt before pulling a large file.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Imap.GetMailAttachSize method, which returns the size in bytes 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

local numAttach = imap:get_mail_num_attach(email)
for i = 0, numAttach - 1 do
    local fname = imap:get_mail_attach_filename(email, i)
    if not imap.last_method_success then
        print(imap.last_error_text)
        return
    end

    local sz = imap:get_mail_attach_size(email, i)
    print(fname .. ": " .. sz .. " bytes")
end

success = imap:disconnect()
if not success then
    print(imap.last_error_text)
    return
end