Sample code for 30+ languages & platforms
Tcl

Save an IMAP Attachment to a File

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchAttachment method, which downloads one attachment from the IMAP server and saves it to the filesystem. The first argument is the Email, the second is the zero-based attachment index, and the third is the file path to write. This example fetches a message headers-only, inspects its attachments without downloading them, and then downloads a specific attachment on demand.

Note: The output path is relative to the application's current working directory. Chilkat does not create missing parent directories, so qa_output/ must already exist.

Background: This is the method's real value. A headers-only fetch downloads no attachment bodies, yet methods such as GetMailNumAttach and GetMailAttachFilename still work from the ckx-imap-* metadata. FetchAttachment then uses that metadata to download just the requested MIME part from the server — even in a later session. A client can therefore list messages and their attachment names cheaply and pull individual attachments only when the user actually wants them, instead of downloading every full message. (If the Email already holds the attachment bytes, they are saved without contacting the server.)

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Imap.FetchAttachment method, which downloads one attachment from the IMAP
#  server and saves it to a file.  The 1st argument is the Email, the 2nd is the zero-based
#  attachment index, and the 3rd is the file path to write.
#  
#  The compelling use case: fetch a message headers-only, inspect its attachments without
#  downloading them, and then download just the attachment(s) you want.

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
}

#  Even though the body was not downloaded, the attachment count and names are available.
set numAttach [CkImap_GetMailNumAttach $imap $email]
puts "Attachments: $numAttach"

if {$numAttach > 0} then {
    #  Get the attachment's original filename and use it for the saved file.
    set fname [CkImap_getMailAttachFilename $imap $email 0]
    if {[CkImap_get_LastMethodSuccess $imap] == 0} then {
        puts [CkImap_lastErrorText $imap]
        delete_CkImap $imap
        delete_CkEmail $email
        exit
    }

    #  Build the output path "qa_output/<filename>".
    set sbPath [new_CkStringBuilder]

    CkStringBuilder_Append $sbPath "qa_output/"
    CkStringBuilder_Append $sbPath $fname
    set savePath [CkStringBuilder_getAsString $sbPath]

    #  Download just the first attachment (index 0) from the server and save it to that path.
    set success [CkImap_FetchAttachment $imap $email 0 $savePath]
    if {$success == 0} then {
        puts [CkImap_lastErrorText $imap]
        delete_CkImap $imap
        delete_CkEmail $email
        delete_CkStringBuilder $sbPath
        exit
    }

    puts "Saved attachment: $savePath"
}

set success [CkImap_Disconnect $imap]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkEmail $email
    delete_CkStringBuilder $sbPath
    exit
}


delete_CkImap $imap
delete_CkEmail $email
delete_CkStringBuilder $sbPath