imap4(n)
β¨ imap4 β the manual, bestie edition β¨
okay so real talk: this is imap4(n), the imap client page. Yes it's long. Yes there are three parts. Sit down, grab a strawberry π, we're doing this.
π NAME π
imap4 β imap client-side tcl implementation of imap protocol
Like, that's the whole name section. Short. Iconic. It's giving minimalism. π
β¨ SYNOPSIS β¨
Okay bestie, here's the whole cast list. Every single one of these, no skips, no cuts:
package require Tcl 8.5 9
package require imap4 ?0.5.5?
::imap4::open hostname ?port?
::imap4::starttls chan
::imap4::login chan user pass
::imap4::folders chan ?-inline? ?mboxref? ?mboxname?
::imap4::select chan ?mailbox?
::imap4::examine chan ?mailbox?
::imap4::fetch chan range ?-inline? ?attr ...?
::imap4::noop chan
::imap4::check chan
::imap4::folderinfo chan ?info?
::imap4::msginfo chan msgid ?info? ?defval?
::imap4::mboxinfo chan ?info?
::imap4::isableto chan ?capability?
::imap4::create chan mailbox
::imap4::delete chan mailbox
::imap4::rename chan oldname newname
::imap4::subscribe chan mailbox
::imap4::unsubscribe chan mailbox
::imap4::search chan expr ?...?
::imap4::close chan
::imap4::cleanup chan
::imap4::debugmode chan ?errormsg?
::imap4::store chan range data flaglist
::imap4::expunge chan
::imap4::copy chan msgid mailbox
::imap4::logout chan
the way that this is literally the entire API in one little list and it's lowkey so satisfying. β¨
π DESCRIPTION π
okay so here's the vibe. The imap4 library package gives you the client side of the Internet Message Access Protocol β that's IMAP for the people in the back β and it does it using standard sockets or a secure connection via TLS/SSL. Your choice, your vibe. π¦
And the best part? The whole thing is fully implemented in Tcl. No mysterious C blob you have to trust. Just Tcl, all the way down. not me being obsessed with that π
This document describes the procedures and explains their usage. So like, every command, what it wants, and how to not embarrass yourself using it. We got you. π«Ά
heads up bestie: this is part 1 of 3, so don't go anywhere β the options and the rest of the lore are coming. πΈ
β¨ PROCEDURES β¨
This package defines the following public procedures, and bestie, they are all lowkey iconic:
::imap4::open hostname ?port?
Opens a new IMAP connection and initializes the handler. The IMAP communication channel (handler) is returned.
hostnameβ mail server πportβ connection port, defaults to143
The namespace variable ::imap4::use_ssl can be used to establish a secure connection via TSL/SSL if set to true. In that case the default connection port defaults to 993.
Note: For connecting via SSL, the Tcl module tls must already be loaded, otherwise an error is raised.
package require tls ; # must be loaded for TLS/SSL
set ::imap4::use_ssl 1 ; # request a secure connection
set chan [::imap4::open $server] ; # default port is now 993
::imap4::starttls chan
Use this when you're tasked with connecting to an unsecure port that must be changed to a secure port prior to user login. This feature is known as STARTTLS.
chanβ imap channel
::imap4::login chan user pass
Logs in using the IMAP LOGIN command. 0 is returned on successful login.
chanβ imap channeluserβ usernamepassβ password
::imap4::folders chan ?-inline? ?mboxref? ?mboxname?
Gets a list of matching folders. 0 is returned on success.
Wildcards '*' and '%' are allowed for mboxref and mboxname. The command ::imap4::folderinfo can be used to retrieve folder information.
chanβ imap channelmboxrefβ mailbox reference, defaults to""mboxnameβ mailbox name, defaults to"*"
If -inline is specified, a compact folderlist is returned instead of the result code. All flags are converted to lowercase and leading special characters are removed.
{{Arc08 noselect} {Arc08/Private {noinferiors unmarked}} {INBOX noinferiors}}
::imap4::select chan ?mailbox?
Selects a mailbox. 0 is returned on success.
chanβ imap channelmailboxβ Path of the mailbox, defaults toINBOX
Prior to examine/select an open mailbox must be closed β see: ::imap4::close.
::imap4::examine chan ?mailbox?
"Examines" a mailbox β the read-only equivalent of ::imap4::select.
chanβ imap channelmailboxβ mailbox name or path to mailbox, defaults toINBOX
Prior to examine/select an open mailbox must be closed β see: ::imap4::close.
::imap4::fetch chan range ?-inline? ?attr ...?
Fetches attributes from messages.
The attributes are fetched and stored in the internal state, which can be retrieved with the command ::imap4::msginfo. 0 is returned on success. If -inline is specified, all records are returned as a list in the order as defined in the attr argument.
chanβ imap channelrangeβ message index in formatFROM:TOattrβ imap attributes to fetch
Note: IfFROMis omitted, the 1st message is assumed. IfTOis omitted, the last message is assumed. All message index ranges are 1-based.
::imap4::noop chan
Sends the NOOP command to the server. May get information as untagged data.
chanβ imap channel
::imap4::check chan
Sends the CHECK command to the server. Flush to disk.
chanβ imap channel
::imap4::folderinfo chan ?info?
Gets information on the recently selected folderlist. If the info argument is omitted or is a null string, the full list of information available for the mailbox is returned.
If the required information name is suffixed with a ? character, the command returns true if the information is available, or false if it is not.
chanβ imap channelinfoβ folderlist options to retrieve
Currently supported options: delim β hierarchy delimiter only, match β ref and mbox search patterns (see ::imap4::folders), names β list of folder names only, flags β list of folder names with flags in format { {name {flags}} ... } (see also compact format in function ::imap4::folders).
{{Arc08 {{\NoSelect}}} {Arc08/Private {{\NoInferiors} {\UnMarked}}} {INBOX {\NoInferiors}}}
::imap4::msginfo chan msgid ?info? ?defval?
Gets information (from previously collected using fetch) from a given msgid. If the info argument is omitted or is a null string, the list of available information options for the given message is returned.
If the required information name is suffixed with a ? character, the command returns true if the information is available, or false if it is not.
chanβ imap channelmsgidβ message numberinfoβ imap keyword to retrievedefvalβ default value, returned ifinfois empty
Note: All message index ranges are 1-based.
::imap4::mboxinfo chan ?info?
Gets information on the currently selected mailbox. If the info argument is omitted or is a null string, the list of available information options for the mailbox is returned.
If the required information name is suffixed with a ? character, the command returns true if the information is available, or false if it is not.
chanβ imap channeloptβ mailbox option to retrieve
Currently supported options: EXISTS (noof msgs), RECENT (noof 'recent' flagged msgs), FLAGS
In conjunction with OK: PERMFLAGS, UIDNEXT, UIDVAL, UNSEEN
Div. states: CURRENT, FOUND, PERM.
::imap4::select $chan INBOX
puts "[::imap4::mboxinfo $chan exists] mails in INBOX"
::imap4::isableto chan ?capability?
Tests for capability. It returns 1 if the requested capability is supported, 0 otherwise. If capability is omitted, all capability imap codes are returned as a list.
chanβ imap channelinfoβ imap keyword to retrieve
Note: Use the capability command to ask the server if not already done by the user.
::imap4::create chan mailbox
Creates a new mailbox.
chanβ imap channelmailboxβ mailbox name
::imap4::delete chan mailbox
Deletes a mailbox.
chanβ imap channelmailboxβ mailbox name
::imap4::rename chan oldname newname
Renames a mailbox.
chanβ imap channelmailboxβ old mailbox namemailboxβ new mailbox name
::imap4::subscribe chan mailbox
Subscribes to a mailbox.
chanβ imap channelmailboxβ mailbox name
::imap4::unsubscribe chan mailbox
Unsubscribes from a mailbox.
chanβ imap channelmailboxβ mailbox name
::imap4::search chan expr ?...?
Searches for mails matching search criterions. 0 is returned on success.
chanβ imap channelexprβ imap search expression
Notes: Currently the following search expressions are handled:
- Mail header flags: all mail header entries (ending with a colon
":"), like"From:","Bcc:", ... - Imap message search flags:
ANSWERED,DELETED,DRAFT,FLAGGED,RECENT,SEEN,NEW,OLD,UNANSWERED,UNDELETED,UNDRAFT,UNFLAGGED,UNSEEN,ALL - Imap header search flags:
BODY,CC,FROM,SUBJECT,TEXT,KEYWORD,BCC - Imap conditional search flags:
SMALLER,LARGER,ON,SENTBEFORE,SENTON,SENTSINCE,SINCE,BEFORE(not implemented),UID(not implemented) - Logical search conditions:
OR,NOT
::imap4::search $chan larger 4000 seen
puts "Found messages: [::imap4::mboxinfo $chan found]"
Found messages: 1 3 6 7 8 9 13 14 15 19 20
::imap4::close chan
Closes the mailbox. Permanently removes \Deleted messages and returns to the AUTH state.
chanβ imap channel
::imap4::cleanup chan
Destroys an IMAP connection and frees the used space. Closes the mailbox. Permanently removes \Deleted messages and returns to the AUTH state.
chanβ imap channel
::imap4::debugmode chan ?errormsg?
Switches the client into command line debug mode.
This is a developers mode only that passes the control to the programmer. Every line entered is sent verbatim to the server (after the addition of the request identifier). The ::imap4::debug variable is automatically set to '1' on enter.
It's possible to execute Tcl commands starting the line with a slash.
chanβ imap channelerrormsgβ optional error message to display
::imap4::store chan range data flaglist
Alters data associated with a message in the selected mailbox.
chanβ imap channelrangeβ message index in formatFROM:TOflaglistβ Flags the data operates on.dataβ The currently defined data items that can be stored are shown below. Note that all of these data types may also be suffixed with".SILENT"to suppress the untagged FETCH response.FLAGSβ Replace the flags for the message (other than\Recent) with the flaglist.+FLAGSβ Add the flags in flaglist to the existing flags for the message.-FLAGSβ Remove the flags in flaglist to the existing flags for the message.
For example:
::imap4::store $chan $start_msgid:$end_msgid +FLAGS "Deleted"
::imap4::expunge chan
Permanently removes all messages that have the \Deleted flag set from the currently selected mailbox, without the need to close the connection.
chanβ imap channel
::imap4::copy chan msgid mailbox
Copies the specified message (identified by its message number) to the named mailbox, i.e. imap folder.
chanβ imap channelmsgidβ message numbermailboxβ mailbox name
::imap4::logout chan
Informs the server that the client is done with the connection and closes the network connection. Permanently removes \Deleted messages.
A new connection will need to be established to login once more.
chanβ imap channel
β¨ EXAMPLES β¨
okay bestie, here's the whole thing in action, start to finish, no skips π copy it, run it, live your truth:
set user myusername
set pass xtremescrt
set server imap.test.tld
set FOLDER INBOX
# Connect to server
set imap [::imap4::open $server]
::imap4::login $imap $user $pass
::imap4::select $imap $FOLDER
# Output all the information about that mailbox
foreach info [::imap4::mboxinfo $imap] {
puts "$info -> [::imap4::mboxinfo $imap $info]"
}
# fetch 3 records inline
set fields {from: to: subject: size}
foreach rec [::imap4::fetch $imap :3 -inline {*}$fields] {
puts -nonewline "#[incr idx])"
for {set j 0} {$j<[llength $fields]} {incr j} {
puts "\t[lindex $fields $j] [lindex $rec $j]"
}
}
# Show all the information available about the message ID 1
puts "Available info about message 1: [::imap4::msginfo $imap 1]"
# Use the capability stuff
puts "Capabilities: [::imap4::isableto $imap]"
puts "Is able to imap4rev1? [::imap4::isableto $imap imap4rev1]"
# Cleanup
::imap4::cleanup $imap
like... the range. connecting, logging in, selecting a mailbox, dumping every scrap of mailbox info, yanking 3 records inline, peeking at message 1, checking what the server can even do, and then cleaning up after yourself like a responsible icon π§ periodt.
π TLS SECURITY CONSIDERATIONS π
so this package hands off the security for https urls and other socket connections to the TLS package. teamwork, slay π«Ά
but here's the thing, and listen up: policy decisions β like which protocols to support, which ciphers to use β those are not the responsibility of TLS, and they're not the responsibility of this package either. nope. those calls belong to whichever application is using the package, and they're probably influenced by which servers that app is gonna talk to anyway. it's giving "you pick, you're the one who knows your server situation" π
for example: in light of the recent POODLE attack discovered by Google β that's http://googleonlinesecurity.blogspot.co.uk/2014/10/this-poodle-bites-exploiting-ssl-30.html if you wanna read up β a ton of servers will disable support for the SSLv3 protocol. so to handle that change, the applications using TLS have to get patched. not this package. and not TLS itself either. and the patch might be as easy as generally activating tls1 support, like this:
package require tls
tls::init -tls1 1 ;# forcibly activate support for the TLS1 protocol
... your own application code ...
see? lowkey simple. highkey important. do not skip this part bestie π
π REFERENCES π
- Mark R. Crispin, "INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1", RFC 3501, March 2003,
http://www.rfc-editor.org/rfc/rfc3501.txt - OpenSSL,
http://www.openssl.org/
π BUGS, IDEAS, FEEDBACK π
ngl, this document and the package it describes will undoubtedly contain bugs and other problems. it's giving humble but also fr fr π please report those in the category imap4 of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. and if you've got ideas for enhancements for either the package and/or the documentation? please send those too, we want them π
when you're proposing code changes, please provide unified diffs, i.e. the output of diff -u. that's the vibe check for patches β¨
also note, and this is important: attachments are strongly preferred over inlined patches. you can attach stuff by going to the Edit form of the ticket right after you create it, and then using the left-most button in the secondary navigation bar. yep, that specific one, the left-most one π
β οΈ heads up: only a small part of rfc3501 implemented. that's not a bug note, that's just... the situation. manage expectations bestie π
π SEE ALSO π
ftp, http, imap, mime, pop3, tls
the whole friend group, all hanging out together π
π KEYWORDS π
email, imap, internet, mail, net, rfc3501, ssl, tls
π CATEGORY π
Networking
and that's the whole page, bestie!! πΈ you read all of it, that's main character energy for real. this lil masterpiece comes to you from tcllib, version 0.5.5, and it goes by imap4(n) β so if you ever forget all this, that's the name you type to summon it again. go forth and fetch your mail πβοΈπ
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms