imap4(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

✨ 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 to 143

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 channel
  • user β€” username
  • pass β€” 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 channel
  • mboxref β€” 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 channel
  • mailbox β€” Path of the mailbox, defaults to INBOX

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 channel
  • mailbox β€” mailbox name or path to mailbox, defaults to INBOX

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 channel
  • range β€” message index in format FROM:TO
  • attr β€” imap attributes to fetch
Note: If FROM is omitted, the 1st message is assumed. If TO is 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 channel
  • info β€” 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 channel
  • msgid β€” message number
  • info β€” imap keyword to retrieve
  • defval β€” default value, returned if info is 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 channel
  • opt β€” 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 channel
  • info β€” 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 channel
  • mailbox β€” mailbox name

::imap4::delete chan mailbox

Deletes a mailbox.

  • chan β€” imap channel
  • mailbox β€” mailbox name

::imap4::rename chan oldname newname

Renames a mailbox.

  • chan β€” imap channel
  • mailbox β€” old mailbox name
  • mailbox β€” new mailbox name

::imap4::subscribe chan mailbox

Subscribes to a mailbox.

  • chan β€” imap channel
  • mailbox β€” mailbox name

::imap4::unsubscribe chan mailbox

Unsubscribes from a mailbox.

  • chan β€” imap channel
  • mailbox β€” mailbox name

::imap4::search chan expr ?...?

Searches for mails matching search criterions. 0 is returned on success.

  • chan β€” imap channel
  • expr β€” 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 channel
  • errormsg β€” optional error message to display

::imap4::store chan range data flaglist

Alters data associated with a message in the selected mailbox.

  • chan β€” imap channel
  • range β€” message index in format FROM:TO
  • flaglist β€” 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 channel
  • msgid β€” message number
  • mailbox β€” 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 πŸ“š


πŸ› 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