Threads
`threads.list`, `list_all`, `iterate`, `get`, `update`, `trash`, `snooze`, `unsnooze` and `list_attachments`.
Reading
from openemail import openemail page = openemail.threads.list( folder='inbox', query='from:ada', label_ids=['INBOX', 'IMPORTANT'], limit=25,) next_page = ( openemail.threads.list(folder='inbox', cursor=page['nextCursor']) if page['nextCursor'] else None) thread = openemail.threads.get('thread_…')print(thread['messageCount'], thread['hasUnread'], thread['totalReplies'])The API pages threads with a pageToken. The client hands it to you as nextCursor and takes it back as cursor, like every other list, and list_all and iterate follow it for you. It is opaque: pass back what you were given and never build one.
The list filters are keyword arguments in snake_case (label_ids=, date_from=), while the keys of a request body keep the API’s camelCase names (addLabelIds on update). A page and a thread come back as dicts, so page['nextCursor'] and thread['messageCount'] read them.
from datetime import datetime, timedelta, timezone from openemail import openemail now = datetime.now(timezone.utc) last_week = openemail.threads.list_all( sort='oldest', date_from=now - timedelta(days=7), date_to=now, from_contacts=True,) for thread in openemail.threads.iterate(sort='sender'): print(thread['id'])sort, date_from, date_to and from_contacts are the thread list’s own controls. sort is newest, oldest, sender or subject, the dates take a datetime or an ISO 8601 string and both ends are included, and from_contacts keeps mail whose newest message came from a saved contact. Every order pages to the end without skipping or repeating a thread. A datetime without a tzinfo is read as local time.
Organising
from datetime import datetime, timedelta, timezone from openemail import openemail openemail.threads.update('thread_…', { 'read': True, 'addLabelIds': ['USER_DONE'], 'removeLabelIds': ['INBOX'],}) openemail.threads.trash('thread_…')openemail.threads.snooze('thread_…', datetime.now(timezone.utc) + timedelta(days=1))openemail.threads.unsnooze('thread_…')Read state IS a label on every backend here, so it travels with the label lists and the ordering is deterministic when you set both. At least one of the three fields must be present.
addLabelIds takes ids from labels.list and the system ids such as ARCHIVE and STARRED. An id that names no label is refused with a 422 label_not_found rather than created, so make the label with labels.create first. threads.list(folder='USER_DONE') lists every thread carrying a label, whichever folder it is in.
Attachments on a message
import base64from pathlib import Path from openemail import openemail files = openemail.threads.list_attachments('thread_…', 'message_…') for file in files: print(file['filename'], file['contentType'], file['size']) if file['content']: name = Path(file['filename']).name Path(name).write_bytes(base64.b64decode(file['content']))content is base64, and an empty string when the stored bytes could not be found, so check its length before decoding. The ciphertext of an encrypted message IS in this list and downloads like any other file; the PGP/MIME version part and any detached signature are not. They keep their ids in encryption.parts and nothing more.
A message that arrived encrypted
This SDK neither encrypts nor decrypts: it cannot open a message somebody else encrypted, and it cannot send an encrypted one. The send request is refused if it carries an encryption marker, because a client with no key has no business asserting one. Keys generated in the OpenEmail app live in the browser that made them and reach nothing here, and when that browser opens a sealed message the plaintext stays in it, and the stored message this call reads is still ciphertext. What threads.get gives you is the envelope, recognised. A message that arrived PGP- or S/MIME-wrapped carries an encryption dict, so an empty decodedBody stops being the only thing you are handed. It is the one key whose absence you cannot survive guessing at, and MessageEncryption in openemail.types describes it.
import sys from openemail import is_sealed, openemail thread = openemail.threads.get('thread_…') for message in thread['messages']: if not message.get('encryption'): continue if not is_sealed(message): continue print('cannot read this one:', message['encryption']['format'], file=sys.stderr)Branch with is_sealed, never on the presence of the field. Two of the five formats, pgp-signed and smime-signed, describe a body that arrived in the CLEAR beside a detached signature, so gating on presence hides mail nobody needed to hide, and the user cannot see it or explain it. is_sealed ships for exactly that reason: the server states the sealed set once, and a third copy written out of the union is the copy that drifts.
Absence is not plaintext. encryption is missing on every message stored before detection shipped, and on anything that reached the mailbox by a path where the detector never ran. It records that nobody looked, a fact about our coverage rather than about the mail, and nothing backfills it.
Where these differ from the rest
- Each entry in
ThreadResource.messagesis aMessageResource, a plaindict[str, Any]whose type names no field, not evenencryption. Typing the fields would be the client asserting a normalisation nobody performs. Readencryptionwithmessage.get('encryption')and branch withis_sealed, because a client that cannot branch on it reads a sealed message as an empty one. - A request that cannot be served faithfully is a 422
capability_unsupported, not a response that looks right and is quietly wrong.
Parameters: threads.list
folderstr- Which folder to list. The server defaults it to `inbox`, so omitting it narrows the listing rather than widening it to everything. It applies to a `query` search as well, unless the query names a folder itself with `in:` or a folder `is:` such as `is:sent`.
querystr- The mailbox search syntax. Plain words must all appear, and each matches loosely: case, accents and separators are ignored and part of a longer word counts, so `min` and `ben jamin` both find "Benjamin". A quoted phrase is matched as written apart from case and accents, so `"ben jamin"` does not find "Ben-Jamin", and filler words are dropped when something else is left to search for. When nothing matches exactly, close spellings are returned instead, so `benjimin` finds "Benjamin": a plain word, or the value of `from:`, `to:`, `cc:`, `subject:`, `body:`, `filename:` or `label:`, may differ from the start of a word by one typo (a changed, missing, extra or swapped letter) when it has four to seven letters and by two when it has eight or more, while a quoted phrase, a word containing a digit, a shorter word and an excluded word still match exactly, and the pages that follow keep matching the same way. Narrow with operators such as `from:ada`, `label:Invoices`, `is:unread`, `has:pdf`, `before:2026/01/31` and `older_than:1y`, and combine them with `OR`, parentheses and a leading `-`; a value the search cannot use is ignored rather than narrowing. Words and the `from:`, `to:`, `cc:`, `subject:` and `body:` operators read the latest message's sender, recipients, subject and the first 4,000 characters of its body with markup stripped, while `filename:` and `has:` read every attachment on the whole conversation and labels and folders read the whole conversation. It narrows the same index the unfiltered listing reads. Sealed messages store no body text, so only their sender, recipients and subject can match. A plain word also matches the name of any attachment on the conversation, whichever message carried it.
label_idsstr | Sequence[str]- Restrict the listing to threads carrying these labels. The endpoint takes a comma-separated string, and the client joins a list or a tuple into one for you. There is no limit on how many you name.
limitint- How many threads to return, from 1 to 100. Omitted, the handler uses 25. The default lives in the handler rather than the schema, so an absent value and an explicit 25 behave alike.
cursorstr- The previous page's `nextCursor`, passed back verbatim. It is the API's `pageToken` under the name every other list uses, and it is opaque, so never construct or edit one.
Response: Page[ThreadSummaryResource]
itemslist[ThreadSummaryResource]- One entry per thread in this page, lifted out of the API’s `data` envelope. Each entry is only an object marker and an id. The listing carries no subject, snippet, participants or labels, so anything more means calling `threads.get` on the threads you want.
items[].idstr- The thread's id, to hand to `threads.get`, `threads.update` and the rest unchanged. It is the same id whether the row came from a filtered listing or from a `query` search.
hasMorebool- Whether there is a further page, derived from `nextCursor` where the API does not state it.
nextCursorstr | None- The API's `nextPageToken`, to send back as `cursor` for the following page, or `None` when there is no further page. An empty token is normalised to `None`, so a falsy check and a `None` check agree.