Tcl Requires Chilkat v11.0.0+
Tcl
Find an email by subject and fetch it by IMAP UID
See more Email Object Examples
Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set imap [new_CkImap]
CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
CkImap_put_RequireSslCertVerify $imap 1
CkImap_put_PeekMode $imap 1
CkImap_put_AutoDownloadAttachments $imap 1
set success [CkImap_Connect $imap "imap.example.com"]
if {$success != 1} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
exit
}
set success [CkImap_Login $imap "MAIL_USER" "MAIL_PASSWORD"]
if {$success != 1} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
exit
}
set success [CkImap_SelectMailbox $imap "INBOX"]
if {$success != 1} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
exit
}
set matches [new_CkMessageSet]
set success [CkImap_QueryMbx $imap "SUBJECT \"Sample message\"" 1 $matches]
if {$success != 1} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkMessageSet $matches
exit
}
if {[CkMessageSet_get_Count $matches] == 0} then {
puts "No matching message in INBOX."
set success [CkImap_Disconnect $imap]
if {$success != 1} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkMessageSet $matches
exit
}
delete_CkImap $imap
delete_CkMessageSet $matches
exit
}
# The search returned UIDs. Use the same identifier type for the fetch.
set uid [CkMessageSet_GetId $matches 0]
set email [new_CkEmail]
set success [CkImap_FetchEmail $imap 0 $uid 1 $email]
if {$success != 1} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkMessageSet $matches
delete_CkEmail $email
exit
}
puts "Mailbox INBOX, UIDVALIDITY [CkImap_get_UidValidity $imap], UID $uid"
set success [CkImap_Disconnect $imap]
if {$success != 1} then {
puts [CkImap_lastErrorText $imap]
delete_CkImap $imap
delete_CkMessageSet $matches
delete_CkEmail $email
exit
}
# The downloaded Email remains usable after disconnecting.
puts "Subject: [CkEmail_subject $email] Files: [CkEmail_get_NumAttachments $email]"
set success [CkEmail_SaveEml $email "imap-received.eml"]
if {$success != 1} then {
puts [CkEmail_lastErrorText $email]
delete_CkImap $imap
delete_CkMessageSet $matches
delete_CkEmail $email
exit
}
puts "Saved imap-received.eml."
delete_CkImap $imap
delete_CkMessageSet $matches
delete_CkEmail $email