0
# Attachments
1
2
Comprehensive attachment support for both file attachments and embedded item attachments. Includes functionality for uploading, downloading, and managing attachments across all Exchange item types.
3
4
## Capabilities
5
6
### File Attachments
7
8
```python { .api }
9
class FileAttachment:
10
def __init__(
11
self,
12
name: str = None,
13
content: bytes = None,
14
content_type: str = None,
15
content_id: str = None,
16
content_location: str = None,
17
is_inline: bool = False
18
):
19
"""Create a file attachment."""
20
21
name: str
22
content: bytes
23
content_type: str
24
content_id: str
25
size: int
26
last_modified_time: EWSDateTime
27
is_inline: bool
28
29
def save(self, path: str):
30
"""Save attachment to file system."""
31
32
@classmethod
33
def from_file(cls, path: str, name: str = None) -> FileAttachment:
34
"""Create attachment from file."""
35
```
36
37
### Item Attachments
38
39
```python { .api }
40
class ItemAttachment:
41
def __init__(self, name: str = None, item: Item = None):
42
"""Create an item attachment."""
43
44
name: str
45
item: Item
46
size: int
47
last_modified_time: EWSDateTime
48
49
def save(self):
50
"""Save the attached item."""
51
```
52
53
### Attachment Management
54
55
```python { .api }
56
class Item:
57
attachments: list[FileAttachment | ItemAttachment]
58
has_attachments: bool
59
60
def attach(self, attachment: FileAttachment | ItemAttachment):
61
"""Add an attachment to the item."""
62
63
def detach(self, attachment: FileAttachment | ItemAttachment):
64
"""Remove an attachment from the item."""
65
```
66
67
Usage examples:
68
69
```python
70
from exchangelib import FileAttachment, ItemAttachment, Message
71
72
# Create message with file attachment
73
message = Message(
74
account=account,
75
subject='Document for Review',
76
body=Body('Please review the attached document.'),
77
to_recipients=[Mailbox(email_address='reviewer@company.com')]
78
)
79
80
# Add file attachment
81
attachment = FileAttachment.from_file('/path/to/document.pdf', 'Document.pdf')
82
message.attachments.append(attachment)
83
message.save()
84
85
# Download attachments from received message
86
for item in account.inbox.filter(has_attachments=True):
87
for attachment in item.attachments:
88
if isinstance(attachment, FileAttachment):
89
attachment.save(f'/downloads/{attachment.name}')
90
91
# Attach another email as item attachment
92
item_attachment = ItemAttachment(
93
name='Referenced Email',
94
item=referenced_message
95
)
96
message.attachments.append(item_attachment)
97
```