python-sage-imap


Table of Contents
Introduction
python-sage-imap is a robust Python package designed for managing IMAP connections and performing various email operations. It provides easy-to-use interfaces for managing email folders, flags, searching emails, and sending emails using SMTP. This package is ideal for developers looking to integrate email functionalities into their applications seamlessly.
Features
- Context manager for managing IMAP connections
- Handling IMAP flags (add/remove)
- Managing IMAP folders (create/rename/delete/list)
- Searching emails with various criteria
- Sending emails using SMTP with support for attachments and templates
- Parsing and handling email messages
Installation
To install python-sage-imap, use pip:
pip install python-sage-imap
Configuration
Before using the package, you need to set up logging for better debugging and monitoring:
import logging
logging.basicConfig(level=logging.DEBUG)
Examples
Example 1: Creating an IMAP Client
This example demonstrates how to create an IMAP client using the IMAPClient class.
The IMAPClient class can also be used without a context manager; simply call connect() to establish the connection and disconnect() to close it
from sage_imap.services import IMAPClient
with IMAPClient('imap.example.com', 'username', 'password') as client:
capabilities = client.capability()
print(f"Server capabilities: {capabilities}")
status, messages = client.select("INBOX")
print(f"Selected INBOX with status: {status}")
Explanation
This example illustrates a low-level approach to working with IMAP. If you want to use imaplib directly but need the added convenience of managing the connection lifecycle, the IMAPClient class is a perfect choice. It allows you to create a connection with the IMAP server and then use all the capabilities of imaplib to customize your workflow.
By using the IMAPClient class in this way, you can take advantage of the full power of imaplib while benefiting from the convenience and safety of automatic connection management.
Example 2: Working with Folder Service
This example demonstrates how to work with folders using the IMAPFolderService.
from sage_imap.services.client import IMAPClient
from sage_imap.services.folder import IMAPFolderService
with IMAPClient('imap.example.com', 'username', 'password') as client:
folder_service = IMAPFolderService(client)
folder_service.create_folder('NewFolder')
folder_service.rename_folder('NewFolder', 'RenamedFolder')
folders = folder_service.list_folders()
print(f"Folders: {folders}")
folder_service.delete_folder('RenamedFolder')
Example 3: Working with Mailbox Methods
Below are usage examples of the IMAPClient and IMAPMailboxService classes, demonstrating their context manager capabilities and various methods:
IMAPMailboxService Example
The IMAPMailboxService class provides methods for managing mailbox operations such as selecting, closing, checking, deleting, moving, and getting status of mailboxes.
Purpose: This class allows for performing various mailbox-related operations within the context of an IMAP connection, ensuring proper error handling and cleanup.
Example Usage with Nested Context Managers:
from sage_imap.services.client import IMAPClient
from sage_imap.services.mailbox import IMAPMailboxService
from sage_imap.helpers.mailbox import DefaultMailboxes
from sage_imap.helpers.message import MessageSet
from helpers.exceptions import IMAPClientError, IMAPMailboxCheckError, IMAPMailboxClosureError
username = 'username'
password = 'password'
try:
with IMAPClient('imap.example.com', username, password) as client:
with IMAPMailboxService(client) as mailbox:
mailbox.select(DefaultMailboxes.INBOX)
msg_set = MessageSet('1,2,3')
mailbox.trash(msg_set)
mailbox.restore(msg_set, DefaultMailboxes.INBOX)
mailbox.delete(msg_set)
except IMAPClientError as e:
print(f"An error occurred with the IMAP client: {e}")
License
This project is licensed under the MIT License.