CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-exchangelib

Client for Microsoft Exchange Web Services (EWS) providing Django-style ORM interface for Exchange mailboxes.

Pending
Overview
Eval results
Files

messages.mddocs/

Messages and Email

Complete email functionality including sending, receiving, replying, forwarding, and managing email messages. Supports both plain text and HTML content, attachments, and advanced email features.

Capabilities

Message Creation and Properties

The Message class represents email messages with comprehensive properties and methods for email operations.

class Message:
    def __init__(
        self,
        account: Account = None,
        folder: Folder = None,
        **kwargs
    ):
        """
        Create a new email message.
        
        Parameters:
        - account: Account the message belongs to
        - folder: Folder to save the message in
        - **kwargs: Message properties (subject, body, to_recipients, etc.)
        """
    
    # Core properties
    subject: str
    body: Body | HTMLBody
    to_recipients: list[Mailbox]
    cc_recipients: list[Mailbox]
    bcc_recipients: list[Mailbox]
    sender: Mailbox
    author: Mailbox
    datetime_sent: EWSDateTime
    datetime_received: EWSDateTime
    datetime_created: EWSDateTime
    is_read: bool
    importance: str
    sensitivity: str
    categories: list[str]
    
    # Attachments
    attachments: list[FileAttachment | ItemAttachment]
    
    # Message threading
    in_reply_to: str
    references: str
    conversation_id: str
    conversation_topic: str
    
    # Message status
    is_draft: bool
    is_from_me: bool
    is_resend: bool
    is_unmodified: bool
    is_submitted: bool
    
    # Extended properties
    internet_message_headers: list
    mime_content: bytes

Sending Messages

Send messages with various options for delivery and saving.

class Message:
    def send(self, save_copy: bool = True, copy_to_folder: Folder = None):
        """
        Send the message.
        
        Parameters:
        - save_copy: Whether to save a copy of the sent message
        - copy_to_folder: Folder to save the sent copy in
        """
    
    def send_and_save(self, update_fields: list = None):
        """
        Send the message and save it to the Sent Items folder.
        
        Parameters:
        - update_fields: List of fields to update before sending
        """

Usage example:

from exchangelib import Message, Mailbox, HTMLBody

# Create and send a message
message = Message(
    account=account,
    subject='Meeting Reminder',
    body=HTMLBody('<p>Don\'t forget about our meeting tomorrow at 2 PM.</p>'),
    to_recipients=[
        Mailbox(email_address='colleague@company.com'),
        Mailbox(email_address='manager@company.com')
    ],
    cc_recipients=[Mailbox(email_address='team@company.com')]
)

message.send_and_save()

Message Replies and Forwarding

Reply to and forward messages with automatic handling of recipients and message threading.

class Message:
    def reply(
        self,
        subject: str,
        body: str,
        to_recipients=None,
        cc_recipients=None,
        bcc_recipients=None,
        author=None
    ):
        """
        Reply to this message and send immediately.
        
        Parameters:
        - subject: Reply subject
        - body: Reply body content
        - to_recipients: Optional override for reply recipients
        - cc_recipients: Optional CC recipients
        - bcc_recipients: Optional BCC recipients
        - author: Reply author (defaults to account owner)
        """
    
    def reply_all(
        self,
        subject: str,
        body: str,
        author=None
    ):
        """
        Reply to all recipients of this message and send immediately.
        
        Parameters:
        - subject: Reply subject
        - body: Reply body content
        - author: Reply author
        """

Usage example:

# Reply to a message
original_message = account.inbox.all().order_by('-datetime_received')[0]

# Reply sends immediately, no need to call send_and_save()
original_message.reply(
    subject='Re: ' + original_message.subject,
    body='Thanks for the update!'
)

# Reply all sends immediately
original_message.reply_all(
    subject='Re: ' + original_message.subject,
    body='Thanks everyone for the update!'
)

Message Content Types

Different content types for message bodies with support for plain text and HTML.

class Body:
    def __init__(self, body: str):
        """
        Plain text message body.
        
        Parameters:
        - body: Plain text content
        """
    
    body: str
    body_type: str = 'Text'

class HTMLBody:
    def __init__(self, body: str):
        """
        HTML message body.
        
        Parameters:
        - body: HTML content
        """
    
    body: str
    body_type: str = 'HTML'

Usage example:

from exchangelib import Body, HTMLBody

# Plain text message
plain_message = Message(
    account=account,
    subject='Plain Text Message',
    body=Body('This is a plain text message.'),
    to_recipients=[Mailbox(email_address='user@company.com')]
)

