imap-tools is a high-level Python library designed for working with email via the IMAP protocol. It provides a user-friendly interface for common email operations such as fetching, parsing, searching, moving, and deleting messages, as well as managing folders. The library is actively maintained, with frequent minor releases, and is currently at version 1.12.0.
pip install imap-toolsVerified import paths — ran on the pinned version, not inferred.
Connects to an IMAP server using environment variables for credentials, fetches the subjects and senders of the first 5 unseen emails in the INBOX, and prints them. It demonstrates the use of `MailBox` as a context manager and basic `fetch` with search criteria.
Most common dictionary operations will work. If converting to a full dictionary immediately, use `dict(msg.headers)`. For optimal performance, access specific headers directly (e.g., `msg.headers['Subject']`) to benefit from lazy loading.
Replace `MailBoxTls` with `MailBoxStartTls`. Ensure you are using the correct class for your connection type: `MailBox` for IMAPS (port 993, SSL/TLS from start), `MailBoxStartTls` for IMAP with STARTTLS (port 143, upgrade to TLS).
Upgrade your Python environment to version 3.8 or later.
Utilize the `chunks` argument (e.g., `mailbox.move(msgs, 'folder', chunks=500)`) to specify the number of UIDs to process in a single IMAP command, splitting large operations into smaller, manageable batches. For fetching, use the `limit` argument to restrict the number of messages retrieved at once.
Review existing code that processes `MailMessage.from_`, `MailMessage.to`, etc., especially if dealing with potentially malformed emails, to ensure compatibility with stricter parsing. Ensure robust error handling for address parsing.
Ensure the username and password are correct. If 2FA is enabled, generate and use an app-specific password. Also, verify that IMAP access is enabled in your email provider's settings and that the server address and port are correct.
Double-check the IMAP server address and port (e.g., 993 for SSL/TLS, 143 for STARTTLS). Ensure the correct connection class is used (`MailBox` for IMAPS/SSL, `MailBoxStartTls` for STARTTLS). Temporarily disable firewalls or antivirus to rule out interference. Verify that your email provider supports the chosen encryption method and that their SSL certificate is valid.
Specify the `charset='utf8'` argument in `MailBox.fetch()` or `MailBox.uids()` methods when your search criteria include non-ASCII characters. If dealing with internationalized domain names (IDNs) in email addresses, manually encode the domain portion using Punycode.
Use `MailBox` for connections on port 993 (IMAPS/SSL/TLS from start) and `MailBoxStartTls` for connections on port 143 (standard IMAP which then upgrades to TLS).
No dependency data recorded yet.