🔍 Code Extractor

class FileAttachment

Maturity: 47

Represents a file attachment (text file, Word document, etc.) associated with an Outlook user event, message, or post, providing access to file metadata and content.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/attachments/file.py
Lines:
4 - 37
Complexity:
simple

Purpose

This class extends the Attachment base class to handle file attachments in Microsoft Outlook/Office365 contexts. It provides properties to access the attachment's content ID, location, and base64-encoded file contents. The class is designed to work within the Office365 SDK ecosystem for managing email attachments, calendar event attachments, and post attachments. It allows reading attachment metadata and content, as well as setting the file content programmatically.

Source Code

class FileAttachment(Attachment):
    """A file (such as a text file or Word document) attached to a user event, message, or post."""

    @property
    def content_id(self):
        """
        The ID of the attachment in the Exchange store.

        :rtype: str or None
        """
        return self.properties.get("contentId", None)

    @property
    def content_location(self):
        """
        :rtype: str or None
        """
        return self.properties.get("content_location", None)

    @property
    def content_bytes(self):
        """
        The base64-encoded contents of the file.

        :rtype: str or None
        """
        return self.properties.get("contentBytes", None)

    @content_bytes.setter
    def content_bytes(self, value):
        """
        Sets the base64-encoded contents of the file.
        """
        self.set_property("contentBytes", value)

Parameters

Name Type Default Kind
bases Attachment -

Parameter Details

__init__: Inherits constructor from Attachment base class. The exact parameters depend on the parent Attachment class implementation, but typically would include properties dictionary or context information for the Office365 API connection.

Return Value

Instantiation returns a FileAttachment object that represents a file attachment in the Office365/Outlook system. The properties return various aspects of the attachment: content_id returns the Exchange store ID (str or None), content_location returns the location string (str or None), and content_bytes returns base64-encoded file contents (str or None).

Class Interface

Methods

@property content_id(self) -> str | None property

Purpose: Gets the ID of the attachment in the Exchange store

Returns: The content ID as a string if available, otherwise None

@property content_location(self) -> str | None property

Purpose: Gets the content location of the attachment

Returns: The content location as a string if available, otherwise None

@property content_bytes(self) -> str | None property

Purpose: Gets the base64-encoded contents of the file

Returns: The base64-encoded file contents as a string if available, otherwise None

@content_bytes.setter content_bytes(self, value: str) -> None property

Purpose: Sets the base64-encoded contents of the file

Parameters:

  • value: The base64-encoded string representation of the file contents to set

Returns: None - this is a setter method that modifies the internal state

Attributes

Name Type Description Scope
properties dict Inherited from Attachment base class - stores the attachment properties including contentId, content_location, and contentBytes instance

Dependencies

  • office365

Required Imports

from office365.outlook.mail.attachments.attachment import Attachment
from office365.outlook.mail.attachments.file_attachment import FileAttachment

Usage Example

# Assuming you have an authenticated Office365 context and a message object
from office365.outlook.mail.attachments.file_attachment import FileAttachment
import base64

# Reading an existing file attachment from a message
message = context.me.messages.get_by_id('message_id')
attachments = message.attachments.get().execute_query()

for attachment in attachments:
    if isinstance(attachment, FileAttachment):
        content_id = attachment.content_id
        location = attachment.content_location
        content_bytes = attachment.content_bytes
        
        # Decode the file content
        if content_bytes:
            file_data = base64.b64decode(content_bytes)
            # Process file_data as needed

# Creating a new file attachment
new_attachment = FileAttachment()
with open('document.pdf', 'rb') as f:
    file_content = f.read()
    encoded_content = base64.b64encode(file_content).decode('utf-8')
    new_attachment.content_bytes = encoded_content

# Add to a message
message.attachments.add(new_attachment)

Best Practices

  • Always check if property values are None before using them, as attachments may not have all properties populated
  • When setting content_bytes, ensure the content is properly base64-encoded before assignment
  • Be mindful of memory usage when working with large file attachments, as content_bytes loads the entire file into memory
  • Use isinstance() checks to verify attachment type before accessing FileAttachment-specific properties
  • The class is designed to work within the Office365 SDK's query execution pattern - properties may require execute_query() calls on parent objects
  • Content_bytes should be decoded using base64.b64decode() to get the actual file binary data
  • This class inherits from Attachment, so all parent class methods and properties are also available

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class Attachment 83.1% similar

    Represents a file or item (contact, event, or message) attached to an event or message in Office 365, providing methods to download and access attachment content and metadata.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/attachments/attachment.py
  • class ItemAttachment 80.2% similar

    A class representing an attachment that contains an Outlook item (contact, event, or message) attached to a user event, message, or post.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/attachments/item.py
  • class TaskFileAttachment 78.0% similar

    Represents a file attachment (text file, Word document, etc.) associated with a Microsoft Graph todoTask, providing access to the file's base64-encoded content.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/todo/attachments/task_file.py
  • class AttachmentItem 74.7% similar

    A data class representing metadata attributes of an attachment item, including its type, name, and size.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/attachments/attachment_item.py
  • class ReferenceAttachment 71.3% similar

    A class representing a reference attachment that links to files stored on OneDrive for Business or other supported cloud storage locations, attached to Outlook events, messages, or posts.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/attachments/reference.py
← Back to Browse