pop API reference
A POP3 receive client (RFC 1939): the line-oriented status dialogue ("+OK" / "-ERR") over the net system library, with plaintext, implicit TLS, or STLS, and auth by USER / PASS, XOAUTH2, CRAM-MD5, or SCRAM-SHA-1 / SCRAM-SHA-256. Retrieved messages come back as strings, ready for the mime module to parse. Because it uses net, this module needs the default jennifer binary (jennifer-tiny has no network stack). A session is stateful: connect, then stat / listSizes / retrieve / deleteMessage, then quit. fetchAll wraps the common "get every message" case. A server "-ERR" throws a catchable pop.PopError (kind "pop3") with the refused command, the server's text and its RFC 2449 / 3206 responseCode (AUTH, IN-USE, SYS/TEMP, ...).
Import with import "pop.j" as pop;. See the pop guide for prose and examples.
Contents
| Name | What it is |
|---|---|
connect | Open a session: greet, optional STLS upgrade, then USER / PASS auth. |
count | Return just the number of messages waiting. |
deleteMessage | Mark message n for deletion (DELE); it is removed at QUIT. |
fetchAll | Connect, retrieve every message (without deleting), and quit. |
listSizes | Return the number and octet size of each message (LIST), in the order the server lists them. |
noop | Do nothing but keep the connection alive (NOOP) - the server resets its idle timer and replies "+OK". |
quit | End the session (committing any deletions) and close the connection. |
reset | Unmark every message marked for deletion this session (RSET), so a QUIT after it deletes nothing. |
retrieve | Fetch message n as a raw message string (RETR), ready for mime.parse. |
retrieveBytes | Fetch message n as its exact bytes (RETR): any octet, dot-stuffing undone, without the final CRLF. |
sizes | Return the octet size of each message (LIST), in the order listed, without the message numbers. |
stat | Return the mailbox message count and total size. |
top | Preview message n: its full headers plus the first lines body lines (TOP). |
topBytes | top as exact bytes: any octet, dot-stuffing undone. |
uidl | Return the stable unique id of every message (UIDL), as MessageId {number, id} pairs in message order. |
uidlOne | Return the stable unique id of one message (UIDL n). |
MessageId | struct: One message's UIDL entry: its message number and its server-assigned unique id. |
MessageSize | struct: One message's LIST entry: its message number and its size. |
Options | struct: Connection settings. |
PopError | struct: A failed POP3 operation. |
Session | struct: A live POP3 session over one connection. |
Stat | struct: Mailbox totals from STAT. |
Functions
connect
pop.connect(opts as Options)Open a session: greet, optional STLS upgrade, then USER / PASS auth.
opts- the connection settings
- Returns
Session - a live authenticated session
- Throws
PopError - kind "pop3" on a server "-ERR" reply, or a SCRAM "+OK" that skips the server-final (the server signature was never verified)
count
pop.count(session as Session)Return just the number of messages waiting.
session- the live session
- Returns
int - the message count
- Throws
PopError - kind "pop3" on a server "-ERR" reply
deleteMessage
pop.deleteMessage(session as Session, n as int)Mark message n for deletion (DELE); it is removed at QUIT.
session- the live session
n- the 1-based message number
- Throws
PopError - kind "pop3" on a server "-ERR" reply
fetchAll
pop.fetchAll(opts as Options)Connect, retrieve every message (without deleting), and quit.
opts- the connection settings
- Returns
list of string - every message as a raw string, in message order
- Throws
PopError - kind "pop3" on a server "-ERR" reply
listSizes
pop.listSizes(session as Session)Return the number and octet size of each message (LIST), in the order the server lists them. A message marked for deletion this session is not listed, so the numbers can have gaps: key on number, not on the list position.
session- the live session
- Returns
list of MessageSize - the number and size of each message
- Throws
PopError - kind "pop3" on a server "-ERR" reply or a malformed LIST line
noop
pop.noop(session as Session)Do nothing but keep the connection alive (NOOP) - the server resets its idle timer and replies "+OK". Useful between long pauses in a session.
session- the live session
- Throws
PopError - kind "pop3" on a server "-ERR" reply
quit
pop.quit(session as Session)End the session (committing any deletions) and close the connection.
session- the live session
reset
pop.reset(session as Session)Unmark every message marked for deletion this session (RSET), so a QUIT after it deletes nothing. Use it to abort a batch of deleteMessage calls before committing.
session- the live session
- Throws
PopError - kind "pop3" on a server "-ERR" reply
retrieve
pop.retrieve(session as Session, n as int)Fetch message n as a raw message string (RETR), ready for mime.parse. The text is the message as sent, dot-stuffing undone, without the final CRLF. A message that is not valid UTF-8 (raw 8-bit Latin-1, say) throws; read it with retrieveBytes.
session- the live session
n- the 1-based message number
- Returns
string - the raw message text
- Throws
PopError - kind "pop3" on a server "-ERR" reply or a message that is not valid UTF-8
retrieveBytes
pop.retrieveBytes(session as Session, n as int)Fetch message n as its exact bytes (RETR): any octet, dot-stuffing undone, without the final CRLF.
session- the live session
n- the 1-based message number
- Returns
bytes - the raw message
- Throws
PopError - kind "pop3" on a server "-ERR" reply
sizes
pop.sizes(session as Session)Return the octet size of each message (LIST), in the order listed, without the message numbers. Once a message is marked for deletion this session it is not listed, and a position no longer maps to a message number; use listSizes for the number of each size.
session- the live session
- Returns
list of int - the size in octets of each listed message
- Throws
PopError - kind "pop3" on a server "-ERR" reply or a malformed LIST line
stat
pop.stat(session as Session)Return the mailbox message count and total size.
session- the live session
- Returns
Stat - the mailbox totals
- Throws
PopError - kind "pop3" on a server "-ERR" reply or a malformed STAT reply
top
pop.top(session as Session, n as int, lines as int)Preview message n: its full headers plus the first lines body lines (TOP). lines = 0 fetches headers only - a cheap way to read Subject / From / Date (parse the result with mime.parse) and decide whether to retrieve the whole body. TOP is widely but not universally supported (a server lacking it answers "-ERR").
session- the live session
n- the 1-based message number
lines- how many leading body lines to include (0 = headers only)
- Returns
string - the header block plus the requested body lines
- Throws
PopError - kind "pop3" on a server "-ERR" reply (or no TOP support), or a response that is not valid UTF-8
topBytes
pop.topBytes(session as Session, n as int, lines as int)top as exact bytes: any octet, dot-stuffing undone.
session- the live session
n- the 1-based message number
lines- how many leading body lines to include (0 = headers only)
- Returns
bytes - the header block plus the requested body lines
- Throws
PopError - kind "pop3" on a server "-ERR" reply (or no TOP support)
uidl
pop.uidl(session as Session)Return the stable unique id of every message (UIDL), as MessageId {number, id} pairs in message order. Unlike the per-session message number, a UIDL id persists across sessions, so this is the key to leave-on-server / skip-seen: keep the ids you have processed, and next time retrieve only the numbers whose id is new.
session- the live session
- Returns
list of MessageId - the unique id of each message
- Throws
PopError - kind "pop3" on a server "-ERR" reply (or no UIDL support) or a malformed UIDL line
uidlOne
pop.uidlOne(session as Session, n as int)Return the stable unique id of one message (UIDL n).
session- the live session
n- the 1-based message number
- Returns
string - the message's persistent unique id
- Throws
PopError - kind "pop3" on a server "-ERR" reply, or a reply that is not "+OK <n> <uid>" for this
n
Structs
MessageId
One message's UIDL entry: its message number and its server-assigned unique id. The id is stable across sessions (unlike the number, which is per-session), so it is the key for "download only what is new": remember the ids you have seen, and on the next connection retrieve only the numbers whose id is new.
def struct MessageId {
number as int,
id as string
};number- the 1-based message number this session
id- the persistent unique id (RFC 1939 UIDL)
MessageSize
One message's LIST entry: its message number and its size.
def struct MessageSize {
number as int,
size as int
};number- the 1-based message number this session
size- the message size in octets
Options
Connection settings. USER / PASS and XOAUTH2 send the password or token as it is, so over transport.Security.None they are refused unless allowInsecureAuth is set; APOP, CRAM-MD5 and SCRAM never send the password.
def struct Options {
host as string,
port as int,
security as transport.Security,
user as string,
pass as string,
auth as string,
allowInsecureAuth as bool
};host- the server hostname
port- the server port (110 plaintext / STLS, 995 implicit TLS)
securitytransport.Security.None,.Tls, or.Starttlsuser- the account username
pass- the password (or access token for xoauth2)
auth- "" (default - USER / PASS), "auto" (pick the strongest mechanism the server offers, falling back to USER / PASS), "apop" (RFC 1939 APOP), "xoauth2" (
passholds the access token), "cram" (CRAM-MD5), "scram-sha-1", or "scram-sha-256" allowInsecureAuth- send USER / PASS or XOAUTH2 over an unencrypted (
.None) connection (default false - the plaintext credentials are refused)
PopError
A failed POP3 operation. Every error of kind "pop3" or "pop" is one, so its fields are safe to read once the kind matches.
def struct PopError {
kind as string,
message as string,
command as string,
responseCode as string,
text as string
};kind"pop3";"pop"for a local guard (a control character in an argument, plaintext credentials refused over.None)message<command>: -ERR ..., or what went wrongcommand- the command the server refused (
PASS,RETR, ...); "" for another failure responseCode- the bracketed response code of the
-ERR(RFC 2449 / 3206:AUTH,IN-USE,LOGIN-DELAY,SYS/TEMP,SYS/PERM), "" when none text- the server's text after
-ERRand the code, "" for another failure
Session
A live POP3 session over one connection.
def struct Session {
conn as net.Conn
};conn- the underlying network connection
Stat
Mailbox totals from STAT.
def struct Stat {
count as int,
size as int
};count- the number of messages in the mailbox
size- the total mailbox size in octets