{"id":3516,"library":"imapclient","title":"IMAPClient","description":"IMAPClient is an easy-to-use, Pythonic, and complete IMAP client library. It provides a higher-level API over Python's built-in `imaplib` module, simplifying interaction with IMAP servers. The library is actively maintained, with its current version being 3.1.0, and receives regular updates.","status":"active","version":"3.1.0","language":"en","source_language":"en","source_url":"https://github.com/mjs/imapclient/","tags":["email","imap","client","mail"],"install":[{"cmd":"pip install imapclient","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"IMAPClient","correct":"from imapclient import IMAPClient"}],"quickstart":{"code":"import os\nfrom imapclient import IMAPClient\n\n# Environment variables for credentials\nIMAP_HOST = os.environ.get('IMAP_HOST', 'imap.example.com')\nIMAP_USERNAME = os.environ.get('IMAP_USERNAME', 'your_username')\nIMAP_PASSWORD = os.environ.get('IMAP_PASSWORD', 'your_password')\n\ntry:\n    # Connect to the IMAP server using a context manager for automatic logout\n    with IMAPClient(IMAP_HOST, ssl=True) as client:\n        client.login(IMAP_USERNAME, IMAP_PASSWORD)\n        print(f\"Successfully logged in to {IMAP_HOST} as {IMAP_USERNAME}\")\n\n        # Select the INBOX folder\n        select_info = client.select_folder('INBOX')\n        print(f\"Selected INBOX: {select_info[b'EXISTS']} messages\")\n\n        # Search for all messages\n        messages = client.search(['ALL'])\n        print(f\"Found {len(messages)} messages.\")\n\n        if messages:\n            # Fetch subjects of the first 5 messages (or fewer if not enough)\n            fetch_uids = messages[:5]\n            response = client.fetch(fetch_uids, ['BODY.PEEK[HEADER.FIELDS (SUBJECT)]'])\n\n            print(\"\\n--- Subjects of first messages ---\")\n            for uid, data in response.items():\n                subject_bytes = data[b'BODY[HEADER.FIELDS (SUBJECT)]']\n                try:\n                    # Decode subject, handling potential encoding issues\n                    subject = subject_bytes.decode('utf-8', errors='ignore').strip()\n                    print(f\"UID {uid}: {subject}\")\n                except UnicodeDecodeError:\n                    print(f\"UID {uid}: Subject decoding failed\")\n        else:\n            print(\"No messages in INBOX.\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n","lang":"python","description":"This quickstart connects to an IMAP server, logs in using credentials from environment variables, selects the 'INBOX' folder, and fetches the subjects of the first few messages. It demonstrates basic connection, authentication, folder selection, and message fetching. Ensure `IMAP_HOST`, `IMAP_USERNAME`, and `IMAP_PASSWORD` environment variables are set."},"warnings":[{"fix":"Upgrade to Python 3.8+ and `imapclient >= 3.0.0`. If Python 2 is required, use `imapclient < 3.0.0`.","message":"Version 3.0.0 removed official support for Python 2.x. Applications targeting Python 2 must remain on `imapclient < 3.0.0`.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"Ensure your project runs on Python 3.8 or newer.","message":"Version 3.0.0 also removed support for Python 3.4, 3.5, and 3.6. The current officially supported Python versions are 3.8 through 3.13.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"Upgrade `imapclient` to version 3.1.0 or newer to ensure full compatibility with Python 3.14+.","message":"Users running Python 3.14+ might experience compatibility issues with `IMAP4_TLS` if using `imapclient` versions older than 3.1.0.","severity":"gotcha","affected_versions":"<3.1.0 (when used with Python 3.14+)"},{"fix":"If connecting to a server with a self-signed certificate (not recommended for production without proper CA setup), you may need to configure the `ssl_context` to disable hostname checking or verification. For example:\n```python\nimport ssl\ncontext = ssl.create_default_context()\ncontext.check_hostname = False\ncontext.verify_mode = ssl.CERT_NONE\nclient = IMAPClient(host, ssl_context=context)\n```","message":"Since version 1.0, IMAPClient enables strict TLS certificate verification by default. Connections to servers with self-signed or invalid certificates may fail.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}