Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
# 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.
set imap [new_CkImap]
CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
exit
}
set success [CkImap_Login $imap "user@example.com" "myPassword"]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
exit
}
set success [CkImap_SelectMailbox $imap "Inbox"]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
exit
}
# 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.
set headersOnly 1
set useUid 0
set seqNum 1
set email [new_CkEmail]
set success [CkImap_FetchEmail $imap $headersOnly $seqNum $useUid $email]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkEmail $email
exit
}
set numAttach [CkImap_GetMailNumAttach $imap $email]
for {set i 0} {$i <= [expr $numAttach - 1]} {incr i} {
set fname [CkImap_getMailAttachFilename $imap $email $i]
if {[CkImap_get_LastMethodSuccess $imap] == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkEmail $email
exit
}
set sz [CkImap_GetMailAttachSize $imap $email $i]
puts "$fname: $sz bytes"
}
set success [CkImap_Disconnect $imap]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkEmail $email
exit
}
delete_CkImap $imap
delete_CkEmail $email