# HTML message
html_message = Message(
    account=account,
    subject='HTML Message',
    body=HTMLBody('''
        <html>
        <body>
            <h1>Important Update</h1>
            <p>This is an <strong>HTML</strong> message with formatting.</p>
            <ul>
                <li>Point 1</li>
                <li>Point 2</li>
            </ul>
        </body>
        </html>
    '''),
    to_recipients=[Mailbox(email_address='user@company.com')]
)

Message Management

CRUD operations for managing messages in folders.

class Message:
    def save(self, update_fields: list = None):
        """
        Save the message to its folder.
        
        Parameters:
        - update_fields: List of specific fields to update
        """
    
    def delete(self, delete_type: str = 'MoveToDeletedItems', send_meeting_cancellations: str = None):
        """
        Delete the message.
        
        Parameters:
        - delete_type: 'HardDelete', 'SoftDelete', or 'MoveToDeletedItems'
        - send_meeting_cancellations: How to handle meeting cancellations
        """
    
    def move(self, to_folder: Folder):
        """
        Move the message to another folder.
        
        Parameters:
        - to_folder: Destination folder
        """
    
    def copy(self, to_folder: Folder):
        """
        Copy the message to another folder.
        
        Parameters:
        - to_folder: Destination folder
        """
    
    def mark_as_read(self):
        """Mark the message as read."""
    
    def mark_as_unread(self):
        """Mark the message as unread."""

Bulk Message Operations

Efficient bulk operations for handling multiple messages at once.

def bulk_create(account: Account, items: list[Message], chunk_size: int = None) -> list[Message]:
    """
    Create multiple messages in bulk.
    
    Parameters:
    - account: Account to create messages in
    - items: List of Message objects to create
    - chunk_size: Number of items to process per request
    
    Returns:
    List of created Message objects with server-assigned properties
    """

def bulk_update(account: Account, items: list[Message], chunk_size: int = None) -> list[Message]:
    """
    Update multiple messages in bulk.
    
    Parameters:
    - account: Account containing the messages
    - items: List of Message objects to update
    - chunk_size: Number of items to process per request
    
    Returns:
    List of updated Message objects
    """

def bulk_delete(account: Account, items: list[Message], chunk_size: int = None, delete_type: str = 'MoveToDeletedItems') -> None:
    """
    Delete multiple messages in bulk.
    
    Parameters:
    - account: Account containing the messages
    - items: List of Message objects to delete
    - chunk_size: Number of items to process per request
    - delete_type: Type of deletion to perform
    """

Usage example:

# Create multiple messages
messages = [
    Message(
        account=account,
        subject=f'Bulk Message {i}',
        body=Body(f'This is bulk message number {i}'),
        to_recipients=[Mailbox(email_address='user@company.com')]
    )
    for i in range(5)
]

# Create all messages at once
created_messages = account.bulk_create(messages)

# Update multiple messages
for msg in created_messages:
    msg.importance = 'High'

account.bulk_update(created_messages)

Message Response Items

Special message types for handling calendar and meeting responses.

class AcceptItem:
    def __init__(self, reference_item_id: ItemId, **kwargs):
        """Accept a meeting request."""

class DeclineItem:
    def __init__(self, reference_item_id: ItemId, **kwargs):
        """Decline a meeting request."""

class TentativelyAcceptItem:
    def __init__(self, reference_item_id: ItemId, **kwargs):
        """Tentatively accept a meeting request."""

class ForwardItem:
    def __init__(self, reference_item_id: ItemId, **kwargs):
        """Forward an item."""

class ReplyToItem:
    def __init__(self, reference_item_id: ItemId, **kwargs):
        """Reply to an item."""

class ReplyAllToItem:
    def __init__(self, reference_item_id: ItemId, **kwargs):
        """Reply all to an item."""

Message Properties and Metadata

Extended properties and metadata for messages.

class Message:
    # Delivery and routing
    message_id: str
    internet_message_id: str
    references: str
    in_reply_to: str
    
    # Security and classification
    sensitivity: str  # 'Normal', 'Personal', 'Private', 'Confidential'
    importance: str  # 'Low', 'Normal', 'High'
    
    # Flags and status
    flag: Flag
    categories: list[str]
    
    # Size and content
    size: int
    has_attachments: bool
    is_associated: bool
    
    # Cultural info
    culture: str
    
    # Extended properties
    extended_properties: list[ExtendedProperty]

Install with Tessl CLI

npx tessl i tessl/pypi-exchangelib

docs

account-auth.md

advanced.md

attachments.md

calendar.md

contacts.md

datetime.md

folders.md

index.md

messages.md

search.md

tasks.md

tile.json