Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
# 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.
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
}
# Loop over the attachments and print each filename.
set numAttach [CkImap_GetMailNumAttach $imap $email]
puts "Attachments: $numAttach"
for {set i 0} {$i <= [expr $numAttach - 1]} {incr i} {
# GetMailAttachFilename returns a string, so assign it to a string variable.
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
}
puts "$fname"
}
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