Skip to main content

A Python package for managing IMAP connections and email operations.

Project description

python-sage-imap

Black Pylint codecov

PyPI release Supported Python versions Documentation License GitHub last commit

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:
    # 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.

  1. 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.
  2. 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 the client object. This allows you to perform various IMAP operations seamlessly.
  3. 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.

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)

    # 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 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}")

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_sage_imap-0.4.6.tar.gz (24.0 kB view details)

Uploaded Source

Built Distribution

python_sage_imap-0.4.6-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file python_sage_imap-0.4.6.tar.gz.

File metadata

  • Download URL: python_sage_imap-0.4.6.tar.gz
  • Upload date:
  • Size: 24.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Windows/11

File hashes

Hashes for python_sage_imap-0.4.6.tar.gz
Algorithm Hash digest
SHA256 0e6f6b3f0ff192889491f555c31395e20b4b1cf6868dd85d236f7f5c5a350e13
MD5 557368c7c8ea876b5d09fd41f4bb5d24
BLAKE2b-256 58367f82ef1348020b738dfbbc3b25b8ef2927c038c96f799af16390ac2e03ac

See more details on using hashes here.

File details

Details for the file python_sage_imap-0.4.6-py3-none-any.whl.

File metadata

File hashes

Hashes for python_sage_imap-0.4.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e1712d14e22e217044342c48ffa818661e6b21043cc0b9762eb4e8d8f7ef5309
MD5 056cd683d659dc3646521ff8549128e9
BLAKE2b-256 f8a5fc710172fabaf5cd09ec45817aeb0c4172ce4df17ffad3e113033cc6d5aa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page