Attachments

Sending and receiving files with your agents.

What are Attachments?

An Attachment is a file that is associated with a Message. This can be anything from a PDF invoice to a CSV report or an image(though we don’t recommend sending images in the first email sent. We go more into this in the deliverability section). Your agents can both send Attachments in outgoing Messages and process Attachments from incoming Messages.

Sending Attachments

To send a file, you include it in an Attachments array when sending a Message. Each object in the array represents one file and must have a content property.

  • content (required): The Base64 encoded content of your file.
  • filename (optional): The name of the file (e.g., invoice.pdf).
  • content_type (optional): The MIME type of the file (e.g., application/pdf).
1import base64
2
3# A simple text file for this example
4
5file_content = "This is the content of our report."
6
7# You must Base64 encode the file content before sending
8
9encoded_content = base64.b64encode(file_content.encode()).decode()
10
11sent_message = client.messages.send(
12inbox_id="reports@agentmail.to",
13to=["supervisor@example.com"],
14subject="Q4 Financial Report",
15text="Please see the attached report.",
16attachments=[{
17"content": encoded_content,
18"filename": "Q4-report.txt",
19"content_type": "text/plain"
20}]
21)

Retrieving Attachments

To retrieve an Attachment, you first need its attachment_id. You can get this ID from the attachments array on a Message object. Once you have the ID, you can download the file.

The API response for getting an attachment is the raw file itself, which you can then save to disk or process in memory.

From a Specific Message

If you know the Message an Attachment belongs to, you can retrieve it directly.

1inbox_id = "inbox_123"
2message_id = "msg_456"
3attachment_id = "attach_789" # From the message object
4
5file_data = client.inboxes.messages.attachments.get(
6inbox_id=inbox_id,
7message_id=message_id,
8attachment_id=attachment_id
9)
10
11# Now you can save the file
12
13with open("downloaded_file.pdf", "wb") as f:
14f.write(file_data)

From a Specific Thread

Similarly, you can retrieve an Attachment if you know the Thread it’s in, which can be more convenient for multi-message conversations.

1inbox_id = "inbox_123"
2thread_id = "thread_abc"
3attachment_id = "attach_789" # From a message within the thread
4
5file_data = client.inboxes.threads.attachments.get(
6inbox_id=inbox_id,
7thread_id=thread_id,
8attachment_id=attachment_id
9)