
Product
Secure Your AI-Generated Code with Socket MCP
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
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.
To install python-sage-imap
, use pip:
pip install python-sage-imap
Before using the package, you need to set up logging for better debugging and monitoring:
import logging
logging.basicConfig(level=logging.DEBUG)
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:
# 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}")
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:
IMAPClient
class is used within a context manager (with
statement). This ensures that the connection to the IMAP server is properly opened and closed.with
block is entered, the connection to the IMAP server is established, and the user is authenticated.with
block is exited, the connection is automatically closed, ensuring that resources are cleaned up properly.Why Use IMAPClient:
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.imaplib
capabilities directly through the client
object. This allows you to perform various IMAP operations seamlessly.Capabilities and Select Methods:
.capability()
method is called to retrieve the server's capabilities, providing information about what commands and features the server supports..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.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.
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)
# 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')
Below are usage examples of the IMAPClient
and IMAPMailboxService
classes, demonstrating their context manager capabilities and various methods:
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.
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:
# Select a mailbox
mailbox.select(DefaultMailboxes.INBOX)
# Delete messages temporarily (move to trash)
msg_set = MessageSet('1,2,3')
mailbox.trash(msg_set)
# Restore messages from trash to original folder
mailbox.restore(msg_set, DefaultMailboxes.INBOX)
# Permanently delete messages
mailbox.delete(msg_set)
except IMAPClientError as e:
print(f"An error occurred with the IMAP client: {e}")
This project is licensed under the MIT License.
FAQs
A Python package for managing IMAP connections and email operations.
We found that python-sage-imap demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.