Tcl
Tcl
Process New Email by Scanning for Senders
Scan email and save application-selected emails to EML files with unique filenames.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# This example assumes 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
# Fetch messages from the mailbox using a search criteria.
# This example finds NEW emails: these are emails that have the RECENT flag set, but not the SEEN flag:
set messageSet [new_CkMessageSet]
set success [CkImap_QueryMbx $imap "NEW" $fetchUids $messageSet]
if {$success == 0} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkMessageSet $messageSet
exit
}
# This example will download headers, and then download
# the full email for those emails sent from a contact
# in our database.
# When downloading headers, each email object contains
# (obviously) the headers, but the body will be missing.
# Also, attachments will not be included. However, it is
# possible to get information about the attachments
# as well as the complete size of the email.
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
}
# Loop over the email objects...
set emailHeader [new_CkEmail]
set fullEmail [new_CkEmail]
set i 0
set numEmails [CkEmailBundle_get_MessageCount $bundle]
while {$i < $numEmails} {
CkEmailBundle_EmailAt $bundle $i $emailHeader
# The sender's email address and name are available
# in the From, FromAddress, and FromName properties.
# If the sender is "Chilkat Support <support@chilkatsoft.com",
# then the From property will hold the entire string.
# the FromName property contains"Chilkat Support",
# and the FromAddress property contains "support@chilkatsoft.com"
puts [CkEmail_from $emailHeader]
puts [CkEmail_fromAddress $emailHeader]
puts [CkEmail_fromName $emailHeader]
# Assume at this point your code checks to see if the sender
# is one in your contacts database. If so, this is
# the code you would write to download the entire
# email and save it to a file.
# 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 $emailHeader
delete_CkEmail $fullEmail
exit
}
# You can use the GenerateFilename method to
# generate a unique filename...
set filename [CkEmail_generateFilename $fullEmail]
# SaveEml saves the entire email, including attachments.
set success [CkEmail_SaveEml $fullEmail $filename]
puts "--"
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 $emailHeader
delete_CkEmail $fullEmail