Managing IMAP connections and performing various email operations.
Project description
python-sage-imap
Table of Contents
- python-sage-imap
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.
from services_client import IMAPClient
with IMAPClient('imap.example.com', 'username', 'password') as client:
# Use the client for IMAP operations
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.
-
IMAPClient Context Manager:
- The
IMAPClient
class is used within a context manager (with
statement). This ensures that the connection to the IMAP server is properly opened and closed. - When the
with
block is entered, the connection to the IMAP server is established, and the user is authenticated. - When the
with
block is exited, the connection is automatically closed, ensuring that resources are cleaned up properly.
- The
-
Why Use IMAPClient:
- The
IMAPClient
exists to simplify the management of IMAP connections. By using it as a context manager, you don't have to worry about manually opening and closing the connection. This reduces the risk of resource leaks and makes your code cleaner and more maintainable. - Within the context manager, you have access to the
imaplib
capabilities directly through theclient
object. This allows you to perform various IMAP operations seamlessly.
- The
-
Capabilities and Select Methods:
- The
.capability()
method is called to retrieve the server's capabilities, providing information about what commands and features the server supports. - The
.select("INBOX")
method is used to select the "INBOX" mailbox for further operations. It returns the status of the selection and the number of messages in the mailbox.
- The
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 services_client import IMAPClient
from services_folder import IMAPFolderService
with IMAPClient('imap.example.com', 'username', 'password') as client:
folder_service = IMAPFolderService(client)
# Create a new folder
folder_service.create_folder('NewFolder')
# Rename the folder
folder_service.rename_folder('NewFolder', 'RenamedFolder')
# List all folders
folders = folder_service.list_folders()
print(f"Folders: {folders}")
# Delete the folder
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 services_client import IMAPClient
from services_mailbox import IMAPMailboxService, DefaultMailboxes, MessageSet, MailboxStatusItems
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:
# Select a mailbox
mailbox.select_mailbox(DefaultMailboxes.INBOX)
# Delete messages temporarily (move to trash)
msg_set = MessageSet('1,2,3')
mailbox.delete_temporarily(msg_set)
# Restore messages from trash to original folder
mailbox.restore_from_trash(msg_set, DefaultMailboxes.INBOX)
# Permanently delete messages
mailbox.delete_permanently(msg_set)
# Get the status of a mailbox
status = mailbox.get_mailbox_status(
DefaultMailboxes.INBOX,
MailboxStatusItems.MESSAGES
)
print(f"Mailbox status: {status}")
except IMAPClientError as e:
print(f"An error occurred with the IMAP client: {e}")
Methods of IMAPMailboxService Explained
-
select_mailbox(mailbox=DefaultMailboxes.INBOX)
- Purpose: Selects the specified mailbox for subsequent operations.
- Example:
mailbox_service.select_mailbox('INBOX')
-
close_mailbox()
- Purpose: Closes the currently selected mailbox to ensure all changes are saved and the connection is properly terminated.
- Example:
mailbox_service.close_mailbox()
-
check()
- Purpose: Sends a CHECK command to the IMAP server to synchronize the mailbox.
- Example:
mailbox_service.check()
-
delete_temporarily(msg_set: MessageSet)
- Purpose: Marks messages for deletion and moves them to the trash folder.
- Example:
mailbox_service.delete_temporarily(MessageSet('1,2,3'))
-
delete_permanently(msg_set: MessageSet)
- Purpose: Permanently deletes messages marked for deletion.
- Example:
mailbox_service.delete_permanently(MessageSet('1,2,3'))
-
move_to_folder(msg_set: MessageSet, folder: str)
- Purpose: Moves messages to the specified folder.
- Example:
mailbox_service.move_to_folder(MessageSet('1,2,3'), 'Archive')
-
restore_from_trash(msg_set: MessageSet, original_folder: str)
- Purpose: Restores messages from the trash to the original folder.
- Example:
mailbox_service.restore_from_trash(MessageSet('1,2,3'), 'INBOX')
-
**get_mailbox_status(mailbox=DefaultMailboxes.INBOX, *status_items: List[MailboxStatusItems
])**
- Purpose: Gets the status of the specified mailbox based on the provided status items.
- Example:
status = mailbox_service.get_mailbox_status('INBOX', MailboxStatusItems.MESSAGES) print(f"Mailbox status: {status}")
By utilizing these classes and methods, you can manage your IMAP mailboxes effectively and perform necessary email operations in a robust and error-handling manner.
License
This project is licensed under the MIT License.
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
File details
Details for the file python_sage_imap-0.1.1.tar.gz
.
File metadata
- Download URL: python_sage_imap-0.1.1.tar.gz
- Upload date:
- Size: 24.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdeacacf39d52e2014ea529b4e6b55adf6824e8f585a59ef4506bfc73337db8f |
|
MD5 | 1a0364fba364f9edea34bd632cb925f3 |
|
BLAKE2b-256 | ee4114252a94fd4a1488d6c468322d0c084f3dd33f390e7b8993cae5d32eefc0 |
File details
Details for the file python_sage_imap-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: python_sage_imap-0.1.1-py3-none-any.whl
- Upload date:
- Size: 29.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53204a81814d48f770f585d42b67652ddbd16f6e9d8146225a6e0e56a975ea40 |
|
MD5 | 828727aafd0e3cf80f0c2d4dd14925f9 |
|
BLAKE2b-256 | a219548aed48d9391b1912103628f6d7f881d3695d67d1ac4b5c0d196931eee6 |