A Python package for retrieving, parsing, and sending emails
Project description
mailsuite
A Python package for retrieving, parsing, and sending emails.
Features
- Provider-agnostic mailbox abstraction (
mailsuite.mailbox)- Single
MailboxConnectioninterface for IMAP, Microsoft Graph, Gmail, and on-disk Maildir - Fetch message identifiers from any folder, retrieve their raw RFC 822 content, and move or delete messages
- Folder management across every backend — create, rename, move, merge,
delete, and existence checks, with consistent
FolderExistsError/FolderNotFoundErrorsemantics - Watch a folder for new messages — the IMAP
IDLEcommand (with periodic session refresh) on the IMAP backend, polling on the cloud backends - Unified
send_message()on backends that support sending (Microsoft Graph, Gmail) — IMAP and Maildir users send throughmailsuite.smtp.send_email - Username/password or OAuth2 (XOAUTH2 / OAUTHBEARER) login for IMAP and SMTP
- Automatic IMAP reconnection after dropped connections and timeouts
- Always uses
/as the folder hierarchy separator, converting to the server's separator and prepending its namespace automatically, and stripping folder-name characters that collide with the separator - Works around backend quirks across Gmail, Microsoft 365, Exchange,
Dovecot, and DavMail, including:
- Gmail / Google Workspace returning an empty
IDLEresponse - Random Microsoft 365 / Exchange
BAD/ "unexpected response" errors - Nonstandard hierarchy separators and namespaces
- Gmail / Google Workspace returning an empty
- Single
- Consistent email parsing
- SHA256 hashes of attachments
- Parsed
Authentication-ResultsandDKIM-Signatureheaders - Parse Microsoft Outlook
.msgfiles usingmsgconvert
- Simplified email creation and sending
- Easily add attachments, plain text, and HTML
- Uses opportunistic encryption (
STARTTLS) with SMTP by default
- DKIM signing and verification
- Generate RSA keypairs and the matching DNS TXT record
- Sign outbound mail with a sensible default header set
- Verify one or many
DKIM-Signatureheaders on a received message
- ARC (Authenticated Received Chain) sealing and verification
- Seal forwarded mail with an ARC set, extending an existing chain
- Verify the ARC chain on a received message and read its
cvresult
Installation
Base install (IMAP, SMTP, DKIM, Maildir, parsing):
pip install mailsuite
If you would like to be able to parse Microsoft Outlook .msg files, install
msgconvert. On Debian-based Linux distributions, msgconvert can be installed
via sudo apt-get install libemail-outlook-message-perl. Other systems can use
cpan -i Email::Outlook::Message.
The Microsoft Graph and Gmail backends are optional extras — the cloud SDKs aren't pulled in unless you ask for them:
pip install "mailsuite[msgraph]" # Microsoft Graph (msgraph-sdk + azure-identity)
pip install "mailsuite[gmail]" # Gmail (google-api-python-client + google-auth-oauthlib)
pip install "mailsuite[all]" # both
Importing mailsuite.mailbox never requires the extras. Referencing
MSGraphConnection or GmailConnection without the matching extra
installed raises an ImportError pointing at the right install command.
Microsoft Graph notes
MSGraphConnection defaults to the worldwide cloud
(https://graph.microsoft.com). To target a sovereign cloud or any
other Graph endpoint, pass graph_url:
MSGraphConnection(..., graph_url="https://graph.microsoft.us")
The azure-identity token cache lives under name="mailsuite" by
default. Applications migrating from a previous installation that used a
different cache name can pass it through token_cache_name= so existing
cached AuthenticationRecords and tokens continue to work — for
example, token_cache_name="parsedmarc" keeps users authenticated
across the migration.
Microsoft Graph permissions
Grant the appropriate Microsoft Graph API permissions on the app
registration based on which MSGraphConnection operations you need.
Combine permissions across rows when you need multiple capabilities —
e.g., to both read and send mail in a delegated flow against your own
mailbox, grant Mail.ReadWrite and Mail.Send.
| Use case | Delegated (own mailbox) | Delegated (shared mailbox) | App-only |
|---|---|---|---|
Read messages only (fetch_message, fetch_messages) |
Mail.Read |
Mail.Read.Shared |
Mail.Read |
| Read + modify (mark read, delete, move, create folder) | Mail.ReadWrite |
Mail.ReadWrite.Shared |
Mail.ReadWrite |
Send mail (send_message) |
Mail.Send |
Mail.Send.Shared |
Mail.Send |
Delegated flows (DeviceCode, UsernamePassword) targeting a shared
mailbox — i.e. when the mailbox argument differs from username —
use the .Shared variants. App-only flows (ClientAssertion, ClientSecret,
Certificate) do not need the .Shared variants since application
permissions span every mailbox in the tenant (unless restricted by an
Application Access Policy).
For delegated flows, MSGraphConnection requests Mail.ReadWrite (or
Mail.ReadWrite.Shared) at authenticate time, so even read-only
callers must consent to at least Mail.ReadWrite. App-only flows
authenticate with https://graph.microsoft.com/.default, which grants
whichever permissions the app registration has consented.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mailsuite-2.2.2.tar.gz.
File metadata
- Download URL: mailsuite-2.2.2.tar.gz
- Upload date:
- Size: 42.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Hatch/1.17.0 {"ci":null,"cpu":"x86_64","distro":{"id":"noble","libc":{"lib":"glibc","version":"2.39"},"name":"Ubuntu","version":"24.04"},"implementation":{"name":"CPython","version":"3.12.3"},"installer":{"name":"hatch","version":"1.17.0"},"openssl_version":"OpenSSL 3.0.13 30 Jan 2024","python":"3.12.3","system":{"name":"Linux","release":"6.6.114.1-microsoft-standard-WSL2"}} HTTPX2/2.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1a45d3539e72022a5ee0966e9deb66924363f6121a3041a963d54725773e103
|
|
| MD5 |
ac31106f0cb0dff9d8af86ce9f42ff8c
|
|
| BLAKE2b-256 |
9c8fe0119c739835bc0c7077e8c2f37a167771efbe65de090dbc8c70009f1bce
|
File details
Details for the file mailsuite-2.2.2-py3-none-any.whl.
File metadata
- Download URL: mailsuite-2.2.2-py3-none-any.whl
- Upload date:
- Size: 50.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: Hatch/1.17.0 {"ci":null,"cpu":"x86_64","distro":{"id":"noble","libc":{"lib":"glibc","version":"2.39"},"name":"Ubuntu","version":"24.04"},"implementation":{"name":"CPython","version":"3.12.3"},"installer":{"name":"hatch","version":"1.17.0"},"openssl_version":"OpenSSL 3.0.13 30 Jan 2024","python":"3.12.3","system":{"name":"Linux","release":"6.6.114.1-microsoft-standard-WSL2"}} HTTPX2/2.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b4ca1118a5194d563628bab72546087a41eaa88c1eba0ecce9610939119227e
|
|
| MD5 |
73f7dc12495f7284325ec5ae75d8e163
|
|
| BLAKE2b-256 |
61943d06e55746dcb9fbe8a044daa57ab3d9324e50e02f62702740bf7bef3910
|