Tcl
Tcl
Scan for Emails with Attachments and Save Attachments to Files
Scan for emails with attachments and save attachments.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set imap [new_CkImap]
# Connect to an IMAP server.
# Use TLS
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
}
# Login
set success [CkImap_Login $imap "myLogin" "myPassword"]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
exit
}
# Select an IMAP mailbox
set success [CkImap_SelectMailbox $imap "Inbox"]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
exit
}
# We can choose to fetch UIDs or sequence numbers.
set fetchUids 1
# Get the message IDs of all the emails in the mailbox
set messageSet [new_CkMessageSet]
set success [CkImap_QueryMbx $imap "ALL" $fetchUids $messageSet]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkMessageSet $messageSet
exit
}
# Fetch the email headers into a bundle object:
set bundle [new_CkEmailBundle]
set headersOnly 1
set success [CkImap_FetchMsgSet $imap $headersOnly $messageSet $bundle]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkMessageSet $messageSet
delete_CkEmailBundle $bundle
exit
}
# Scan for emails with attachments, and save the attachments
# to a sub-directory.
set fullEmail [new_CkEmail]
set emailHeader [new_CkEmail]
set i 0
while {$i < [CkEmailBundle_get_MessageCount $bundle]} {
# The bundle contains email headers..
CkEmailBundle_EmailAt $bundle $i $emailHeader
# Does this email have attachments?
# Use GetMailNumAttach because the attachments
# are not actually in the email object because
# we only downloaded headers.
set numAttach [CkImap_GetMailNumAttach $imap $emailHeader]
if {$numAttach > 0} then {
# Download the entire email and save the
# attachments. (Remember, we
# need to download the entire email because
# only the headers were previously downloaded.
# The ckx-imap-uid header field is added when
# headers are downloaded. This makes it possible
# to get the UID from the email object.
set uidStr [CkEmail_getHeaderField $emailHeader "ckx-imap-uid"]
set uid $uidStr
set success [CkImap_FetchEmail $imap 0 $uid 1 $fullEmail]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkMessageSet $messageSet
delete_CkEmailBundle $bundle
delete_CkEmail $fullEmail
delete_CkEmail $emailHeader
exit
}
set success [CkEmail_SaveAllAttachments $fullEmail "attachmentsDir"]
for {set j 0} {$j <= [expr $numAttach - 1]} {incr j} {
set filename [CkImap_getMailAttachFilename $imap $emailHeader $j]
puts "$filename"
}
}
set i [expr $i + 1]
}
# Disconnect from the IMAP server.
set success [CkImap_Disconnect $imap]
delete_CkImap $imap
delete_CkMessageSet $messageSet
delete_CkEmailBundle $bundle
delete_CkEmail $fullEmail
delete_CkEmail $emailHeader