Email guides
Read messages and threads
Filter and paginate messages, inspect thread history, and retrieve content and attachments.
On this page
Single page#
page = mail.list_inbox(limit=25, direction="inbound", is_read=False)
for item in page.get("items", []):
print(item["message_id"], item.get("subject_snippet", ""), item.get("sender_verified", False))
next_cursor = page.get("next_cursor")Use cursor=next_cursor for the next page. An emptyitems list is normal. Optional filters include q,label, verified_only, is_read,include_archived, and direction. Select inbound or outbound direction deliberately; an inbox listing is not a delivery receipt.
All pages (auto-pagination)#
for page in mail.iter_inbox_pages(page_size=50, direction="inbound"):
for item in page.get("items", []):
print(item["message_id"])Iteration stops when there is no next cursor. Poll again later for new mail; this iterator is not a persistent event stream.
Fetch a full message#
message_id = "msg-REPLACE-WITH-INCOMING-ID"
message = mail.get_message(message_id)
metadata = message.get("metadata") or {}
print(metadata.get("Sender"), metadata.get("SubjectSnippet"))
print(message.get("body_text", ""))
if message.get("raw_mime"):
print(message["raw_mime"])
elif message.get("raw_mime_ref"):
print("Raw MIME has a temporary download reference.")| Return field | Contract |
|---|---|
| message_id / body_text | Message identifier and extracted body text; body text can be empty. |
| metadata | Opaque stored metadata, including Sender, Subject, MessageID, and trust/thread fields when present. Its inner key spelling is preserved. |
| raw_mime | Nullable; inline MIME depends on deployment configuration and size. |
| raw_mime_ref | Conditional temporary URL and expiry for large MIME; do not log or persist signed URLs. |
| inbound_attachments | Attachment manifest. Content may still be pending extraction. |
| encrypted / encryption | Conditional encryption flags and algorithm metadata. |
Fetch content and attachments#
message = mail.fetch_message(
"msg-REPLACE-WITH-INCOMING-ID",
include_attachments=True,
max_poll_seconds=30.0,
)
if message.get("body_decrypt_error"):
print("Body decryption failed:", message["body_decrypt_error"])
else:
print(message.get("body_text_decrypted", message.get("body_text", "")))
for attachment in message.get("downloaded_attachments", []):
print(attachment["index"], attachment["status"], attachment.get("error"))Without download_dir, downloaded data stays in bytes; with it, inspect each entry's path. Saved filenames are unique, so earlier downloads are preserved. Entries also expose filename, content type, encrypted/decrypted flags, status, and an optional error; local write failures appear as save_failed errors on the affected entry.
max_poll_seconds bounds polling waits; individual HTTP calls use the client's timeout_seconds. Waits are capped by the remaining budget, and a zero budget makes one attempt without sleeping. Invalid timing values fail before network calls.
iter_inbox combines pagination with full-message fetching. Supplydownload_dir, decrypt_with, and passphrasewhen your workflow requires them; inspect each result before processing.
Threads#
thread_root_id = "REPLACE-WITH-THREAD-ROOT"
for page in mail.iter_thread_pages(thread_root_id, page_size=25):
for item in page.get("items", []):
print(item["message_id"])list_thread_messages returns one page; iter_thread_pagespaginates; iter_thread_messages fetches full messages and files. Use the stored thread root returned by the API.
Message state and deletion#
message_id = "msg-REPLACE-WITH-INCOMING-ID"
mail.patch_message_read(message_id, is_read=True)
mail.patch_message_labels(message_id, add=["processed"])
mail.patch_message_archive(message_id, archived=True)
print(mail.get_unread_count()["count"])Archiving hides a record from default listings; include archived messages to find it again. delete_message(message_id) deletes the selected record. Confirm retention needs before deleting. For event-driven reads, configure a webhook.